#dyn #supertraits #value #mut #user #along #helping

upcast

Simple trait for helping along upcasting of dyn supertraits. pub trait A {} pub trait B: A + Upcast<dyn A> {} // Put this in your library impl<'a, T: A + 'a> UpcastFrom<T> for dyn A + 'a { fn up_from(value: &T) -> &(dyn A + 'a) { value } fn up_from_mut(value: &mut T) -> &mut (dyn A + 'a) { value } } // Now your users can do an upcast if needed, or you can within implementations fn do_cast(b: &dyn B) -> &dyn A { b.up() }

1 unstable release

0.1.0 Aug 9, 2019

#8 in #along

Download history 21/week @ 2024-03-30 2/week @ 2024-04-06

51 downloads per month

MIT license

4KB

Simple trait for helping along upcasting of dyn supertraits.

pub trait A {}
pub trait B: A + Upcast<dyn A> {}

// Put this in your library
impl<'a, T: A + 'a> UpcastFrom<T> for dyn A + 'a {
    fn up_from(value: &T) -> &(dyn A + 'a) { value }
    fn up_from_mut(value: &mut T) -> &mut (dyn A + 'a) { value }
}

// Now your users can do an upcast if needed, or you can within implementations
fn do_cast(b: &dyn B) -> &dyn A {
    b.up()
}

No runtime deps