Derive Macro deref_derive::Deref

source ·
#[derive(Deref)]
{
    // Attributes available to this derive:
    #[deref]
}
Expand description

Used to derive Deref for a struct.

Example

If have a struct with only one field, you can derive Deref for it.

#[derive(Default, Deref)]
struct Foo {
    field: String,
}

assert_eq!(Foo::default().len(), 0);

If you have a struct with multiple fields, you will have to use the deref attribute.

#[derive(Default, Deref)]
struct Foo {
   #[deref]
   field: u32,
   other_field: String,
}

assert_eq!(*Foo::default(), 0);

Tuple structs are also supported.

#[derive(Default, Deref, DerefMut)]
struct Foo(u32, #[deref] String);

let mut foo = Foo::default();
*foo = "bar".to_string();
foo.push('!');

assert_eq!(*foo, "bar!");