Consider such code:
trait Foo {
fn foo(&self);
}
fn consume_func(b: Box>) {
unimplemented!();
}
fn produce_func() -> Box<
Here's one way to do it: dereference b
("un-boxing" it to a Box<Foo + Send>
), then wrap it up immediately in another Box<T>
, allowing the compiler to infer the correct T
(in this case Box<Foo>
).
consume_func(Box::new(*b));
This works because Box<Foo + Send>
can be automatically coerced to Box<Foo>
, but Box<Box<Foo + Send>>
cannot be coerced to Box<Box<Foo>>
.
Although you've stated you cannot change consume_func
, others with similar issues can change it to accept a generic:
fn consume_func<F: Foo + ?Sized>(b: Box<Box<F>>) {
unimplemented!();
}