Search code examples
memory-managementrusttrait-objects

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


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...


Solution

  • Rc<T> implements impl<T> From<Box<T, Global>> so you can just use into:

    let trait_rc: Rc<dyn Trait> = trait_box.into();
    

    Permalink to the playground