It would be pretty convenient to use Deref to generate & TraitType from a generic container, rather than calling instance.as_ref (). i.e:
(*my_container).do_thing();
vs.
my_container.as_ref().do_thing();
For this, I tried to implement Deref in a container type, but I get this error:
<anon>:9:28: 9:29 error: expected a reference to a trait [E0172] <anon>:9 impl<T> Deref for HasTrait<T + Send> {
From:
use std::ops::Deref; trait Foo {} struct HasTrait<T> { data:Box<T> } impl<T> Deref for HasTrait<T + Send> { type Target = T; fn deref<'a>(&'a self) -> &'a T { return self.as_ref(); } } struct IsFoo; unsafe impl Send for IsFoo {} impl Foo for IsFoo {} fn main() { let is_foo = IsFoo; let foo:Box<Foo> = box is_foo as Box<Foo>; let has_foo = HasTrait { data: foo }; let foo_ref:&Foo = *has_foo; }
I tried to use? Sized to increase T borders to allow traits, but it didn’t help?
What is the right way to do this?
source share