你如何在包含 rust 特征的泛型类型上实现 deref?

How do you implement deref on a generic type containing a trait in rust?

如果能够使用 Deref 从通用容器生成 &TraitType,而不是调用 instance.as_ref() 会更方便。即:

(*my_container).do_thing();

对比

my_container.as_ref().do_thing();

为此,我尝试在容器类型上实现 Deref,但出现此错误:

<anon>:9:28: 9:29 error: expected a reference to a trait [E0172]
<anon>:9 impl<T> Deref for HasTrait<T + Send> {

发件人:

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; 
}

我试过使用 ?Sized 来增加 T 的边界以允许特征,但它似乎没有帮助?

正确的做法是什么?

这个有效:

use std::ops::Deref;

struct HasTrait<T: ?Sized> {
    data: Box<T>
}

impl<T: ?Sized> HasTrait<T> {
    fn as_ref(&self) -> &T {
        &*self.data
    }
}

impl<T: ?Sized> Deref for HasTrait<T> {
    type Target = T;

    fn deref<'a>(&'a self) -> &'a T {  // '
        self.as_ref()
    }
}

trait Foo {}
struct 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; 
}

基本上,您的问题与大小无关。只是这里HasTrait<T + Send>

impl<T> Deref<T> for HasTrait<T + Send>

没有意义。 T 可以是任意类型,u64 + Send 之类的是没有意义的。因此,我担心,您将无法约束 HasTrait 包含 only 特征和 only 那些类型是 Send。只是没有语法,我很确定类型系统不支持它。