Macro Div
macro_rules! Div { ... }
What #[derive(Mul)] generates
NOTE:
Div,Rem,ShrandShlderives are fully equivalent to theMulderive described below.
In contrast with deriving Add, there are two ways to derive Mul: scalar and structural.
Scalar implementation
By default, deriving Mul is quite different from deriving Add. It is not used
to multiply two structs together. Instead, it will normally multiply a struct,
which can have multiple fields, with a single primitive type (e.g. a u64).
A new struct is then created with all the fields from the previous struct
multiplied by this other value.
A simple way of explaining the reasoning behind this difference between Add
and Mul deriving, is looking at arithmetic on meters. One meter can be added
to one meter, to get two meters. Also, one meter times two would be two meters,
but one meter times one meter would be one square meter. As this second case
clearly requires more knowledge about the meaning of the type in question
deriving for this is not implemented.
Structs
Deriving Mul for a struct with a single field multiplies its field with
anything Multiplicable, producing the resulting struct.
# use Mul;
#
;
This generates code equivalent to:
# use Mul;
#
# ;
#
#
#
The behaviour is slightly different for multiple fields, since the right hand
side of the multiplication now needs the Copy trait.
# use Mul;
#
;
This generates code equivalent to:
# use Mul;
#
# ;
#
#
#
Ignoring
Sometimes a struct needs to hold a field (most commonly PhantomData) that doesn't
participate in a scalar Mul implementation. Such field could be ignored using
the #[mul(skip)] attribute.
# use PhantomData;
# use Mul;
#
] );
Enums
Deriving scalar Mul implementation for enums is not (yet) supported.
Although it shouldn't be impossible no effort has been put into this yet.
Structural implementation
Specifying the #[mul(forward)] attribute generates a structural Mul
implementation with the same semantics as Add: Multiplying respective
fields together and producing a new value with these fields.
Structs
# use Mul;
#
;
This generates code equivalent to:
# use Mul;
#
# ;
#
#
#
The behaviour is similar with more or less fields.
Ignoring
Sometimes a struct needs to hold a field (most commonly PhantomData) that doesn't
participate in a structural Mul implementation. Such field could be ignored using
the #[mul(skip)] attribute.
# use PhantomData;
# use Mul;
#
] );
Enums
For enums each variant can be Multiplied structurally in a similar way to another
instance of the same variant. There's one big difference however: it returns
a Result<EnumType>, because an error is returned when two different variants are
Multiplied together.
# use Mul;
#
This generates code equivalent to:
# use Mul;
#
#
#
Also note the Unit variant that throws a derive_more::UnitError when Multiplying it
to itself.
Ignoring
Similarly to structs, enum fields could be ignored using the #[add(skip)] attribute.
# use Mul;
#
; // doesn't implement `Mul`
NOTE: Ignoring all the fields of a variant or ignoring the variant itself is not allowed (results in a compilation error).