Here is trait
and struct
:
trait A {}
#[derive(Debug, Clone)]
struct B;
#[derive(Debug, Clone)]
struct C;
impl A for B {}
impl A for C {}
I use it as Box<trait object>
:
let b_box: Box<dyn A> = Box::new(B.clone());
For convient, I need to the instance of trait A
has method to_box
so that I can use:
let b_box = B.to_box();
//this b_box is the same as above one
I tried realize it like this, but that does not work:
trait ToBox {
type T;
fn to_box(&self) -> Box<Self::T>;
}
impl<M: A + Clone> ToBox for M {
type T = dyn A;
fn to_box(&self) -> Box<Self::T> {
let res: Box<Self::T> = Box::new((self).clone());
res
}
}
[E0277] Error: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
โญโ[command_52:1:1]
โ
2 โ type T = dyn A;
ยท โโโฌโโ
ยท โฐโโโโ doesn't have a size known at compile-time
ยท
ยท Note: required by this bound in `ToBox::T`
โโโโฏ
What am I doing wrong?