Giter Club home page Giter Club logo

real-async-trait-rs's Introduction

#[real_async_trait]

Build Status Crates.io Documentation

This nightly-only crate provides a proof-of-concept proc macro attribute that allows async functions within traits, without the possible runtime overhead of wrapping everything in a Box and erasing the types. This is made possible thanks to the unstable generic_associated_types and type_alias_impl_trait nightly features.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

real-async-trait-rs's People

Contributors

4ldo2 avatar danielhenrymantilla avatar eholk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

real-async-trait-rs's Issues

master and latest crates.io version not compiling

The latest version currently on crates.io (0.0.2) does not incorporate PR #5 which is needed to use this crate with a recent compiler, therefore a new version should be pushed to crates.io.
Furthermore, the latest version of syn now requires the extra-traits feature to be enabled for some of the traits used in this crate.
Currently, compilation will fail with errors like:

error[E0277]: `AttrStyle` doesn't implement `Debug`
   --> /home/nett/.cargo/git/checkouts/real-async-trait-rs-99511d8d826cde78/1789be6/src/lib.rs:559:9
    |
559 |         assert_eq!(attr.style, AttrStyle::Outer);
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `AttrStyle` cannot be formatted using `{:?}` because it doesn't implement `Debug`
    |
    = help: the trait `Debug` is not implemented for `AttrStyle`
    = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

Multiple impl in one module do not compile

If I have two implementations for async traits in one module, I get a compiler error:

error[E0428]: the name `__real_async_trait_impl` is defined multiple times
  --> src/lib.rs:22:1
   |
13 | #[real_async_trait]
   | ------------------- previous definition of the module `__real_async_trait_impl` here
...
22 | #[real_async_trait]
   | ^^^^^^^^^^^^^^^^^^^ `__real_async_trait_impl` redefined here
   |
   = note: `__real_async_trait_impl` must be defined only once in the type namespace of this module
   = note: this error originates in the attribute macro `real_async_trait` (in Nightly builds, run with -Z macro-backtrace for more info)

I used this code:

#![feature(generic_associated_types)]
#![feature(type_alias_impl_trait)]

use real_async_trait::real_async_trait;

#[real_async_trait]
trait X {
    async fn nop<'a>(&'a self) -> ();
}

struct T {}

#[real_async_trait]
impl X for T {
    async fn nop<'a>(&'a self) -> () {
        ()
    }
}

struct T2 {}

#[real_async_trait]
impl X for T2 {
    async fn nop<'a>(&'a self) -> () {
        ()
    }
}

I used real-async-trait version 0.0.2 and rustc 1.57.0-nightly (485ced56b 2021-10-07).

Is impl trait alias really necessary?

I was wondering what part currently fails if removing the type alias, as that could potentially bring us to stable rust, with GATs now stabilized.

type parameter is part of concrete type but not used in parameter list for the `impl Trait` type alias

It seems like the macro doesn't support implementing traits for structs with generic parameters like:

trait Foo {}
struct Bar<F: Foo> { f: F }

#[real_async_trait]
trait Bat {
    async fn baz();
}

#[real_async_trait]
impl<F: Foo> Bat for Bar<F> {
    async fn baz() { }
}

Compilation fails with the error

error: type parameter `F` is part of concrete type but not used in parameter list for the `impl Trait` type alias
  --> src/main.rs:13:1
   |
13 | #[real_async_trait]
   | ^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in the attribute macro `real_async_trait` (in Nightly builds, run with -Z macro-backtrace for more info)

Manually typing out the async trait definition and implementation works:

trait Foo {}
struct Bar<F: Foo> { f: F }

trait Bat {
    type BazFuture<'a>: Future<Output = ()>;
    fn baz<'a>() -> Self::BazFuture<'a>;
}

impl<F: Foo> Bat for Bar<F> {
    type BazFuture<'a> = impl Future<Output = ()>;
    fn baz<'a>() -> Self::BazFuture<'a> { async move { } }
}

`generic_associated_types` stabilized

The generic_associated_types is not only no longer marked incomplete but has in fact been stabilized and is no longer necessary to specify.
Please also consider updating the README accordingly.
Also, adding a return_position_impl_trait_in_trait mode, enabled via optional feature on this crate, would be absolutely hilarious.

Generates invalid code after rust-lang/rust#89970

generates invalid code after rust-lang/rust#89970

the generated code:

  fn open< 'a>(& 'a mut self,path: & 'a[u8],flags:usize) -> Self::__real_async_trait_impl_TypeFor_open< 'a> ;
  type __real_async_trait_impl_TypeFor_open< 'a> : ::core::future::Future<Output = Result<usize,Errno> > + 'a;
18 | #[real_async_trait]
   | ^^^^^^^^^^^^^^^^^^^
...
21 |     async fn open<'a>(&'a mut self, path: &'a [u8], flags: usize) -> Result<usize, Errno>;
   |                     - help: add the required where clauses: `where Self: 'a`

the expected code:

    fn open<'a>(&'a mut self, path: &'a [u8], flags: usize, ) -> Self::__real_async_trait_impl_TypeFor_opsen<'a>;
    
    type __real_async_trait_impl_TypeFor_opsen<'a>: ::core::future::Future<Output = Result<usize, Errno>> + 'a where Self: 'a;

i'm not sure if this is solving the issue completely but it was ok in my case.

diff --git a/src/lib.rs b/src/lib.rs
index 1366b81..cc2ba22 100644
--- a/src/lib.rs
+++ b/src/lib.rs
 use syn::{
     AngleBracketedGenericArguments, AttrStyle, Attribute, Binding, Block, Expr, ExprAsync, FnArg,
     GenericArgument, GenericParam, Generics, Ident, ImplItem, ImplItemType, ItemImpl, ItemTrait,
@@ -311,7 +315,30 @@ fn handle_item_impl(mut item: ItemImpl) -> TokenStream {
             generics: Generics {
                 lt_token: Some(Token!(<)(Span::call_site())),
                 gt_token: Some(Token!(>)(Span::call_site())),
-                where_clause: None,
+                where_clause: Some(WhereClause {
+                    where_token: Where::default(),
+                    predicates: function_lifetimes
+                        .iter()
+                        .cloned()
+                        .map(|lifetimedef| {
+                            WherePredicate::Type(PredicateType {
+                                colon_token: Token!(:)(Span::call_site()),
+                                lifetimes: None,
+                                bounded_ty: Type::Path(TypePath {
+                                    qself: None,
+                                    path: Path {
+                                        leading_colon: None,
+                                        segments: Punctuated::from_iter([PathSegment {
+                                            ident: Ident::new("Self", Span::call_site()),
+                                            arguments: PathArguments::None,
+                                        }]),
+                                    },
+                                }),
+                                bounds: Punctuated::from_iter([TypeParamBound::Lifetime(lifetimedef.lifetime)]),
+                            })
+                        })
+                        .collect(),
+                }),
                 params: function_lifetimes
                     .iter()
                     .cloned()
@@ -590,7 +617,30 @@ fn handle_item_trait(mut item: ItemTrait) -> TokenStream {
             generics: Generics {
                 lt_token: Some(Token!(<)(Span::call_site())),
                 gt_token: Some(Token!(>)(Span::call_site())),
-                where_clause: None,
+                where_clause: Some(WhereClause {
+                    where_token: Where::default(),
+                    predicates: function_lifetimes
+                        .iter()
+                        .cloned()
+                        .map(|lifetimedef| {
+                            WherePredicate::Type(PredicateType {
+                                colon_token: Token!(:)(Span::call_site()),
+                                lifetimes: None,
+                                bounded_ty: Type::Path(TypePath {
+                                    qself: None,
+                                    path: Path {
+                                        leading_colon: None,
+                                        segments: Punctuated::from_iter([PathSegment {
+                                            ident: Ident::new("Self", Span::call_site()),
+                                            arguments: PathArguments::None,
+                                        }]),
+                                    },
+                                }),
+                                bounds: Punctuated::from_iter([TypeParamBound::Lifetime(lifetimedef.lifetime)]),
+                            })
+                        })
+                        .collect(),
+                }),
                 params: function_lifetimes
                     .iter()
                     .cloned()

dyn real_async_trait?

Is there a way to make a dyn Trait object out of a type that impls a trait annotated with #[real_async_trait]?

I'd like to be able to do something like:

#[real_async_trait]
trait Foo {
    async fn foo();
}

struct Bar {
    maybe_foo: Option<Box<dyn Foo>>,
}

When I try to do this, however, I get an error saying I need to specify the associated type for Foo::foo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.