Struct TypeMatch

Source
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>

Source

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.

Source

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.

Source

pub fn cell(&self) -> &'a Cell

Returns a reference to the Cell that the TypeMatch object is matching on.

Source

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.

Auto Trait Implementations§

§

impl<'a> Freeze for TypeMatch<'a>

§

impl<'a> !RefUnwindSafe for TypeMatch<'a>

§

impl<'a> Send for TypeMatch<'a>

§

impl<'a> Sync for TypeMatch<'a>

§

impl<'a> Unpin for TypeMatch<'a>

§

impl<'a> !UnwindSafe for TypeMatch<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.