4

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

NanoWizard
  • 2,104
  • 1
  • 21
  • 34

1 Answers1

11

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

mcarton
  • 27,633
  • 5
  • 85
  • 95