Assume there's a trait called StringableValue
; its purpose is to be implemented for types that hold values that also have string representations:
pub trait StringableValue {
type Value;
fn set(&mut self, sval: &str) -> Result<(), Error>;
fn val(&self) -> &Self::Value;
fn sval(&self) -> &str;
}
Now let's say that one would want Deref
[*] to be implemented for all StringableValue
implementors and it should return a reference to StringableValue::Value.
A requirement can be expressed:
pub trait StringableValue : Deref {
// ...
}
However, is it possible to actually express the implementation a single time, using generics?
Something along the line of:
impl<T> Deref for T
where
T: StringableValue
{
type Target = <T as StringableValue>::Value;
fn deref(&self) -> &<T as StringableValue>::Value {
self.val()
}
}
[*] Don't worry, I know it's a bad idea to implement Deref
on things that aren't smart pointers. This is just about pure curiosity, and not anything that will be used in practice.