具有特征的通用 API

Generic APIs with traits

我正在尝试 generalize/decouple API,以便可以使用满足特征的替代结构。在有关嵌套特征的语法上苦苦挣扎。这是我正在尝试做的精简示例。请注意,在真实的中,有多个子特征,这可能会产生后续问题"how to reduce verbosity, if I'm on the right track"。

pub mod general {
    /// A trait to make the API generic
    pub trait MyTrait<A: PartialEq> {
        // type A: Partial
        fn val(self) -> A;
    }

    /// Part of the generic API
    pub struct Data<A: PartialEq, T: MyTrait<A>> {
        mys: T
    }

    /// Another part of the generic API
    fn doit<A: PartialEq>(s: impl MyTrait<A>) -> impl MyTrait {
        s
    }
}

pub mod specific {
    /// Api-specific substruct
    #[derive(PartialEq)]
    pub struct Aval {
        val: u32
    }

    /// My top-level custom struct
    pub struct MyS {
        val: Aval
    }
}

impl<A: PartialEq> crate::general::MyTrait<A> for crate::specific::MyS {
    fn val(self) -> crate::specific::Aval {
        self.val
    }
}

/// Example of how we'd use this
fn main() {
    let mys = crate::specific::MyS{ val: crate::specific::Aval{ val: 0 } };
    let S = crate::general::Data{mys};
    crate::general::doit(mys);  // Eg pretend we cloned or didn't create S.
}

Playground

在这个具体的例子中,我们有一个鸡+蛋:error[E0392]: parameter `A` is never used 的数据结构错误。如果我们删除 A:error[E0412]: cannot find type `A` in this scope

我怀疑这是与关联类型相关的语法问题。

只需从 struct 中删除特征绑定。

struct Data<T> {
    mys: T
}

您仍然可以为 Data<T> 添加方法和特征实现,需要 T 上更具体的边界,但您不需要它们来定义 Data<T> 的布局,因此您不应该在那里指定它们(在这种情况下,你不能,除非你添加一个 PhantomData 成员引用 A)。

您的代码还有许多其他错误,但我相信您会解决的。如果您再次遇到问题,请随时发表评论。