#[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::Wrapper- [
AsRef] - [
core::borrow::Borrow] You 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 implementing [core::ops::Deref]AsSlicefor implementing [AsRef]<[u8]>BorrowSlicefor implementing [core::borrow::Borrow]<[Self::Inner]>
- Formatting:
FromStrfor implementing [core::str::FromStr]Debugfor implementing [core::fmt::Debug]Displayfor implementing [core::fmt::Display]FromHexfor implementing [amplify::hex::FromHex]LowerHexfor implementing [core::fmt::LowerHex]UpperHexfor implementing [core::fmt::UpperHex]LowerExpfor implementing [core::fmt::LowerExp]UpperExpfor implementing [core::fmt::UpperExp]Octalfor implementing [core::fmt::Octal]
- Indexed access to the inner type:
Indexfor implementing [core::ops::Index]<usize>IndexRangefor implementing [core::ops::Index]<[core::ops::Range]<usize>>IndexTofor implementing [core::ops::Index]<[core::ops::RangeTo]<usize>>IndexFromfor implementing [core::ops::Index]<[core::ops::RangeFrom]<usize>>IndexInclusivefor implementing [core::ops::Index]<[core::ops::RangeInclusive]<usize>>IndexToInclusivefor implementing [core::ops::Index]<[core::ops::RangeToInclusive]<usize>>IndexFullfor implementing [core::ops::Index]<[core::ops::RangeFrom]<usize>>
- Arithmetic operations:
Negfor implementing [core::ops::Neg]Addfor implementing [core::ops::Add]Subfor implementing [core::ops::Sub]Mulfor implementing [core::ops::Mul]Divfor implementing [core::ops::Div]Remfor implementing [core::ops::Rem]
- Boolean and bit-wise operations:
Notfor implementing [core::ops::Not]BitAndfor implementing [core::ops::BitAnd]BitOrfor implementing [core::ops::BitOr]BitXorfor implementing [core::ops::BitXor]Shlfor implementing [core::ops::Shl]Shrfor implementing [core::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>);