Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fast async API with asynchronous traits #290

Open
uselessgoddess opened this issue Jan 15, 2022 · 2 comments
Open

Implement fast async API with asynchronous traits #290

uselessgoddess opened this issue Jan 15, 2022 · 2 comments
Labels
api:Rust This affects the api async:Rust Unblock your code futures:Rust Wait better times Rust The most hype language

Comments

@uselessgoddess
Copy link
Member

uselessgoddess commented Jan 15, 2022

Implement async Doublets trait without dyn Future trait

Pin<Box<...>> from async_trait eats a lot of time, but it can be fixed

How to be

As you know, traits do not support non-dyn return -> impl Trait, but we can write:

trait ReturnImplTrait {
    type Impl;
    
    fn foo(&self) -> Self::Impl;
}

We also write [via #![feature(type_alias_impl_trait)] feature]:

impl ReturnImplTrait for () {
    type Impl = impl Display;

    fn foo(&self) -> Self::Impl {
        "it's really working"
    }
}

But

It's a wrong code:

trait ReturnImplTrait {
    type Impl;

    fn foo<T: Display>(t: T) -> Self::Impl;
}

impl ReturnImplTrait for () {
    type Impl = impl Display;

    fn foo<T: Display>(t: T) -> Self::Impl {
        t
    }
}

Also, not work

trait ReturnImplTrait {
    type Impl: Display;

    fn foo<T: Display>(t: T) -> Self::Impl;
}

impl ReturnImplTrait for () {
    type Impl = impl Display;

    fn foo<T: Display>(t: T) -> Self::Impl {
        t
    }
}

Hm… Use more features and magic!

Forward all generic params to trait-type (use #![feature(generic_associated_types)] feature)

Oh, Miracle

trait ReturnImplTrait {
    type Impl<T>;

    fn foo<T: Display>(t: T) -> Self::Impl<T>;
}

impl ReturnImplTrait for () {
    type Impl<T> = impl Display;

    fn foo<T: Display>(t: T) -> Self::Impl<T> {
        t
    }
}

Ok, use Future

trait AsyncSum {
    type SumFuture<T: Add<U>, U>: Future<Output = <T as Add<U>>::Output>;

    fn sum<T: Add<U>, U>(t: T, u: U) -> Self::SumFuture<T, U>;
}

impl AsyncSum for () {
    type SumFuture<T: Add<U>, U> = impl Future<Output = <T as Add<U>>::Output>;

    fn sum<T: Add<U>, U>(t: T, u: U) -> Self::SumFuture<T, U> {
        async { t.add(u) }
    }
}
@uselessgoddess uselessgoddess added futures:Rust Wait better times api:Rust This affects the api async:Rust Unblock your code labels Jan 15, 2022
@uselessgoddess
Copy link
Member Author

You can write via my fork of async-trait:

#[async_trait(!dyn)]
trait AsyncSum {
    async fn sum<T: Add<U>, U>(t: T, u: U) -> <T as Add<U>>::Output;
}

#[async_trait(!dyn)]
impl AsyncSum for () {
    async fn sum<T: Add<U>, U>(t: T, u: U) -> <T as Add<U>>::Output {
        t + u
    }
}

@uselessgoddess
Copy link
Member Author

uselessgoddess commented Jan 18, 2022

Or rate it rust-lang/rust#91611

@uselessgoddess uselessgoddess added the Rust The most hype language label Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:Rust This affects the api async:Rust Unblock your code futures:Rust Wait better times Rust The most hype language
Projects
None yet
Development

No branches or pull requests

1 participant