diff --git a/.rustfmt.toml b/.rustfmt.toml index 952a284..18425d7 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -6,17 +6,17 @@ array_width = 100 attr_fn_like_width = 100 fn_call_width = 100 -format_code_in_doc_comments = true +format_code_in_doc_comments = false fn_single_line = true format_macro_matchers = true -format_macro_bodues = true +format_macro_bodies = true format_strings = true merge_derives = false overflow_delimited_expr = true reorder_modules = false use_field_init_shorthand = true use_try_shorthand = true -wrap_comments = true +wrap_comments = false where_single_line = true unstable_features = true empty_item_single_line = true diff --git a/Cargo.lock b/Cargo.lock index 1d15ff7..3804f8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,11 +4,11 @@ version = 3 [[package]] name = "amplify" -version = "4.0.0" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26966af46e0d200e8bf2b7f16230997c1c3f2d141bc27ccc091c012ed527b58" +checksum = "213783efd41ed08b2861e86dd5aac633cf612f861e9222ace8b3621bd58af5c6" dependencies = [ - "amplify_derive 3.0.0", + "amplify_derive 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "amplify_num", "ascii", "wasm-bindgen", @@ -16,26 +16,26 @@ dependencies = [ [[package]] name = "amplify_derive" -version = "3.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "580f12b79a9e10cfa8d2515128d83a53f387e290096a75904c92b8a2a4d542a6" +version = "3.0.1" dependencies = [ - "amplify_syn 2.0.0", + "amplify", + "amplify_syn 2.0.1", "proc-macro2", "quote", - "syn", + "syn 1.0.109", + "wasm-bindgen-test", ] [[package]] name = "amplify_derive" version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c87df0f28e6eb1f2d355f29ba6793fa9ca643967528609608d5cbd70bd68f9d1" dependencies = [ - "amplify", - "amplify_syn 2.0.1", + "amplify_syn 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2", "quote", - "syn", - "wasm-bindgen-test", + "syn 1.0.109", ] [[package]] @@ -46,22 +46,22 @@ checksum = "ddce3bc63e807ea02065e8d8b702695f3d302ae4158baddff8b0ce5c73947251" [[package]] name = "amplify_syn" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29b08d74fda406d5a94abfdcdb91ba13bb06562ccf0a4581867fa924ca242b01" +version = "2.0.1" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] name = "amplify_syn" version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7736fb8d473c0d83098b5bac44df6a561e20470375cd8bcae30516dc889fd62a" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -72,9 +72,9 @@ checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" [[package]] name = "bumpalo" -version = "3.12.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" [[package]] name = "cfg-if" @@ -94,42 +94,39 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.60" +version = "0.3.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" +checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a" dependencies = [ "wasm-bindgen", ] [[package]] name = "log" -version = "0.4.17" +version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "once_cell" -version = "1.17.0" +version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "proc-macro2" -version = "1.0.50" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.23" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] @@ -142,9 +139,20 @@ checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" [[package]] name = "syn" -version = "1.0.107" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" dependencies = [ "proc-macro2", "quote", @@ -153,15 +161,15 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.6" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "wasm-bindgen" -version = "0.2.83" +version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -169,24 +177,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.83" +version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.38", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.33" +version = "0.4.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23639446165ca5a5de86ae1d8896b737ae80319560fbaa4c2887b7da6e7ebd7d" +checksum = "c02dbc21516f9f1f04f187958890d7e6026df8d16540b7ad9492bc34a67cea03" dependencies = [ "cfg-if", "js-sys", @@ -196,9 +204,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.83" +version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -206,28 +214,28 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.83" +version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.38", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.83" +version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" +checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" [[package]] name = "wasm-bindgen-test" -version = "0.3.33" +version = "0.3.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d2fff962180c3fadf677438054b1db62bee4aa32af26a45388af07d1287e1d" +checksum = "6e6e302a7ea94f83a6d09e78e7dc7d9ca7b186bc2829c24a22d0753efd680671" dependencies = [ "console_error_panic_hook", "js-sys", @@ -239,9 +247,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.33" +version = "0.3.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4683da3dfc016f704c9f82cf401520c4f1cb3ee440f7f52b3d6ac29506a49ca7" +checksum = "ecb993dd8c836930ed130e020e77d9b2e65dd0fbab1b67c790b0f5d80b11a575" dependencies = [ "proc-macro2", "quote", @@ -249,9 +257,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.60" +version = "0.3.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f" +checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/src/lib.rs b/src/lib.rs index b42e166..a66a974 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -648,11 +648,13 @@ pub fn derive_getters(input: TokenStream) -> TokenStream { /// * `amplify::Wrapper` /// * [`AsRef`] /// * [`core::borrow::Borrow`] +/// You may skip `AsRef` and `Borrow` implementations with `#[wrapper(NoRefs)]`. /// /// You can implement additional derives, it they are implemented for the /// wrapped type, using `#[wrapper()]` proc macro: /// 1. Reference access to the inner type: /// * `Deref` for implementing [`core::ops::Deref`] +/// * `AsSlice` for implementing [`AsRef`]`<[u8]>` /// * `BorrowSlice` for implementing /// [`core::borrow::Borrow`]`<[Self::Inner]>` /// 2. Formatting: @@ -775,11 +777,14 @@ pub fn derive_wrapper(input: TokenStream) -> TokenStream { /// * `amplify::WrapperMut` /// * [`AsMut`] /// * [`core::borrow::BorrowMut`] +/// You may skip `AsMut` and `BorrowMut` implementations with +/// `#[wrapper_mut(NoRefs)]`. /// /// You can implement additional derives, it they are implemented for the /// wrapped type, using `#[wrapper()]` proc macro: /// 1. Reference access to the inner type: /// * `DerefMut` for implementing [`core::ops::DerefMut`] +/// * `AsSliceMut` for implementing [`AsMut`]`<[u8]>` /// * `BorrowSliceMut` for implementing /// [`core::borrow::BorrowMut`]`<[Self::Inner]>` /// 2. Indexed access to the inner type: @@ -832,7 +837,7 @@ pub fn derive_wrapper(input: TokenStream) -> TokenStream { /// Display /// )] /// #[display(inner)] -/// #[wrapper(NumberFmt, MathOps, BoolOps)] +/// #[wrapper(NumberFmt, MathOps, BoolOps, FromStr)] /// #[wrapper_mut(MathAssign, BitAssign)] /// struct Int64(i64); /// ``` diff --git a/src/wrapper.rs b/src/wrapper.rs index 771c9ba..a25e18c 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -24,8 +24,9 @@ use crate::util::get_amplify_crate; const NAME: &str = "wrapper"; const EXAMPLE: &str = r#"#[wrapper(LowerHex, Add)]"#; -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)] enum Wrapper { + NoRefs, // Formatting FromStr, Display, @@ -38,6 +39,9 @@ enum Wrapper { UpperExp, // References Deref, + AsRef, + AsSlice, + Borrow, BorrowSlice, // Indexes Index, @@ -71,10 +75,14 @@ enum Wrapper { BitOps, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)] enum WrapperMut { + NoRefs, // References DerefMut, + AsMut, + AsSliceMut, + BorrowMut, BorrowSliceMut, // Indexes IndexMut, @@ -103,9 +111,12 @@ enum WrapperMut { BitAssign, } -pub trait FromPath: Sized + Copy { +pub trait FromPath: Sized + Copy + Ord { const IDENT: &'static str; const DERIVE: &'static str; + const NO_REFS: Self; + fn default_set() -> Vec; + fn is_not_ref(&self) -> bool; fn from_path(path: &Path) -> Result>; fn populate(self, list: &mut Vec); } @@ -113,6 +124,11 @@ pub trait FromPath: Sized + Copy { impl FromPath for Wrapper { const IDENT: &'static str = "wrapper"; const DERIVE: &'static str = "Wrapper"; + const NO_REFS: Self = Self::NoRefs; + + fn default_set() -> Vec { vec![Wrapper::AsRef, Wrapper::Borrow] } + + fn is_not_ref(&self) -> bool { *self != Wrapper::AsRef && *self != Wrapper::Borrow } fn from_path(path: &Path) -> Result> { path.segments.first().map_or( @@ -123,11 +139,16 @@ impl FromPath for Wrapper { "Display" => Some(Wrapper::Display), "Debug" => Some(Wrapper::Debug), "Octal" => Some(Wrapper::Octal), + "FromHex" => Some(Wrapper::FromHex), "LowerHex" => Some(Wrapper::LowerHex), "UpperHex" => Some(Wrapper::UpperHex), "LowerExp" => Some(Wrapper::LowerExp), "UpperExp" => Some(Wrapper::UpperExp), + "NoRefs" => Some(Wrapper::NoRefs), + "AsRef" => Some(Wrapper::AsRef), + "AsSlice" => Some(Wrapper::AsSlice), "Deref" => Some(Wrapper::Deref), + "Borrow" => Some(Wrapper::Borrow), "BorrowSlice" => Some(Wrapper::BorrowSlice), "Index" => Some(Wrapper::Index), "IndexRange" => Some(Wrapper::IndexRange), @@ -210,7 +231,12 @@ impl FromPath for Wrapper { } impl Wrapper { - pub fn into_token_stream2(self, input: &DeriveInput) -> TokenStream2 { + pub fn into_token_stream2( + self, + input: &DeriveInput, + from: &Type, + field: &TokenStream2, + ) -> TokenStream2 { let impl_generics_params = input.generics.params.clone(); let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); let ident_name = &input.ident; @@ -226,10 +252,7 @@ impl Wrapper { #[inline] fn from_str(s: &str) -> Result { use ::core::str::FromStr; - use #amplify_crate::Wrapper; - Ok(Wrapper::from_inner( - ::Inner::from_str(s)?, - )) + <#from as FromStr>::from_str(s).map(Self::from) } } }, @@ -239,8 +262,7 @@ impl Wrapper { { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use #amplify_crate::Wrapper; - ::core::fmt::Display::fmt(Wrapper::as_inner(self), f) + ::core::fmt::Display::fmt(&self.#field, f) } } }, @@ -250,8 +272,7 @@ impl Wrapper { { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use #amplify_crate::Wrapper; - ::core::fmt::Debug::fmt(Wrapper::as_inner(self), f) + ::core::fmt::Debug::fmt(&self.#field, f) } } }, @@ -261,8 +282,7 @@ impl Wrapper { { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use #amplify_crate::Wrapper; - ::core::fmt::Octal::fmt(Wrapper::as_inner(self), f) + ::core::fmt::Octal::fmt(&self.#field, f) } } }, @@ -277,8 +297,7 @@ impl Wrapper { + ExactSizeIterator + DoubleEndedIterator, { - use #amplify_crate::Wrapper; - ::Inner::from_byte_iter(iter).map(Self::from_inner) + <#from as #amplify_crate::hex::FromHex>::from_byte_iter(iter).map(Self::from) } } }, @@ -288,8 +307,7 @@ impl Wrapper { { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use #amplify_crate::Wrapper; - ::core::fmt::LowerHex::fmt(Wrapper::as_inner(self), f) + ::core::fmt::LowerHex::fmt(&self.#field, f) } } }, @@ -299,8 +317,7 @@ impl Wrapper { { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use #amplify_crate::Wrapper; - ::core::fmt::UpperHex::fmt(Wrapper::as_inner(self), f) + ::core::fmt::UpperHex::fmt(&self.#field, f) } } }, @@ -310,8 +327,7 @@ impl Wrapper { { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use #amplify_crate::Wrapper; - ::core::fmt::LowerExp::fmt(Wrapper::as_inner(self), f) + ::core::fmt::LowerExp::fmt(&self.#field, f) } } }, @@ -321,8 +337,7 @@ impl Wrapper { { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use #amplify_crate::Wrapper; - ::core::fmt::UpperExp::fmt(Wrapper::as_inner(self), f) + ::core::fmt::UpperExp::fmt(&self.#field, f) } } }, @@ -330,11 +345,38 @@ impl Wrapper { #[automatically_derived] impl #impl_generics ::core::ops::Deref for #ident_name #ty_generics #where_clause { - type Target = ::Inner; + type Target = #from; #[inline] fn deref(&self) -> &Self::Target { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self) + &self.#field + } + } + }, + Wrapper::AsRef => quote! { + #[automatically_derived] + impl #impl_generics ::core::convert::AsRef<#from> for #ident_name #ty_generics #where_clause { + #[inline] + fn as_ref(&self) -> &#from { + &self.#field + } + } + }, + Wrapper::AsSlice => quote! { + #[automatically_derived] + impl #impl_generics AsRef<[u8]> for #ident_name #ty_generics #where_clause + { + #[inline] + fn as_ref(&self) -> &[u8] { + AsRef::<[u8]>::as_ref(&self.#field) + } + } + }, + Wrapper::Borrow => quote! { + #[automatically_derived] + impl #impl_generics ::core::borrow::Borrow<#from> for #ident_name #ty_generics #where_clause { + #[inline] + fn borrow(&self) -> &#from { + &self.#field } } }, @@ -344,8 +386,7 @@ impl Wrapper { { #[inline] fn borrow(&self) -> &[u8] { - use #amplify_crate::Wrapper; - ::core::borrow::Borrow::<[u8]>::borrow(Wrapper::as_inner(self)) + ::core::borrow::Borrow::<[u8]>::borrow(&self.#field) } } }, @@ -358,12 +399,11 @@ impl Wrapper { #[automatically_derived] impl <#impl_generics_params> ::core::ops::Index for #ident_name #ty_generics #where_clause { - type Output = <::Inner as ::core::ops::Index>::Output; + type Output = <#from as ::core::ops::Index>::Output; #[inline] fn index(&self, index: usize) -> &Self::Output { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self).index(index) + self.#field.index(index) } } } @@ -373,12 +413,11 @@ impl Wrapper { #[automatically_derived] impl <#impl_generics_params> ::core::ops::Index<::core::ops::Range> for #ident_name #ty_generics #where_clause { - type Output = <::Inner as ::core::ops::Index<::core::ops::Range>>::Output; + type Output = <#from as ::core::ops::Index<::core::ops::Range>>::Output; #[inline] fn index(&self, index: ::core::ops::Range) -> &Self::Output { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self).index(index) + self.#field.index(index) } } } @@ -388,12 +427,11 @@ impl Wrapper { #[automatically_derived] impl <#impl_generics_params> ::core::ops::Index<::core::ops::RangeFrom> for #ident_name #ty_generics #where_clause { - type Output = <::Inner as ::core::ops::Index<::core::ops::RangeFrom>>::Output; + type Output = <#from as ::core::ops::Index<::core::ops::RangeFrom>>::Output; #[inline] fn index(&self, index: ::core::ops::RangeFrom) -> &Self::Output { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self).index(index) + self.#field.index(index) } } } @@ -403,12 +441,11 @@ impl Wrapper { #[automatically_derived] impl <#impl_generics_params> ::core::ops::Index<::core::ops::RangeTo> for #ident_name #ty_generics #where_clause { - type Output = <::Inner as ::core::ops::Index<::core::ops::RangeTo>>::Output; + type Output = <#from as ::core::ops::Index<::core::ops::RangeTo>>::Output; #[inline] fn index(&self, index: ::core::ops::RangeTo) -> &Self::Output { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self).index(index) + self.#field.index(index) } } } @@ -418,12 +455,11 @@ impl Wrapper { #[automatically_derived] impl <#impl_generics_params> ::core::ops::Index<::core::ops::RangeInclusive> for #ident_name #ty_generics #where_clause { - type Output = <::Inner as ::core::ops::Index<::core::ops::RangeInclusive>>::Output; + type Output = <#from as ::core::ops::Index<::core::ops::RangeInclusive>>::Output; #[inline] fn index(&self, index: ::core::ops::RangeInclusive) -> &Self::Output { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self).index(index) + self.#field.index(index) } } } @@ -433,12 +469,11 @@ impl Wrapper { #[automatically_derived] impl <#impl_generics_params> ::core::ops::Index<::core::ops::RangeToInclusive> for #ident_name #ty_generics #where_clause { - type Output = <::Inner as ::core::ops::Index<::core::ops::RangeInclusive>>::Output; + type Output = <#from as ::core::ops::Index<::core::ops::RangeInclusive>>::Output; #[inline] fn index(&self, index: ::core::ops::RangeToInclusive) -> &Self::Output { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self).index(index) + self.#field.index(index) } } } @@ -448,12 +483,11 @@ impl Wrapper { #[automatically_derived] impl <#impl_generics_params> ::core::ops::Index<::core::ops::RangeFull> for #ident_name #ty_generics #where_clause { - type Output = <::Inner as ::core::ops::Index<::core::ops::RangeFull>>::Output; + type Output = <#from as ::core::ops::Index<::core::ops::RangeFull>>::Output; #[inline] fn index(&self, index: ::core::ops::RangeFull) -> &Self::Output { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self).index(index) + self.#field.index(index) } } } @@ -466,8 +500,7 @@ impl Wrapper { #[inline] fn neg(self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Neg::neg(Wrapper::into_inner(self))) + Self { #field: ::core::ops::Neg::neg(self.#field) } } } }, @@ -479,8 +512,7 @@ impl Wrapper { #[inline] fn not(self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Not::not(Wrapper::into_inner(self))) + Self { #field: ::core::ops::Not::not(self.#field) } } } }, @@ -492,8 +524,7 @@ impl Wrapper { #[inline] fn add(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Add::add(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::Add::add(self.#field, rhs.#field) } } } }, @@ -505,8 +536,7 @@ impl Wrapper { #[inline] fn sub(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Sub::sub(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::Sub::sub(self.#field, rhs.#field) } } } }, @@ -518,8 +548,7 @@ impl Wrapper { #[inline] fn mul(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Mul::mul(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::Mul::mul(self.#field, rhs.#field) } } } }, @@ -531,8 +560,7 @@ impl Wrapper { #[inline] fn div(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Div::div(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::Div::div(self.#field, rhs.#field) } } } }, @@ -544,8 +572,7 @@ impl Wrapper { #[inline] fn rem(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Rem::rem(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::Rem::rem(self.#field, rhs.#field) } } } }, @@ -557,8 +584,7 @@ impl Wrapper { #[inline] fn shl(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Shl::shl(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::Shl::shl(self.#field, rhs.#field) } } } }, @@ -570,8 +596,7 @@ impl Wrapper { #[inline] fn shr(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::Shr::shr(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::Shr::shr(self.#field, rhs.#field) } } } }, @@ -583,8 +608,7 @@ impl Wrapper { #[inline] fn bitand(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::BitAnd::bitand(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::BitAnd::bitand(self.#field, rhs.#field) } } } }, @@ -596,8 +620,7 @@ impl Wrapper { #[inline] fn bitor(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::BitOr::bitor(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::BitOr::bitor(self.#field, rhs.#field) } } } }, @@ -609,12 +632,18 @@ impl Wrapper { #[inline] fn bitxor(self, rhs: Self) -> Self { - use #amplify_crate::Wrapper; - Wrapper::from_inner(::core::ops::BitXor::bitxor(Wrapper::into_inner(self), rhs.into_inner())) + Self { #field: ::core::ops::BitXor::bitxor(self.#field, rhs.#field) } } } }, - _ => unreachable!(), + Wrapper::NoRefs | + Wrapper::Hex | + Wrapper::Exp | + Wrapper::NumberFmt | + Wrapper::RangeOps | + Wrapper::MathOps | + Wrapper::BoolOps | + Wrapper::BitOps => unreachable!(), } } } @@ -622,13 +651,22 @@ impl Wrapper { impl FromPath for WrapperMut { const IDENT: &'static str = "wrapper_mut"; const DERIVE: &'static str = "WrapperMut"; + const NO_REFS: Self = Self::NoRefs; + + fn default_set() -> Vec { vec![WrapperMut::AsMut, WrapperMut::BorrowMut] } + + fn is_not_ref(&self) -> bool { *self != WrapperMut::AsMut && *self != WrapperMut::BorrowMut } fn from_path(path: &Path) -> Result> { path.segments.first().map_or( Err(attr_err!(path.span(), NAME, "must contain at least one identifier", EXAMPLE)), |segment| { Ok(match segment.ident.to_string().as_str() { + "NoRefs" => Some(WrapperMut::NoRefs), "DerefMut" => Some(WrapperMut::DerefMut), + "AsMut" => Some(WrapperMut::AsMut), + "AsSliceMut" => Some(WrapperMut::AsSliceMut), + "BorrowMut" => Some(WrapperMut::BorrowMut), "BorrowSliceMut" => Some(WrapperMut::BorrowSliceMut), "IndexMut" => Some(WrapperMut::IndexMut), "IndexRangeMut" => Some(WrapperMut::IndexRangeMut), @@ -696,7 +734,12 @@ impl FromPath for WrapperMut { } impl WrapperMut { - pub fn into_token_stream2(self, input: &DeriveInput) -> TokenStream2 { + pub fn into_token_stream2( + self, + input: &DeriveInput, + _from: &Type, + field: &TokenStream2, + ) -> TokenStream2 { let impl_generics_params = input.generics.params.clone(); let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); let ident_name = &input.ident; @@ -709,8 +752,35 @@ impl WrapperMut { { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self) + &mut self.#field + } + } + }, + WrapperMut::AsMut => quote! { + #[automatically_derived] + impl #impl_generics ::core::convert::AsMut<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause { + #[inline] + fn as_mut(&mut self) -> &mut ::Inner { + &mut self.#field + } + } + }, + WrapperMut::AsSliceMut => quote! { + #[automatically_derived] + impl #impl_generics AsMut<[u8]> for #ident_name #ty_generics #where_clause + { + #[inline] + fn as_mut(&mut self) -> &mut [u8] { + AsMut::<[u8]>::as_mut(&mut self.#field) + } + } + }, + WrapperMut::BorrowMut => quote! { + #[automatically_derived] + impl #impl_generics ::core::borrow::BorrowMut<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause { + #[inline] + fn borrow_mut(&mut self) -> &mut ::Inner { + &mut self.#field } } }, @@ -720,8 +790,7 @@ impl WrapperMut { { #[inline] fn borrow_mut(&mut self) -> &mut [u8] { - use #amplify_crate::WrapperMut; - ::core::borrow::BorrowMut::<[u8]>::borrow_mut(WrapperMut::as_inner_mut(self)) + ::core::borrow::BorrowMut::<[u8]>::borrow_mut(&mut self.#field) } } }, @@ -736,8 +805,7 @@ impl WrapperMut { { #[inline] fn index_mut(&mut self, index: usize) -> &mut Self::Output { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self).index_mut(index) + self.as_mut().index_mut(index) } } } @@ -749,8 +817,7 @@ impl WrapperMut { { #[inline] fn index_mut(&mut self, index: ::core::ops::Range) -> &mut Self::Output { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self).index_mut(index) + self.as_mut().index_mut(index) } } } @@ -762,8 +829,7 @@ impl WrapperMut { { #[inline] fn index_mut(&mut self, index: ::core::ops::RangeFrom) -> &mut Self::Output { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self).index_mut(index) + self.as_mut().index_mut(index) } } } @@ -775,8 +841,7 @@ impl WrapperMut { { #[inline] fn index_mut(&mut self, index: ::core::ops::RangeTo) -> &mut Self::Output { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self).index_mut(index) + self.as_mut().index_mut(index) } } } @@ -788,8 +853,7 @@ impl WrapperMut { { #[inline] fn index_mut(&mut self, index: ::core::ops::RangeInclusive) -> &mut Self::Output { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self).index_mut(index) + self.as_mut().index_mut(index) } } } @@ -801,8 +865,7 @@ impl WrapperMut { { #[inline] fn index_mut(&mut self, index: ::core::ops::RangeToInclusive) -> &mut Self::Output { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self).index_mut(index) + self.as_mut().index_mut(index) } } } @@ -814,8 +877,7 @@ impl WrapperMut { { #[inline] fn index_mut(&mut self, index: ::core::ops::RangeFull) -> &mut Self::Output { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self).index_mut(index) + self.as_mut().index_mut(index) } } } @@ -826,8 +888,7 @@ impl WrapperMut { { #[inline] fn add_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::AddAssign::add_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::AddAssign::add_assign(&mut self.#field, rhs.#field) } } }, @@ -837,8 +898,7 @@ impl WrapperMut { { #[inline] fn sub_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::SubAssign::sub_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::SubAssign::sub_assign(&mut self.#field, rhs.#field) } } }, @@ -848,8 +908,7 @@ impl WrapperMut { { #[inline] fn mul_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::MulAssign::mul_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::MulAssign::mul_assign(&mut self.#field, rhs.#field) } } }, @@ -859,8 +918,7 @@ impl WrapperMut { { #[inline] fn div_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::DivAssign::div_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::DivAssign::div_assign(&mut self.#field, rhs.#field) } } }, @@ -870,8 +928,7 @@ impl WrapperMut { { #[inline] fn rem_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::RemAssign::rem_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::RemAssign::rem_assign(&mut self.#field, rhs.#field) } } }, @@ -881,8 +938,7 @@ impl WrapperMut { { #[inline] fn shl_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::ShlAssign::shl_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::ShlAssign::shl_assign(&mut self.#field, rhs.#field) } } }, @@ -892,8 +948,7 @@ impl WrapperMut { { #[inline] fn shr_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::ShrAssign::shr_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::ShrAssign::shr_assign(&mut self.#field, rhs.#field) } } }, @@ -903,8 +958,7 @@ impl WrapperMut { { #[inline] fn bitand_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::BitAndAssign::bitand_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::BitAndAssign::bitand_assign(&mut self.#field, rhs.#field) } } }, @@ -914,8 +968,7 @@ impl WrapperMut { { #[inline] fn bitor_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::BitOrAssign::bitor_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::BitOrAssign::bitor_assign(&mut self.#field, rhs.#field) } } }, @@ -925,12 +978,15 @@ impl WrapperMut { { #[inline] fn bitxor_assign(&mut self, rhs: Self) { - use #amplify_crate::{Wrapper, WrapperMut}; - ::core::ops::BitXorAssign::bitxor_assign(WrapperMut::as_inner_mut(self), rhs.into_inner()) + ::core::ops::BitXorAssign::bitxor_assign(&mut self.#field, rhs.#field) } } }, - _ => unreachable!(), + WrapperMut::NoRefs | + WrapperMut::RangeMut | + WrapperMut::MathAssign | + WrapperMut::BoolAssign | + WrapperMut::BitAssign => unreachable!(), } } } @@ -943,7 +999,9 @@ pub(crate) fn inner(input: DeriveInput) -> Result { let (field, from) = get_params(&input)?; let wrappers = get_wrappers::(&input)?; - let wrapper_derive = wrappers.iter().map(|w| w.into_token_stream2(&input)); + let wrapper_derive = wrappers + .iter() + .map(|w| w.into_token_stream2(&input, &from, &field)); Ok(quote! { #[automatically_derived] @@ -970,26 +1028,7 @@ pub(crate) fn inner(input: DeriveInput) -> Result { impl #impl_generics ::core::convert::From<#ident_name #ty_generics> for #from #where_clause { #[inline] fn from(wrapped: #ident_name #ty_generics) -> Self { - use #amplify_crate::Wrapper; - Wrapper::into_inner(wrapped) - } - } - - #[automatically_derived] - impl #impl_generics ::core::convert::AsRef<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause { - #[inline] - fn as_ref(&self) -> &::Inner { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self) - } - } - - #[automatically_derived] - impl #impl_generics ::core::borrow::Borrow<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause { - #[inline] - fn borrow(&self) -> &::Inner { - use #amplify_crate::Wrapper; - Wrapper::as_inner(self) + wrapped.#field } } @@ -1002,10 +1041,12 @@ pub(crate) fn inner_mut(input: DeriveInput) -> Result { let ident_name = &input.ident; let amplify_crate = get_amplify_crate(&input); - let (field, _from) = get_params(&input)?; + let (field, from) = get_params(&input)?; let wrappers = get_wrappers::(&input)?; - let wrapper_derive = wrappers.iter().map(|w| w.into_token_stream2(&input)); + let wrapper_derive = wrappers + .iter() + .map(|w| w.into_token_stream2(&input, &from, &field)); Ok(quote! { #[automatically_derived] @@ -1016,24 +1057,6 @@ pub(crate) fn inner_mut(input: DeriveInput) -> Result { } } - #[automatically_derived] - impl #impl_generics ::core::convert::AsMut<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause { - #[inline] - fn as_mut(&mut self) -> &mut ::Inner { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self) - } - } - - #[automatically_derived] - impl #impl_generics ::core::borrow::BorrowMut<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause { - #[inline] - fn borrow_mut(&mut self) -> &mut ::Inner { - use #amplify_crate::WrapperMut; - WrapperMut::as_inner_mut(self) - } - } - #( #wrapper_derive )* }) } @@ -1119,7 +1142,7 @@ fn get_params(input: &DeriveInput) -> Result<(TokenStream2, Type)> { } fn get_wrappers(input: &DeriveInput) -> Result> { - let mut wrappers = vec![]; + let mut wrappers = T::default_set(); const WRAPPER_DERIVE_ERR: &str = "Wrapper attributes must be in a form of type list"; for attr in input .attrs @@ -1145,5 +1168,8 @@ fn get_wrappers(input: &DeriveInput) -> Result> { _ => return Err(attr_err!(attr, WRAPPER_DERIVE_ERR)), } } + if wrappers.contains(&T::NO_REFS) { + wrappers = wrappers.into_iter().filter(T::is_not_ref).collect(); + } Ok(wrappers) } diff --git a/syn/src/attr.rs b/syn/src/attr.rs index b4214e0..9c6bc76 100644 --- a/syn/src/attr.rs +++ b/syn/src/attr.rs @@ -626,8 +626,8 @@ impl ParametrizedAttr { /// [`AttrReq::path_req`], [`AttrReq::integer_req`], /// [`AttrReq::float_req`], [`AttrReq::char_req`], /// [`AttrReq::bytes_req`], [`AttrReq::string_req`] correspondingly. - /// 2. [`ParametrizedAttr::paths`] values - /// matching ones specified in [`AttrReq::arg_req`] with values set to + /// 2. [`ParametrizedAttr::paths`] values matching ones specified in + /// [`AttrReq::arg_req`] with values set to /// [`crate::ListReq::Predefined::default`] are moved into /// [`ParametrizedAttr::args`] field. pub fn check(&mut self, req: AttrReq) -> Result<(), Error> {