pub trait MetaTuple: MetaBox {
// Required methods
fn get<T: 'static>(&self) -> Option<&T>;
fn get_mut<T: 'static>(&mut 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 { ... }
}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,
for a tuple like (&A, A), get returns the first value while get_mut returns the second value.
§Dyn Compatibility
For a boxed dynamic version, see super trait MetaBox.
Required Methods§
Provided Methods§
Sourcefn join<T: 'static>(self, other: T) -> Join<Self, MetaItem<T>>where
Self: Sized,
fn join<T: 'static>(self, other: T) -> Join<Self, MetaItem<T>>where
Self: Sized,
Join with another concrete value.
Sourcefn join_ref<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,
Join with a reference to a concrete value.
If querying for a mutable reference, will return None.
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.