Hey Rustaceans! Got an easy question? Ask here (21/2020)!

I'd like to know the proper way to do trait inheritance with trait objects. Currently, if I want a trait which is like Any but allows for cloning, I either have to manually add all the methods I want from Any to the trait, or end up with something like this:

pub trait CloneBoxAny: std::fmt::Debug + 'static {
    fn clone_box_any(self: &Self) -> Box<dyn CloneBoxAny>;
    fn as_any_mut(&mut self) -> &mut dyn Any;
    fn as_any(&self) -> &dyn Any;
}

impl<T: Clone + std::fmt::Debug + 'static> CloneBoxAny for T {
    fn clone_box_any(self: &Self) -> Box<dyn CloneBoxAny> {
        Box::new(self.clone())
    }
    fn as_any(&self) -> &dyn Any {
        self as &dyn Any
    }
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self as &mut dyn Any
    }
}

then downcasting looks like:

let our_box: Box<dyn CloneBoxAny> = ...
let downcast = Any::downcast_ref::<bool>((*our_box).as_any());

but this is all incredibly messy, and so there's got to be a better way. Could someone please recommend one?

/r/rust Thread