Macro DerefMut
macro_rules! DerefMut { ... }
What #[derive(DerefMut)] generates
Deriving Deref only works for a single field of a struct.
Furthermore it requires that the type also implements Deref, so usually
Deref should also be derived.
The resulting implementation of Deref will allow you to mutably dereference
the struct its member directly.
- Dereferencing to the field, i.e. like if your type was a reference type.
- Doing a dereference on the field, for when the field itself is a reference
type like
&mutandBox.
With #[deref_mut] or #[deref_mut(ignore)] it's possible to indicate the
field that you want to derive DerefMut for.
Example usage
# use ;
#
;
// You can specify the field you want to derive DerefMut for
let mut num = Num;
let mut boxed = MyBoxedInt;
let mut cool_vec = CoolVec;
*num += 123;
assert_eq!;
*boxed += 1000;
assert_eq!;
cool_vec.push;
assert_eq!;
Structs
When deriving a non-forwarded Deref for a struct:
# use ;
#
Code like this will be generated:
# use Deref;
#
#
When deriving DerefMut for a tuple struct with one field:
# use ;
#
;
When deriving a forwarded DerefMut for a struct:
# use Deref;
# ;
#
Enums
Deriving DerefMut is not supported for enums.