I want to apply a trait to an Option<T>
for all T: MyTrait
. I find this kind of hard to explain:
use std::borrow::Borrow;
struct Consumer {}
impl Consumer {
fn consume<T>(&self, _v: T) where T: MyTrait { }
}
trait MyTrait {
fn do_something<T>(consumer: &Consumer, value: T) where T: Borrow<Self>;
}
impl<U> MyTrait for Option<U> {
fn do_something<'a, T>(consumer: &Consumer, value: T)
where T: Borrow<Self>, &'a U: MyTrait {
if let Some(v) = value.borrow() {
consumer.consume(v);
} else {
}
}
}
This get's me a: the parameter type
U may not live long enough
. I tried several other solutions and received different error messages. I cannot wrap my head around this.
ps: I successfully implemented this pattern for &[T]
but I fail to transfer this to an Option