MetaTuple

Trait MetaTuple 

Source
pub unsafe trait MetaTuple: MetaAny {
    // Required methods
    fn get<T: 'static>(&self) -> Option<&T>;
    fn get_mut<T: 'static>(&mut self) -> Option<&mut T>;
    fn get_mut_ptr<T: 'static>(&self) -> Option<*mut T>;

    // Provided methods
    fn join<T: 'static>(self, other: T) -> Join<Self, MetaItem<T>>
       where Self: Sized { ... }
    fn join_ref<T: 'static>(self, other: &T) -> Join<Self, &MetaItem<T>>
       where Self: Sized { ... }
    fn join_mut<T: 'static>(self, other: &mut T) -> Join<Self, &mut MetaItem<T>>
       where Self: Sized { ... }
    fn join_tuple<T: MetaTuple>(self, other: T) -> Join<Self, T>
       where Self: Sized { ... }
    fn join_dyn_ref(self, other: &dyn MetaAny) -> Join<Self, &DynMetaTuple>
       where Self: Sized { ... }
    fn join_dyn_mut(
        self,
        other: &mut dyn MetaAny,
    ) -> Join<Self, &mut DynMetaTuple>
       where Self: Sized { ... }
    fn query_ref<T: MetaQuery>(&self) -> Option<T::Output<'_>> { ... }
    fn query_mut<T: MetaQuery>(&mut self) -> Option<T::Output<'_>> { ... }
}
Expand description

A statically typed opaque tuple that can contain any type.

This is a zero cost abstraction in most cases. To create, see macro meta_tuple!.

§Use Case

This trait is generally used for complicated input and output abstractions, for example

pub trait CardComponent {
    fn play(&self, input: &impl MetaTuple) -> impl MetaTuple;
}

impl CardComponent for Attack {
    fn play(&self, input: &impl MetaTuple) -> impl MetaTuple {
        let attacker = input.get::<Attacker>().unwrap();
        let defender = input.get::<Defender>().unwrap();
        let damage_dealt = self.calculate_damage(attacker, defender);
        input.join(DamageDealt(damage_dealt))
    }
}

pub trait CardComponent {
    pub fn play(&self, input: &impl MetaTuple) -> impl MetaTuple;
}

§Semantics

For functions like get, we look for the first correct item, duplicated items will not be used. &impl MetaTuple and &mut impl MetaTuple both implement MetaTuple.

§Warning

Due to our semantics, for a tuple like (&A, A), get returns the first value while get_mut returns the second value, since &A cannot return a &mut A.

§Dyn Compatibility

For a boxed dynamic version, see super trait MetaAny.

§Safety

This trait cannot return overlapping references for different types.

For example

struct A{
    b: B,
    c: C,
}

The implementation can return A, or (B, C) but not both.

Required Methods§

Source

fn get<T: 'static>(&self) -> Option<&T>

Obtain an item, if exists.

Source

fn get_mut<T: 'static>(&mut self) -> Option<&mut T>

Obtain a mutable item, if exists.

Source

fn get_mut_ptr<T: 'static>(&self) -> Option<*mut T>

Obtain a mutable item as pointer, if exists.

Provided Methods§

Source

fn join<T: 'static>(self, other: T) -> Join<Self, MetaItem<T>>
where Self: Sized,

Join with another concrete value.

Source

fn join_ref<T: 'static>(self, other: &T) -> Join<Self, &MetaItem<T>>
where Self: Sized,

Join with a reference to a concrete value.

If querying for a mutable reference, will return None.

Source

fn join_mut<T: 'static>(self, other: &mut T) -> Join<Self, &mut MetaItem<T>>
where Self: Sized,

Join with a mutable reference to a concrete value.

Source

fn join_tuple<T: MetaTuple>(self, other: T) -> Join<Self, T>
where Self: Sized,

Join with another MetaTuple.

Source

fn join_dyn_ref(self, other: &dyn MetaAny) -> Join<Self, &DynMetaTuple>
where Self: Sized,

Join with a &dyn MetaAny.

Source

fn join_dyn_mut(self, other: &mut dyn MetaAny) -> Join<Self, &mut DynMetaTuple>
where Self: Sized,

Join with a &mut dyn MetaAny.

Source

fn query_ref<T: MetaQuery>(&self) -> Option<T::Output<'_>>

Try obtain multiple values from the MetaTuple.

Source

fn query_mut<T: MetaQuery>(&mut self) -> Option<T::Output<'_>>

Try obtain multiple values from the MetaTuple.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl MetaTuple for ()

Source§

fn get<T: 'static>(&self) -> Option<&T>

Source§

fn get_mut<T: 'static>(&mut self) -> Option<&mut T>

Source§

fn get_mut_ptr<T: 'static>(&self) -> Option<*mut T>

Source§

impl<T: 'static> MetaTuple for Option<T>

Source§

fn get<U: 'static>(&self) -> Option<&U>

Source§

fn get_mut<U: 'static>(&mut self) -> Option<&mut U>

Source§

fn get_mut_ptr<U: 'static>(&self) -> Option<*mut U>

Source§

impl<T: MetaTuple + ?Sized> MetaTuple for &T

Source§

fn get<U: 'static>(&self) -> Option<&U>

Source§

fn get_mut<U: 'static>(&mut self) -> Option<&mut U>

Source§

fn get_mut_ptr<U: 'static>(&self) -> Option<*mut U>

Source§

impl<T: MetaTuple + ?Sized> MetaTuple for &mut T

Source§

fn get<U: 'static>(&self) -> Option<&U>

Source§

fn get_mut<U: 'static>(&mut self) -> Option<&mut U>

Source§

fn get_mut_ptr<U: 'static>(&self) -> Option<*mut U>

Implementors§