#[derive(Wrapper)]
{
// Attributes available to this derive:
#[wrap]
#[wrapper]
#[amplify_crate]
}
Expand description
Creates rust new type wrapping existing type. Can be used in structures
containing multiple named or unnamed fields; in this case the field you’d
like to wrap should be marked with #[wrap] attribute; otherwise the first
field is assumed to be the wrapped one.
NB: You have to use derive(From) in order foe Wrapper to work properly.
Also, in case of multiple fields, each non-wrapped field type must implement
Default trait.
Supports automatic implementation of the following traits:
amplify::WrapperAsRefcore::borrow::BorrowYou may skipAsRefandBorrowimplementations with#[wrapper(NoRefs)].
You can implement additional derives, it they are implemented for the
wrapped type, using #[wrapper()] proc macro:
- Reference access to the inner type:
Dereffor implementingcore::ops::DerefAsSlicefor implementingAsRef<[u8]>BorrowSlicefor implementingcore::borrow::Borrow<[Self::Inner]>
- Formatting:
FromStrfor implementingcore::str::FromStrDebugfor implementingcore::fmt::DebugDisplayfor implementingcore::fmt::DisplayFromHexfor implementing [amplify::hex::FromHex]LowerHexfor implementingcore::fmt::LowerHexUpperHexfor implementingcore::fmt::UpperHexLowerExpfor implementingcore::fmt::LowerExpUpperExpfor implementingcore::fmt::UpperExpOctalfor implementingcore::fmt::Octal
- Indexed access to the inner type:
Indexfor implementingcore::ops::Index<usize>IndexRangefor implementingcore::ops::Index<core::ops::Range<usize>>IndexTofor implementingcore::ops::Index<core::ops::RangeTo<usize>>IndexFromfor implementingcore::ops::Index<core::ops::RangeFrom<usize>>IndexInclusivefor implementingcore::ops::Index<core::ops::RangeInclusive<usize>>IndexToInclusivefor implementingcore::ops::Index<core::ops::RangeToInclusive<usize>>IndexFullfor implementingcore::ops::Index<core::ops::RangeFrom<usize>>
- Arithmetic operations:
Negfor implementingcore::ops::NegAddfor implementingcore::ops::AddSubfor implementingcore::ops::SubMulfor implementingcore::ops::MulDivfor implementingcore::ops::DivRemfor implementingcore::ops::Rem
- Boolean and bit-wise operations:
Notfor implementingcore::ops::NotBitAndfor implementingcore::ops::BitAndBitOrfor implementingcore::ops::BitOrBitXorfor implementingcore::ops::BitXorShlfor implementingcore::ops::ShlShrfor implementingcore::ops::Shr
There are shortcuts for derivations:
#[wrapper(Hex)]will derive bothLowerHex,UpperHexandFromHex;#[wrapper(Exp)]will derive bothLowerExpandUpperExp;#[wrapper(NumberFmt)]will derive all number formatting traits (LowerHex,UpperHex,LowerExp,UpperExp,Octal);#[wrapper(RangeOps)]will derive all index traits working with ranges (IndexRange,IndexTo,IndexFrom,IndexInclusive,IndexToInclusive,IndexFull);#[wrapper(MathOps)]will derive all arithmetic operations (Neg,Add,Sub,Mul,Div,Rem);#[wrapper(BoolOps)]will derive all boolean operations (Not,BitAnd,BitOr,BitXor);#[wrapper(BitOps)]will derive all boolean operations and bit shifts (Not,BitAnd,BitOr,BitXor,Shl,Shr).
Other traits, such as PartialEq, Eq, PartialOrd, Ord,
Hash can be implemented using standard #[derive] attribute in the
same manner as Default, Debug and From
§Example
Simple wrapper:
use amplify::Wrapper;
#[derive(Wrapper, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, From, Debug, Display)]
#[display(inner)]
#[wrapper(LowerHex, UpperHex, Octal)]
#[wrapper(MathOps, BitOps)]
struct Int64(i64);More complex wrapper with multiple unnamed fields:
use std::marker::PhantomData;
use amplify::Wrapper;
#[derive(Clone, Wrapper, Default, From)]
#[wrapper(Debug)]
struct Wrapped<T, U>(
#[wrap]
#[from]
HashMap<usize, Vec<U>>,
PhantomData<T>,
)
where U: Sized + Clone + Debug;
let w = Wrapped::<(), u8>::default();
assert_eq!(w.into_inner(), HashMap::<usize, Vec<u8>>::default());Wrappers for indexable types
use amplify::Wrapper;
#[derive(Wrapper, From)]
#[wrapper(Index, RangeOps)]
struct VecNewtype(Vec<u8>);