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§
Sourcefn get_mut_ptr<T: 'static>(&self) -> Option<*mut T>
fn get_mut_ptr<T: 'static>(&self) -> Option<*mut T>
Obtain a mutable item as pointer, if exists.
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.
Sourcefn join_mut<T: 'static>(self, other: &mut T) -> Join<Self, &mut MetaItem<T>>where
Self: Sized,
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.
Sourcefn join_tuple<T: MetaTuple>(self, other: T) -> Join<Self, T>where
Self: Sized,
fn join_tuple<T: MetaTuple>(self, other: T) -> Join<Self, T>where
Self: Sized,
Join with another MetaTuple.
Sourcefn join_dyn_ref(self, other: &dyn MetaAny) -> Join<Self, &DynMetaTuple>where
Self: Sized,
fn join_dyn_ref(self, other: &dyn MetaAny) -> Join<Self, &DynMetaTuple>where
Self: Sized,
Join with a &dyn MetaAny.
Sourcefn join_dyn_mut(self, other: &mut dyn MetaAny) -> Join<Self, &mut DynMetaTuple>where
Self: Sized,
fn join_dyn_mut(self, other: &mut dyn MetaAny) -> Join<Self, &mut DynMetaTuple>where
Self: Sized,
Join with a &mut dyn MetaAny.
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.