[][src]Trait intertrait::CastTo

pub trait CastTo {
    fn ref_to<T: ?Sized + 'static>(&self) -> Option<&T>;
fn mut_to<T: ?Sized + 'static>(&mut self) -> Option<&mut T>;
fn box_to<T: ?Sized + 'static>(self: Box<Self>) -> Option<Box<T>>;
fn impls<T: ?Sized + 'static>(&self) -> bool; }

A trait that is blanket-implemented for traits extending Any to allow for casting to another trait.

Examples

Casting an immutable reference

impl Source for Data {}
let data = Data;
let source: &dyn Source = &data;
let greet = source.ref_to::<dyn Greet>();
greet.unwrap().greet();

Casting a mutable reference.

impl Source for Data {}
let mut data = Data;
let source: &mut dyn Source = &mut data;
let greet = source.mut_to::<dyn Greet>();
greet.unwrap().greet();

Casting a Box.

impl Source for Data {}
let data = Box::new(Data);
let source: Box<dyn Source> = data;
let greet = source.box_to::<dyn Greet>();
greet.unwrap().greet();

Testing if a cast is possible

impl Source for Data {}
let data = Data;
let source: &dyn Source = &data;
assert!(source.impls::<dyn Greet>());
assert!(!source.impls::<dyn std::fmt::Debug>());

Required methods

fn ref_to<T: ?Sized + 'static>(&self) -> Option<&T>

Casts a reference to this trait into that of type T.

fn mut_to<T: ?Sized + 'static>(&mut self) -> Option<&mut T>

Casts a mutable reference to this trait into that of type T.

fn box_to<T: ?Sized + 'static>(self: Box<Self>) -> Option<Box<T>>

Casts a box to this trait into that of type T.

fn impls<T: ?Sized + 'static>(&self) -> bool

Tests if this trait object can be cast into T.

Loading content...

Implementors

impl<S: ?Sized + CastFrom> CastTo for S[src]

A blanket implementation of CastTo for traits extending CastFrom.

Loading content...