Beginner(ish) generics question - impl needs "stricter requirement than trait"
You need to define the trait in a way that lets implementations have bounds on what sort of value they accept. One way would be to make the trait generic over the value type:
trait SettingsDatabase<V> {
fn set(&mut self, key: &str, value: V);
}
impl<V: UDValue> SettingsDatabase<V> for AppleSettings {...}
However, then you can’t practically use `dyn SettingsDatabase`, if that was something you were hoping for; it also means that any function generic over `T: SettingsDatabase` needs to specify _which types it is planning to write_ , which is probably undesirable in this case. Another is to require each implementation to specify a specific `enum` or other such type for all values:
trait SettingsDatabase {
type Value;
fn set(&mut self, key: &str, value: Self::Value);
// Convenience for conversion
fn set_from<V: Into<Self::Value>>(&mut self, key: &str, value: V)
where
Self: Sized // keeps the trait dyn compatible
{
self.set(key, value.into());
}
}
impl SettingsDatabase for AppleSettings {
type Value = NSObject; // whatever your type for all possible values is
fn set(&mut self, key: &str, value: Self::Value) {...}
}
This way, the set of usable value types is defined by the `From`/`Into` implementations present.