Trait safecast::TryCastInto

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

    // Provided method
    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§

source

fn can_cast_into(&self) -> bool

Test if self can be cast into T.

source

fn opt_cast_into(self) -> Option<T>

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

Provided Methods§

source

fn try_cast_into<Err, OnErr: FnOnce(&Self) -> Err>( self, on_err: OnErr ) -> Result<T, Err>

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

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<F, T: TryCastFrom<F>> TryCastInto<T> for F