I have a trait Cell
:
pub trait Cell: Debug + Clone + Ord {}
But when I try to create a vector of them like this:
pub struct Tuple {
pub cells: Vec<Box<dyn Cell>>,
}
The compiler gave me the following error:
error[E0038]: the trait `storage::tuple::cell::Cell` cannot be made into an object
--> src/storage/tuple/tuple.rs:20:24
|
20 | pub cells: Vec<Box<dyn Cell>>,
| ^^^^^^^^ `storage::tuple::cell::Cell` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
So I was baffled after researching for a while:
- What makes a trait "object safe", and what makes it not?
- Can we inherit an "unsized" trait and make our child trait "size/object safe" at the same time?
- What's the usage of a "not object safe" trait?
- Why we cannot make a
Box<dyn trait>
when the trait is "not object safe"? Given the fact that we are manipulating objects on heap, why does the compiler still not let me go?
I tried:
- Remove the "Ord" or "Clone" trait, but the trait doesn't make sense any more.
- Add "+ Sized" to the trait, it doesn't fix the compile error.