Skip to content

Commit 4359ca7

Browse files
committed
wrapper: allow to skip AsRef, AsMut, Borrow and BorrowMut implementations
1 parent 29b0954 commit 4359ca7

File tree

2 files changed

+82
-40
lines changed

2 files changed

+82
-40
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ pub fn derive_getters(input: TokenStream) -> TokenStream {
648648
/// * `amplify::Wrapper`
649649
/// * [`AsRef`]
650650
/// * [`core::borrow::Borrow`]
651+
/// You may skip `AsRef` and `Borrow` implementations with `#[wrapper(NoRefs)]`.
651652
///
652653
/// You can implement additional derives, it they are implemented for the
653654
/// wrapped type, using `#[wrapper()]` proc macro:
@@ -775,6 +776,7 @@ pub fn derive_wrapper(input: TokenStream) -> TokenStream {
775776
/// * `amplify::WrapperMut`
776777
/// * [`AsMut`]
777778
/// * [`core::borrow::BorrowMut`]
779+
/// You may skip `AsMut` and `BorrowMut` implementations with `#[wrapper_mut(NoRefs)]`.
778780
///
779781
/// You can implement additional derives, it they are implemented for the
780782
/// wrapped type, using `#[wrapper()]` proc macro:

src/wrapper.rs

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ use crate::util::get_amplify_crate;
2424
const NAME: &str = "wrapper";
2525
const EXAMPLE: &str = r#"#[wrapper(LowerHex, Add)]"#;
2626

27-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
27+
#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)]
2828
enum Wrapper {
29+
NoRefs,
2930
// Formatting
3031
FromStr,
3132
Display,
@@ -38,6 +39,8 @@ enum Wrapper {
3839
UpperExp,
3940
// References
4041
Deref,
42+
AsRef,
43+
Borrow,
4144
BorrowSlice,
4245
// Indexes
4346
Index,
@@ -71,10 +74,13 @@ enum Wrapper {
7174
BitOps,
7275
}
7376

74-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
77+
#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)]
7578
enum WrapperMut {
79+
NoRefs,
7680
// References
7781
DerefMut,
82+
AsMut,
83+
BorrowMut,
7884
BorrowSliceMut,
7985
// Indexes
8086
IndexMut,
@@ -103,16 +109,28 @@ enum WrapperMut {
103109
BitAssign,
104110
}
105111

106-
pub trait FromPath: Sized + Copy {
112+
pub trait FromPath: Sized + Copy + Ord {
107113
const IDENT: &'static str;
108114
const DERIVE: &'static str;
115+
const NO_REFS: Self;
116+
fn default_set() -> Vec<Self>;
117+
fn is_not_ref(&self) -> bool;
109118
fn from_path(path: &Path) -> Result<Option<Self>>;
110119
fn populate(self, list: &mut Vec<Self>);
111120
}
112121

113122
impl FromPath for Wrapper {
114123
const IDENT: &'static str = "wrapper";
115124
const DERIVE: &'static str = "Wrapper";
125+
const NO_REFS: Self = Self::NoRefs;
126+
127+
fn default_set() -> Vec<Self> {
128+
vec![Wrapper::AsRef, Wrapper::Borrow]
129+
}
130+
131+
fn is_not_ref(&self) -> bool {
132+
*self != Wrapper::AsRef && *self != Wrapper::Borrow
133+
}
116134

117135
fn from_path(path: &Path) -> Result<Option<Self>> {
118136
path.segments.first().map_or(
@@ -128,7 +146,10 @@ impl FromPath for Wrapper {
128146
"UpperHex" => Some(Wrapper::UpperHex),
129147
"LowerExp" => Some(Wrapper::LowerExp),
130148
"UpperExp" => Some(Wrapper::UpperExp),
149+
"NoRefs" => Some(Wrapper::NoRefs),
150+
"AsRef" => Some(Wrapper::AsRef),
131151
"Deref" => Some(Wrapper::Deref),
152+
"Borrow" => Some(Wrapper::Borrow),
132153
"BorrowSlice" => Some(Wrapper::BorrowSlice),
133154
"Index" => Some(Wrapper::Index),
134155
"IndexRange" => Some(Wrapper::IndexRange),
@@ -339,6 +360,26 @@ impl Wrapper {
339360
}
340361
}
341362
},
363+
Wrapper::AsRef => quote! {
364+
#[automatically_derived]
365+
impl #impl_generics ::core::convert::AsRef<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause {
366+
#[inline]
367+
fn as_ref(&self) -> &<Self as #amplify_crate::Wrapper>::Inner {
368+
use #amplify_crate::Wrapper;
369+
Wrapper::as_inner(self)
370+
}
371+
}
372+
},
373+
Wrapper::Borrow => quote! {
374+
#[automatically_derived]
375+
impl #impl_generics ::core::borrow::Borrow<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause {
376+
#[inline]
377+
fn borrow(&self) -> &<Self as #amplify_crate::Wrapper>::Inner {
378+
use #amplify_crate::Wrapper;
379+
Wrapper::as_inner(self)
380+
}
381+
}
382+
},
342383
Wrapper::BorrowSlice => quote! {
343384
#[automatically_derived]
344385
impl #impl_generics ::core::borrow::Borrow<[u8]> for #ident_name #ty_generics #where_clause
@@ -623,13 +664,25 @@ impl Wrapper {
623664
impl FromPath for WrapperMut {
624665
const IDENT: &'static str = "wrapper_mut";
625666
const DERIVE: &'static str = "WrapperMut";
667+
const NO_REFS: Self = Self::NoRefs;
668+
669+
fn default_set() -> Vec<Self> {
670+
vec![WrapperMut::AsMut, WrapperMut::BorrowMut]
671+
}
672+
673+
fn is_not_ref(&self) -> bool {
674+
*self != WrapperMut::AsMut && *self != WrapperMut::BorrowMut
675+
}
626676

627677
fn from_path(path: &Path) -> Result<Option<Self>> {
628678
path.segments.first().map_or(
629679
Err(attr_err!(path.span(), NAME, "must contain at least one identifier", EXAMPLE)),
630680
|segment| {
631681
Ok(match segment.ident.to_string().as_str() {
682+
"NoRefs" => Some(WrapperMut::NoRefs),
632683
"DerefMut" => Some(WrapperMut::DerefMut),
684+
"AsMut" => Some(WrapperMut::AsMut),
685+
"BorrowMut" => Some(WrapperMut::BorrowMut),
633686
"BorrowSliceMut" => Some(WrapperMut::BorrowSliceMut),
634687
"IndexMut" => Some(WrapperMut::IndexMut),
635688
"IndexRangeMut" => Some(WrapperMut::IndexRangeMut),
@@ -715,6 +768,26 @@ impl WrapperMut {
715768
}
716769
}
717770
},
771+
WrapperMut::AsMut => quote! {
772+
#[automatically_derived]
773+
impl #impl_generics ::core::convert::AsMut<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause {
774+
#[inline]
775+
fn as_mut(&mut self) -> &mut <Self as #amplify_crate::Wrapper>::Inner {
776+
use #amplify_crate::WrapperMut;
777+
WrapperMut::as_inner_mut(self)
778+
}
779+
}
780+
},
781+
WrapperMut::BorrowMut => quote! {
782+
#[automatically_derived]
783+
impl #impl_generics ::core::borrow::BorrowMut<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause {
784+
#[inline]
785+
fn borrow_mut(&mut self) -> &mut <Self as #amplify_crate::Wrapper>::Inner {
786+
use #amplify_crate::WrapperMut;
787+
WrapperMut::as_inner_mut(self)
788+
}
789+
}
790+
},
718791
WrapperMut::BorrowSliceMut => quote! {
719792
#[automatically_derived]
720793
impl #impl_generics ::core::borrow::BorrowMut<[u8]> for #ident_name #ty_generics #where_clause
@@ -976,24 +1049,6 @@ pub(crate) fn inner(input: DeriveInput) -> Result<TokenStream2> {
9761049
}
9771050
}
9781051

979-
#[automatically_derived]
980-
impl #impl_generics ::core::convert::AsRef<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause {
981-
#[inline]
982-
fn as_ref(&self) -> &<Self as #amplify_crate::Wrapper>::Inner {
983-
use #amplify_crate::Wrapper;
984-
Wrapper::as_inner(self)
985-
}
986-
}
987-
988-
#[automatically_derived]
989-
impl #impl_generics ::core::borrow::Borrow<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause {
990-
#[inline]
991-
fn borrow(&self) -> &<Self as #amplify_crate::Wrapper>::Inner {
992-
use #amplify_crate::Wrapper;
993-
Wrapper::as_inner(self)
994-
}
995-
}
996-
9971052
#( #wrapper_derive )*
9981053
})
9991054
}
@@ -1017,24 +1072,6 @@ pub(crate) fn inner_mut(input: DeriveInput) -> Result<TokenStream2> {
10171072
}
10181073
}
10191074

1020-
#[automatically_derived]
1021-
impl #impl_generics ::core::convert::AsMut<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause {
1022-
#[inline]
1023-
fn as_mut(&mut self) -> &mut <Self as #amplify_crate::Wrapper>::Inner {
1024-
use #amplify_crate::WrapperMut;
1025-
WrapperMut::as_inner_mut(self)
1026-
}
1027-
}
1028-
1029-
#[automatically_derived]
1030-
impl #impl_generics ::core::borrow::BorrowMut<<#ident_name #impl_generics as #amplify_crate::Wrapper>::Inner> for #ident_name #ty_generics #where_clause {
1031-
#[inline]
1032-
fn borrow_mut(&mut self) -> &mut <Self as #amplify_crate::Wrapper>::Inner {
1033-
use #amplify_crate::WrapperMut;
1034-
WrapperMut::as_inner_mut(self)
1035-
}
1036-
}
1037-
10381075
#( #wrapper_derive )*
10391076
})
10401077
}
@@ -1120,7 +1157,7 @@ fn get_params(input: &DeriveInput) -> Result<(TokenStream2, Type)> {
11201157
}
11211158

11221159
fn get_wrappers<T: FromPath>(input: &DeriveInput) -> Result<Vec<T>> {
1123-
let mut wrappers = vec![];
1160+
let mut wrappers = T::default_set();
11241161
const WRAPPER_DERIVE_ERR: &str = "Wrapper attributes must be in a form of type list";
11251162
for attr in input
11261163
.attrs
@@ -1146,5 +1183,8 @@ fn get_wrappers<T: FromPath>(input: &DeriveInput) -> Result<Vec<T>> {
11461183
_ => return Err(attr_err!(attr, WRAPPER_DERIVE_ERR)),
11471184
}
11481185
}
1186+
if wrappers.contains(&T::NO_REFS) {
1187+
wrappers = wrappers.into_iter().filter(T::is_not_ref).collect();
1188+
}
11491189
Ok(wrappers)
11501190
}

0 commit comments

Comments
 (0)