Consider such code:
trait Foo {
fn foo(&self);
}
fn consume_func(b: Box<Box<Foo>>) {
unimplemented!();
}
fn produce_func() -> Box<Box<Foo + Send>> {
unimplemented!();
}
fn main() {
let b = produce_func();
consume_func(b);
}
It doesn't compile:
error[E0308]: mismatched types
--> src/main.rs:24:18
|
24 | consume_func(b);
| ^ expected trait `Foo`, found trait `Foo + std::marker::Send`
|
= note: expected type `std::boxed::Box<std::boxed::Box<Foo + 'static>>`
found type `std::boxed::Box<std::boxed::Box<Foo + std::marker::Send>>`
The double Box
is a way to give a C library a void *
pointer from Box<Trait>
. Because of fat pointers, I can not convert Box<Foo>
to void *
.
I can not change consume_func
, and I'd prefer to not use unsafe
or additional allocation.