创建一个通用于另一个通用结构的结构

Create a struct that is generic over another generic struct

我正在尝试在 Rust 中创建一个结构,它本身相对于其他通用结构是通用的。这很令人困惑,所以希望这个例子能让事情变得更清楚:

use std::ops::Deref;
use std::rc::Rc;

struct Foo<T: Deref> {
    val: T<i32>,
    other: i32,
}

impl<T> Foo<T> {
    pub fn new(&self, val: T<i32>, other: i32) -> Self {
        Foo {val: val, other: other}
    }
}

fn main() {
    let foo = Foo::new(Rc::new(0), 0);
}

playground

我希望能够通过使用 Rc<i32> 对象或 Arc<i32> 对象调用 new 来创建 Foo 对象,具体取决于我是否需要线程安全或不是。但是,当我尝试此操作时出现以下错误:error[E0109]: type parameters are not allowed on this type,因为编译器抱怨 val: T<i32>, 中的 i32。这在 Rust 中可能吗?如果是这样,假设它会自动解除引用,我可以安全地调用 i32 上的方法吗?

该语法没有意义,但此版本编译:

use std::ops::Deref;
use std::rc::Rc;
use std::sync::Arc;

struct Foo<T> {
    val: T,
    other: i32,
}

impl<T> Foo<T>
    where T: Deref<Target = i32>
{
    pub fn new(val: T, other: i32) -> Self {
        Foo {
            val: val,
            other: other,
        }
    }
}

fn main() {
    let foo = Foo::new(Rc::new(0), 0);
    let foo = Foo::new(Arc::new(0), 0);
}

注意 trait bounds 的读法:T: Deref<Target = i32> "Any T that implements Deref with a Target of an i32".

然后您可以实现取消引用的方法 val:

fn sum(&self) -> i32 {
    *self.val + self.other
}

总的来说,类似

的概念
struct Foo<T> {
    val: T<i32>,
}

不会有用。仅仅因为某些东西在 i32 上被参数化并不意味着你可以 任何事情 i32。同样,一个类型可以用 i32 之外的东西进行参数化(或根本不参数化),并且仍然可以让您访问 i32.