Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust trait alias

Tags:

rust

I am working on a actor project which need alias for the trait because it is so long, but even with the nightly feature #![feature(trait_alias)] seems can not achieve.

In short I write a playground: I want alias A<T> to be shorter causing I have many generic type on A in real case; and the same time I want to access the type Output = Self; from its implementations B. Appreciate for any help.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=5a9bb8d3f76112c0b73ea1da8af34959

#![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;
}

like image 935
raul Avatar asked Nov 21 '25 05:11

raul


1 Answers

trait alias is only mean to be:

used wherever traits would normally be used as either bounds or trait objects. Source

So, your use case doesn't match, you can't do that.

like image 114
Stargateur Avatar answered Nov 24 '25 14:11

Stargateur