`error[E0038]: the trait `Player` cannot be made into an object
--> src/mankala.rs:7:15
|
7 | players: [dyn Player; 2],
| ^^^^^^^^^^ `Player` cannot be made into an object
|
note: for a trait to be "dyn-compatible" 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/mankala.rs:3:40
|
3 | trait Player : Fn(&BoardState) -> u8 + Sized {}
| ------ ^^^^^ ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...`
I am trying to create a game with 2 players represented as functions each get the board every turn, but they can both be simple functions or closures, so as I understand they need to be dynamic. I am a bit of beginner, so any kind of advice will be helpful. Thanks in advance!
I assume you added Sized
to your trait because you first had dyn Player
and the compiler complained that it is not Sized
. Trait objects (dyn Trait
s) are not and cannot be Sized
since they represent any possible implementation. And adding a Sized
constraint means you cannot create dyn Trait
at all.
Because of this "unsized"-ness, trait objects can really only be held behind some kind of indirection. In this case you likely want to wrap your players in a Box
:
players: [Box<dyn Player>; 2],