pub type AsDerefCopiedOfClonedOrDefault<T> = Copied<AsDeref<OrDefault<Cloned<T>>>>;
Expand description

First either clones component T or gets the default value, then dereferences this value and copies it.

This is primarily useful over AsDerefCopiedOrDefault when default for the component is different than the default for the dereferenced type.

§Example

#[derive(Component, Deref, Clone)]
struct Temperature(f32);

// Notably the default for Temperature is different than the default for the
// dereferenced value. Using this type, if the component is not present on
// the entity, the query will return 20.0, rather than 0.0.
impl Default for Temperature {
    fn default() -> Self {
        Self(20.0)
    }
}

fn example(query: Query<AsDerefCopiedOfClonedOrDefault<Temperature>>) {
    let _: f32 = query.get_single().unwrap();
}

§Counter example: Outer type must implement Default, Deref AND Clone

#[derive(Component, Deref)]
struct Temperature(f32);

impl Default for Temperature {
    fn default() -> Self {
        Self(20.0)
    }
}

fn bad_example(query: Query<AsDerefCopiedOfClonedOrDefault<Temperature>>) {
    let _: f32 = query.get_single().unwrap();
}

§Counter example: Dereferenced type must implement Copy

#[derive(Component, Deref)]
struct Temperatures(Vec<f32>);

impl Default for Temperatures {
    fn default() -> Self {
        Self(vec![20.0])
    }
}

fn bad_example(query: Query<AsDerefCopiedOfClonedOrDefault<Temperatures>>) {
    let _: Vec<f32> = query.get_single().unwrap();
}

Aliased Type§

struct AsDerefCopiedOfClonedOrDefault<T>(/* private fields */);