Reputation: 581
Here's the error message:
error[E0038]: the trait `room::RoomInterface` cannot be made into an object
--> src\permanent\registry\mod.rs:12:33
|
12 | pub fn load(id : String) -> Result<Box<dyn RoomInterface>, String>{
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `room::RoomInterface` 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>
--> src\permanent\rooms\mod.rs:36:31
|
36 | pub trait RoomInterface : Copy + Sized {
| ------------- ^^^^ ^^^^^ ...because it requires `Self: Sized`
| | |
| | ...because it requires `Self: Sized`
| this trait cannot be made into an object...
For more information about this error, try `rustc --explain E0038`.
It says that to use the trait as an object, it requires Sized. Except that Sized is right there, in the declaration of the trait! It literally points to the word "Sized" and tells me I need to have "Sized". What's going on?
Upvotes: 7
Views: 5277
Reputation: 1116
The problem is opposite to what you think: The problem is that you require Self: Sized
but only traits which do not require Self: Sized
can be made into an object.
As the error message states you must remove both bounds for Copy
and Sized
for RoomInterface
:
pub trait RoomInterface {
The article linked in the error message is a great resource, I recommend reading it: https://doc.rust-lang.org/reference/items/traits.html#object-safety
Also this discussion might be interesting: https://users.rust-lang.org/t/trait-objects-and-the-sized-trait/14410/2
Upvotes: 7