我正在开发一个演员项目,需要使用trait别名,因为原始名称太长了,但即使使用夜版功能#![feature(trait_alias)]
似乎也无法实现。
简而言之,我写了一个playground:
我想让别名A<T>
更短,因为我在实际情况下有许多泛型类型在A
中;同时我想从其实现B
中访问type Output = Self;
。
感谢任何帮助。
#![feature(trait_alias)]
trait A<T> {
type Output;
fn test(a: T) -> Self::Output;
}
//To alias the trait, real situation longer than this.
//attempt 1:
trait B: A<String>{}
//attempt 2:
//trait B : A<String, Output=Self> where Self: std::marker::Sized {}
//impl<T> B for T where T: A<String, Output=T> {}
//attempt 3 with trait_alias:
//trait B = A<String>;
struct SA;
impl B for SA {
type Output = Self;
}
impl TraitAlias for Type
不支持该语法: "trait_alias 特性添加了 trait 别名的支持。这允许为一个或多个 trait(目前仅限于单个常规 trait 和任意数量的自动 trait)创建别名,并在标准 trait 作为约束条件或 trait 对象使用的地方引用它们 [强调部分已添加]." - Mac O'Brien