pub trait TryCastInto<T>: Sized {
    fn can_cast_into(&self) -> bool;
    fn opt_cast_into(self) -> Option<T>;

    fn try_cast_into<Err, OnErr: FnOnce(&Self) -> Err>(
        self,
        on_err: OnErr
    ) -> Result<T, Err> { ... } }
Expand description

Trait for defining a cast operation when the destination type cannot always be cast from the source type. Defines a can_cast_into method which borrows self, allowing for pattern matching without moving self. If can_cast_into returns true, then calling opt_cast_into must return Some(...), otherwise try_cast_into may panic.

Analogous to TryFrom. The inverse of TryCastInto. Prefer implementing TryCastFrom over TryCastInto because implementing TryCastFrom automatically provides an implementation of TryCastInto.

Required Methods

Test if self can be cast into T.

Returns Some(T) if self can be cast into T, otherwise None.

Provided Methods

Returns Ok(T) if self can be cast into T, otherwise calls on_err.

Implementors