Skip to main content

impl_deref_mut

Macro impl_deref_mut 

Source
macro_rules! impl_deref_mut {
    (<$($generic:ident),+> in $($rest:tt)+) => { ... };
    (<const $($rest:tt)+) => { ... };
    (<$generic:ident, $($rest:tt)+) => { ... };
    (@impl [$($generic:tt)+] $this:ty) => { ... };
    (@impl [$($generic:tt)+] $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 and const 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 type parameter and const parameter:

struct Array<T, const N: usize>([T; N]);
impl_more::impl_deref!(<T, const N: usize> in Array<T, N> => [T; N]);
impl_more::impl_deref_mut!(<T, const N: usize> in Array<T, N>);

let mut array = Array([1, 2, 3]);
array[0] = 4;

assert_eq!(*array, [4, 2, 3]);

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!");