What #[derive(Deref)] generates

Deriving Deref only works for structs with a single field, e.g. newtypes. The result is that you will deref it's member directly. So this is mostly useful for newtypes that contain a pointer type such as Box or Rc.

1 Example usage

#[derive(Deref)]
struct MyBoxedInt(Box<i32>);

#[derive(Deref)]
struct NumRef<'a> {
    num: &'a i32,
}

fn main() {
    let int = 123i32;
    let boxed = MyBoxedInt(Box::new(int));
    let num_ref = NumRef{num: &int};
    assert_eq!(123, *boxed);
    assert_eq!(123, *num_ref);
}

2 Tuple structs

When deriving Deref for a tuple struct with one field:

#[derive(Deref)]
struct MyBoxedInt(Box<i32>);

Code like this will be generated:

impl ::std::ops::Deref for MyBoxedInt {
    type Target = <Box<i32> as ::std::ops::Deref>::Target;
    #[inline]
    fn deref(&self) -> &Self::Target {
        <Box<i32> as ::std::ops::Deref>::deref(&self.0)
    }
}

3 Regular structs

When deriving Deref for a regular struct with one field:

#[derive(Deref)]
struct NumRef<'a> {
    num: &'a i32,
}

Code like this will be generated:

impl<'a> ::std::ops::Deref for NumRef<'a> {
    type Target = <&'a i32 as ::std::ops::Deref>::Target;
    #[inline]
    fn deref(&self) -> &Self::Target {
        <&'a i32 as ::std::ops::Deref>::deref(&self.num)
    }
}

4 Enums

Deriving Deref is not supported for enums.