Greetings!
I'm trying to implement a seedable and serializable random number generator for my game. The seed type should be kept generic if possible, but when trying to define trait bounds for it, the compiler disagrees.
Note: This is running nightly-2019-07-19
#[derive(Clone, Debug)]
pub struct GameRng<T> {
inner: T,
}
impl<T: Rng + 'static> GameRng<T> {
pub fn new(inner: T) -> GameRng<T> {
GameRng { inner }
}
}
impl<T: SeedableRng + Rng + 'static> SeedableRng for GameRng<T> {
type Seed = dyn Default + Sized + AsMut<[u8]>;
fn from_seed(seed: S) -> GameRng<T> {
GameRng::new(SeedableRng::from_seed(seed))
}
}
The above code yields the following error message:
error[E0225]: only auto traits can be used as additional traits in a trait object
--> src/util/game_rng.rs:44:31
|
44 | type Seed = dyn Default + Sized + AsMut<[u8]>;
| ------- ^^^^^
| | |
| | additional non-auto trait
| | trait alias used in trait object type (additional use)
| first non-auto trait
| trait alias used in trait object type (first use)
I understand one possible workaround is to define a custom seed type that implements Default and AsMut, but any rng that we substitute T
with should have an associated seed type already. Is there a way to refer to that?