How do you convert a Box<dyn Trait> to a Rc<dyn Trait>?

Viewed 361

I have a function which receives a Box<dyn Trait> and needs to convert that into an Rc<dyn Trait> to share read-only ownership within the thread.

With a Box<T> of some T: Sized, we can do Rc::new(*my_box), but unfortunately that doesn't work for unsized trait objects.

Here's an oversimplified example which hopefully clarifies the problem:

use std::rc::Rc;

pub trait Trait {}

pub struct Foo {}
impl Trait for Foo {}

fn main() {
    let trait_box: Box<dyn Trait> = Box::new(Foo {});
    let trait_rc: Rc<dyn Trait> = Rc::new(*trait_box); // -> Error
}

Playground link

I saw some things here and there about exposing the internal RcBox to support moving between Box and Rc, but AFAIK it's not usable today.

Is there a workaround for this?

Or if this type of conversion isn't possible, what is the recommended method for storing a trait object which can be mutated up to a point, and then after that point be shared immutably with the rest of the program?

Using a Rc<RefCell<dyn Trait>> seems like overkill when I know I just have a single owner up to that point...

1 Answers
Related