pub struct TypeMatch<'a> { /* private fields */ }
Expand description
A helper object that provides a convenient way to match on the Cell’s type.
This object is created by the Cell::type_match or Provider::type_match functions and helps you implement different type casting logic based on the Cell’s data type. It is intended for use in manual implementations of the Downcast, Upcast, ops traits, and other scenarios where you need to handle Cell data based on the Cell type.
The TypeMatch::is and TypeMatch::belongs_to matching functions return true if the Cell’s data corresponds to a particular Script type. Through these functions, you enumerate all possible Cell data types that your implementation supports. Whenever you encounter a supported type (the matching function returns true), you handle the Cell accordingly and return a meaningful successful result.
If no matching cases are found, you fall back to the RuntimeError that TypeMatch generates for you by calling the TypeMatch::mismatch function. This function returns a descriptive error indicating that the provided Cell’s type does not match any of the expected types enumerated by the matching functions.
impl<'a> Downcast<'a> for Foo {
fn downcast(origin: Origin, provider: Provider<'a>) -> RuntimeResult<Self> {
let cell = provider.to_owned();
let mut type_match = cell.type_match();
// The Cell type is "Foo". Handling this case.
if type_match.is::<Foo>() {
return cell.take::<Foo>(origin);
}
// The Cell type is "bool". Handling this case.
if type_match.is::<bool>() {
let inner = cell.take::<bool>(origin)?;
return Ok(Foo(inner));
}
// Otherwise, returning an error indicating that the Cell type
// should be either "Foo" or "bool".
Err(type_match.mismatch(origin))
}
fn hint() -> TypeHint {
Foo::type_meta().into()
}
}
Implementations§
Source§impl<'a> TypeMatch<'a>
impl<'a> TypeMatch<'a>
Sourcepub fn is<T: ScriptType + ?Sized>(&mut self) -> bool
pub fn is<T: ScriptType + ?Sized>(&mut self) -> bool
Checks if the Cell’s type is exactly of type T
.
If the Cell’s type is not T
, it returns false and remembers that T
is one of the expected types.
Sourcepub fn belongs_to<T: ScriptType + ?Sized>(&mut self) -> bool
pub fn belongs_to<T: ScriptType + ?Sized>(&mut self) -> bool
Checks if the Cell’s type and the type T
belong to the
same type family.
If the Cell’s type does not belong to T
’s family, it returns false
and remembers that all types from the T
type family are the expected
types.
Sourcepub fn cell(&self) -> &'a Cell
pub fn cell(&self) -> &'a Cell
Returns a reference to the Cell that the TypeMatch object is matching on.
Sourcepub fn mismatch(self, origin: Origin) -> RuntimeError
pub fn mismatch(self, origin: Origin) -> RuntimeError
Creates a RuntimeError indicating that the Cell’s type does not match any of the expected types.
This function should be called as the last statement after all matching cases (is and belongs_to functions) have been checked, and all of them return false.
The origin
parameter specifies the Rust or Script source code range
where the Cell was supposed to be accessed.