diff --git a/examples/ci-tests/src/simple-example.rs b/examples/ci-tests/src/simple-example.rs index cb3a7c2..2166b25 100644 --- a/examples/ci-tests/src/simple-example.rs +++ b/examples/ci-tests/src/simple-example.rs @@ -15,17 +15,14 @@ fn display_test_data() { let f0: types::Byte = 0x12u8.into(); let f2 = types::Byte3::new_builder().nth1(f0).build(); let f29 = types::StructB::new_builder() - .f2(0x34u8.into()) + .f2(0x34u8) .f4(f2.clone()) .build(); let f41 = { - let mut f41_builder = types::Bytes::new_builder() - .push(0x12.into()) - .push(0x12.into()) - .push(0x13.into()); - assert_eq!(f41_builder.replace(1, 0x34.into()), Some(0x12.into())); - assert_eq!(f41_builder.replace(2, 0x56.into()), Some(0x13.into())); - assert_eq!(f41_builder.replace(3, 0x56.into()), None); + let mut f41_builder = types::Bytes::new_builder().push(0x12).push(0x12).push(0x13); + assert_eq!(f41_builder.replace(1, 0x34), Some(0x12.into())); + assert_eq!(f41_builder.replace(2, 0x56), Some(0x13.into())); + assert_eq!(f41_builder.replace(3, 0x56), None); f41_builder.build() }; let f43 = types::Byte3Vec::new_builder() diff --git a/examples/ci-tests/tests/proptest.rs b/examples/ci-tests/tests/proptest.rs index 32ceaf0..3614fe0 100644 --- a/examples/ci-tests/tests/proptest.rs +++ b/examples/ci-tests/tests/proptest.rs @@ -52,7 +52,7 @@ fn arbitrary_table0() -> impl Strategy { } fn arbitrary_table1() -> impl Strategy { - any::().prop_map(|data| types::Table1::new_builder().f1(data.into()).build()) + any::().prop_map(|data| types::Table1::new_builder().f1(data).build()) } fn arbitrary_table5() -> impl Strategy { diff --git a/examples/ci-tests/tests/simple.rs b/examples/ci-tests/tests/simple.rs index 514ab93..000db56 100644 --- a/examples/ci-tests/tests/simple.rs +++ b/examples/ci-tests/tests/simple.rs @@ -45,15 +45,15 @@ macro_rules! test_option_set_default { } macro_rules! test_vector_push_default { - ($type:ident, $expected1:expr, $expected2:expr, $expected3:expr) => { + ($type:ident, $item:ident, $expected1:expr, $expected2:expr, $expected3:expr) => { let t = types::$type::default(); - let t = test_vector_push_default!($type, t, $expected1); - let t = test_vector_push_default!($type, t, $expected2); - let _ = test_vector_push_default!($type, t, $expected3); + let t = test_vector_push_default!($type, $item, t, $expected1); + let t = test_vector_push_default!($type, $item, t, $expected2); + let _ = test_vector_push_default!($type, $item, t, $expected3); }; - ($type:ident, $input:ident, $expected:expr) => {{ + ($type:ident, $item:ident, $input:ident, $expected:expr) => {{ let expected = $expected; - let builder = $input.as_builder().push(Default::default()); + let builder = $input.as_builder().push(types::$item::default()); let result = builder.build(); assert_eq!( result.as_slice(), @@ -426,6 +426,7 @@ fn option_set_default() { fn fixvec_push_default() { test_vector_push_default!( Bytes, + Byte, s!("0x\ 01000000\ 00\ @@ -444,6 +445,7 @@ fn fixvec_push_default() { ); test_vector_push_default!( Words, + Word, s!("0x\ 01000000\ 0000\ @@ -462,6 +464,7 @@ fn fixvec_push_default() { ); test_vector_push_default!( Byte3Vec, + Byte3, s!("0x\ 01000000\ 000000\ @@ -480,6 +483,7 @@ fn fixvec_push_default() { ); test_vector_push_default!( Byte7Vec, + Byte7, s!("0x\ 01000000\ 00000000_000000\ @@ -498,6 +502,7 @@ fn fixvec_push_default() { ); test_vector_push_default!( StructIVec, + StructI, s!("0x\ 01000000\ 00000000\ @@ -516,6 +521,7 @@ fn fixvec_push_default() { ); test_vector_push_default!( StructJVec, + StructJ, s!("0x\ 01000000\ 00000000_000000\ @@ -534,6 +540,7 @@ fn fixvec_push_default() { ); test_vector_push_default!( StructPVec, + StructP, s!("0x\ 01000000\ 00000000_00000000\ @@ -581,8 +588,8 @@ fn dynvec_push_default() { 00000000\ 00000000\ "); - test_vector_push_default!(BytesVec, s1, s2, s3); - test_vector_push_default!(WordsVec, s1, s2, s3); + test_vector_push_default!(BytesVec, Bytes, s1, s2, s3); + test_vector_push_default!(WordsVec, Words, s1, s2, s3); let s1 = s!("0x\ 08000000\ \ @@ -601,8 +608,8 @@ fn dynvec_push_default() { 10000000\ 10000000\ "); - test_vector_push_default!(ByteOptVec, s1, s2, s3); - test_vector_push_default!(WordOptVec, s1, s2, s3); - test_vector_push_default!(WordsOptVec, s1, s2, s3); - test_vector_push_default!(BytesOptVec, s1, s2, s3); + test_vector_push_default!(ByteOptVec, ByteOpt, s1, s2, s3); + test_vector_push_default!(WordOptVec, WordOpt, s1, s2, s3); + test_vector_push_default!(WordsOptVec, WordsOpt, s1, s2, s3); + test_vector_push_default!(BytesOptVec, BytesOpt, s1, s2, s3); } diff --git a/examples/lazy-reader-tests/Cargo.lock b/examples/lazy-reader-tests/Cargo.lock index f9c152a..13d1f04 100644 --- a/examples/lazy-reader-tests/Cargo.lock +++ b/examples/lazy-reader-tests/Cargo.lock @@ -113,7 +113,7 @@ checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "molecule" -version = "0.7.5" +version = "0.8.0" dependencies = [ "bytes", "cfg-if", @@ -121,7 +121,7 @@ dependencies = [ [[package]] name = "molecule-codegen" -version = "0.7.5" +version = "0.8.0" dependencies = [ "case", "molecule", diff --git a/tools/codegen/src/generator/languages/rust/builder/setters.rs b/tools/codegen/src/generator/languages/rust/builder/setters.rs index 851a025..45fb28a 100644 --- a/tools/codegen/src/generator/languages/rust/builder/setters.rs +++ b/tools/codegen/src/generator/languages/rust/builder/setters.rs @@ -12,8 +12,11 @@ impl ImplSetters for ast::Option_ { fn impl_setters(&self) -> m4::TokenStream { let inner = entity_name(self.item().typ().name()); quote!( - pub fn set(mut self, v: Option<#inner>) -> Self { - self.0 = v; + pub fn set(mut self, v: T) -> Self + where + T: ::core::convert::Into> + { + self.0 = v.into(); self } ) @@ -40,8 +43,11 @@ impl ImplSetters for ast::Array { let inner = entity_name(self.item().typ().name()); let item_count = usize_lit(self.item_count()); let entire_setter = quote!( - pub fn set(mut self, v: [#inner; #item_count]) -> Self { - self.0 = v; + pub fn set(mut self, v: T) -> Self + where + T: ::core::convert::Into<[#inner; #item_count]> + { + self.0 = v.into(); self } ); @@ -50,8 +56,11 @@ impl ImplSetters for ast::Array { let index = usize_lit(idx); let func = func_name(&format!("nth{}", idx)); quote!( - pub fn #func(mut self, v: #inner) -> Self { - self.0[#index] = v; + pub fn #func(mut self, v: T) -> Self + where + T: ::core::convert::Into<#inner> + { + self.0[#index] = v.into(); self } ) @@ -95,8 +104,11 @@ fn impl_setters_for_struct_or_table(inner: &[ast::FieldDecl]) -> m4::TokenStream let field_name = field_name(f.name()); let field_type = entity_name(f.typ().name()); quote!( - pub fn #field_name(mut self, v: #field_type) -> Self { - self.#field_name = v; + pub fn #field_name(mut self, v: T) -> Self + where + T: ::core::convert::Into<#field_type> + { + self.#field_name = v.into(); self } ) @@ -110,20 +122,27 @@ fn impl_setters_for_struct_or_table(inner: &[ast::FieldDecl]) -> m4::TokenStream fn impl_setters_for_vector(inner_name: &str) -> m4::TokenStream { let inner = entity_name(inner_name); quote!( - pub fn set(mut self, v: Vec<#inner>) -> Self { + pub fn set(mut self, v: Vec<#inner>) -> Self + { self.0 = v; self } - pub fn push(mut self, v: #inner) -> Self { - self.0.push(v); + pub fn push(mut self, v: T) -> Self + where + T: ::core::convert::Into<#inner> + { + self.0.push(v.into()); self } pub fn extend>(mut self, iter: T) -> Self { self.0.extend(iter); self } - pub fn replace(&mut self, index: usize, v: #inner) -> Option<#inner> { - self.0.get_mut(index).map(|item| ::core::mem::replace(item, v)) + pub fn replace(&mut self, index: usize, v: T) -> Option<#inner> + where + T: ::core::convert::Into<#inner> + { + self.0.get_mut(index).map(|item| ::core::mem::replace(item, v.into())) } ) }