Derive Macro bevy::prelude::Deref

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

Implements Deref for structs. This is especially useful when utilizing the newtype pattern.

For single-field structs, the implementation automatically uses that field. For multi-field structs, you must specify which field to use with the #[deref] attribute.

If you need DerefMut as well, consider using the other derive macro alongside this one.

§Example

§Tuple Structs

Using a single-field struct:

use bevy_derive::Deref;

#[derive(Deref)]
struct MyNewtype(String);

let foo = MyNewtype(String::from("Hello"));
assert_eq!("Hello", *foo);

Using a multi-field struct:

use bevy_derive::Deref;

#[derive(Deref)]
struct MyStruct<T>(#[deref] String, PhantomData<T>);

let foo = MyStruct(String::from("Hello"), PhantomData::<usize>);
assert_eq!("Hello", *foo);

§Named Structs

Using a single-field struct:

use bevy_derive::{Deref, DerefMut};

#[derive(Deref, DerefMut)]
struct MyStruct {
  value: String,
}

let foo = MyStruct {
  value: String::from("Hello")
};
assert_eq!("Hello", *foo);

Using a multi-field struct:

use bevy_derive::{Deref, DerefMut};

#[derive(Deref, DerefMut)]
struct MyStruct<T> {
  #[deref]
  value: String,
  _phantom: PhantomData<T>,
}

let foo = MyStruct {
  value:String::from("Hello"),
  _phantom:PhantomData::<usize>
};
assert_eq!("Hello", *foo);