diff --git a/Cargo.toml b/Cargo.toml index d92b1a303..a557339a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,8 @@ members = [ "eth2/utils/ssz_derive", "eth2/utils/swap_or_not_shuffle", "eth2/utils/fisher_yates_shuffle", + "eth2/utils/test_random", + "eth2/utils/test_random_derive", "beacon_node", "beacon_node/db", "beacon_node/beacon_chain", diff --git a/eth2/utils/test_random/Cargo.toml b/eth2/utils/test_random/Cargo.toml new file mode 100644 index 000000000..6346aa628 --- /dev/null +++ b/eth2/utils/test_random/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "test_random" +version = "0.1.0" +authors = ["thojest "] +edition = "2018" + +[dependencies] +rand = "0.5.5" \ No newline at end of file diff --git a/eth2/utils/test_random/src/lib.rs b/eth2/utils/test_random/src/lib.rs new file mode 100644 index 000000000..aa0d38a5a --- /dev/null +++ b/eth2/utils/test_random/src/lib.rs @@ -0,0 +1,39 @@ +use rand::RngCore; + +pub trait TestRandom +where + T: RngCore, +{ + fn random_for_test(rng: &mut T) -> Self; +} + +impl TestRandom for u64 { + fn random_for_test(rng: &mut T) -> Self { + rng.next_u64() + } +} + +impl TestRandom for u32 { + fn random_for_test(rng: &mut T) -> Self { + rng.next_u32() + } +} + +impl TestRandom for usize { + fn random_for_test(rng: &mut T) -> Self { + rng.next_u32() as usize + } +} + +impl TestRandom for Vec +where + U: TestRandom, +{ + fn random_for_test(rng: &mut T) -> Self { + vec![ + ::random_for_test(rng), + ::random_for_test(rng), + ::random_for_test(rng), + ] + } +} diff --git a/eth2/utils/test_random_derive/Cargo.toml b/eth2/utils/test_random_derive/Cargo.toml new file mode 100644 index 000000000..e596ad29c --- /dev/null +++ b/eth2/utils/test_random_derive/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "test_random_derive" +version = "0.1.0" +authors = ["thojest "] +edition = "2018" +description = "Procedural derive macros for implementation of TestRandom trait" + +[lib] +proc-macro = true + +[dependencies] +syn = "0.15" +quote = "0.6" +test_random = {path = "../test_random"} + diff --git a/eth2/utils/test_random_derive/src/lib.rs b/eth2/utils/test_random_derive/src/lib.rs new file mode 100644 index 000000000..27e62d31a --- /dev/null +++ b/eth2/utils/test_random_derive/src/lib.rs @@ -0,0 +1,15 @@ +extern crate proc_macro; + +use crate::proc_macro::TokenStream; +use quote::quote; +use syn; +use syn::DeriveInput; + +#[proc_macro_derive(TestRandom)] +pub fn test_random_derive(input: TokenStream) -> TokenStream { + let ast = syn::parse(input).unwrap(); + + impl_test_random(&ast) +} + +fn impl_test_random(ast: &DeriveInput) -> TokenStream {}