Macro impl_more::impl_deref_and_mut

source ·
macro_rules! impl_deref_and_mut {
    (<$($generic:ident),+> in $this:ty => $target:ty) => { ... };
    (<$($generic:ident),+> in $this:ty => $field:ident : $target:ty) => { ... };
    ($this:ty => $target:ty) => { ... };
    ($this:ty => $field:ident : $target:ty) => { ... };
}
Expand description

Implements Deref and DerefMut by forwarding through an inner field’s implementation.

Use the ref <type> form for deref-ing to types with lifetimes like &str. For newtype structs, only the struct name and deref target type is necessary.

This macro has the same type parameter support and format as impl_deref.

Also see forward_deref_and_mut.

§Examples

struct MyNewTypeStruct(String);
impl_more::impl_deref_and_mut!(MyNewTypeStruct => String);

let foo = MyNewTypeStruct("one".to_owned());
let foo_ref: &String = &foo;

// Unlike `forward_deref_and_mut`, this macro will not forward the deref implementation
// through the named type. Even so, in some cases Rust will be able to support these cases.

let foo_ref: &str = &foo;

fn accepts_string_slice(_: &str) {}
accepts_string_slice(&foo);