[][src]Trait safecast::TryCastFrom

pub trait TryCastFrom<T>: Sized {
    pub fn can_cast_from(value: &T) -> bool;
pub fn opt_cast_from(value: T) -> Option<Self>; pub fn try_cast_from<Err, OnErr: FnOnce(&T) -> Err>(
        value: T,
        on_err: OnErr
    ) -> Result<Self, Err> { ... } }

Trait for defining a cast operation when the source type cannot always be cast to the destination type. Defines a can_cast_from method which borrows the source value, allowing for pattern matching without moving the value. When can_cast_from returns true, calling opt_cast_from must return Some(...), otherwise try_cast_from 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

pub fn can_cast_from(value: &T) -> bool[src]

Test if value can be cast into Self.

pub fn opt_cast_from(value: T) -> Option<Self>[src]

Returns Some(Self) if the source value can be cast into Self, otherwise None.

Loading content...

Provided methods

pub fn try_cast_from<Err, OnErr: FnOnce(&T) -> Err>(
    value: T,
    on_err: OnErr
) -> Result<Self, Err>
[src]

Returns Ok(Self) if the source value can be cast into Self, otherwise calls on_err.

Loading content...

Implementors

impl<F, T: CastFrom<F>> TryCastFrom<F> for T[src]

Loading content...