Macro impl_more::impl_deref_mut
source · macro_rules! impl_deref_mut { (<$($generic:ident),+> in $this:ty) => { ... }; (<$($generic:ident),+> in $this:ty => $field:ident) => { ... }; ($this:ty) => { ... }; ($this:ty => $field:ident) => { ... }; }
Expand description
Implement DerefMut
for a struct.
The first argument is that of the struct to create the impl for and this type must also
implement Deref
. The second argument is required for non-newtype structs and is the field
to deref to.
This macro has the same type parameter support and format as impl_deref
.
Also see impl_deref
, impl_deref_and_mut
, and forward_deref_and_mut
.
Examples
With a newtype struct:
use impl_more::{impl_deref, impl_deref_mut};
struct Foo(String);
impl_deref!(Foo => String);
impl_deref_mut!(Foo);
let mut foo = Foo("bar".to_owned());
foo.push('!');
assert_eq!(*foo, "bar!");
With a named field struct and type parameter:
struct Foo<T> { msg: T };
impl_more::impl_deref!(<T> in Foo<T> => msg: T);
impl_more::impl_deref_mut!(<T> in Foo<T> => msg);
let mut foo = Foo { msg: "bar".to_owned() };
foo.push('!');
assert_eq!(*foo, "bar!");