async-trait-0.1.24/.gitignore010066400017500001750000000000361351553426600142600ustar0000000000000000/target **/*.rs.bk Cargo.lock async-trait-0.1.24/.travis.yml010066400017500001750000000003401355510100400143570ustar0000000000000000language: rust matrix: include: - rust: nightly script: cargo test env: RUSTFLAGS='--cfg async_trait_nightly_testing' - rust: beta script: cargo test - rust: stable script: cargo check async-trait-0.1.24/Cargo.toml.orig010064400017500001750000000016721361621557400151640ustar0000000000000000[package] name = "async-trait" version = "0.1.24" authors = ["David Tolnay "] edition = "2018" license = "MIT OR Apache-2.0" description = "Type erasure for async trait methods" repository = "https://github.com/dtolnay/async-trait" documentation = "https://docs.rs/async-trait" readme = "README.md" [lib] proc-macro = true [badges] travis-ci = { repository = "dtolnay/async-trait" } [dependencies] proc-macro2 = "1.0" quote = "1.0" syn = { version = "1.0", features = ["full", "visit-mut"] } [dev-dependencies] rustversion = "1.0" trybuild = { version = "1.0.19", features = ["diff"] } [features] # Alternative implementation that produces worse error messages but potentially # works on old nightlies without https://github.com/rust-lang/rust/pull/61775 in # some cases where the default implementation does not. This feature is semver # exempt and might get removed without notice in any patch version. support_old_nightly = [] async-trait-0.1.24/Cargo.toml0000644000000023101361621560700114550ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "async-trait" version = "0.1.24" authors = ["David Tolnay "] description = "Type erasure for async trait methods" documentation = "https://docs.rs/async-trait" readme = "README.md" license = "MIT OR Apache-2.0" repository = "https://github.com/dtolnay/async-trait" [lib] proc-macro = true [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.syn] version = "1.0" features = ["full", "visit-mut"] [dev-dependencies.rustversion] version = "1.0" [dev-dependencies.trybuild] version = "1.0.19" features = ["diff"] [features] support_old_nightly = [] [badges.travis-ci] repository = "dtolnay/async-trait" async-trait-0.1.24/LICENSE-APACHE010064400017500001750000000251371351553620100142120ustar0000000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. async-trait-0.1.24/LICENSE-MIT010064400017500001750000000017771351553620100137260ustar0000000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. async-trait-0.1.24/README.md010064400017500001750000000162551361265107000135460ustar0000000000000000Async trait methods =================== [![Build Status](https://api.travis-ci.com/dtolnay/async-trait.svg?branch=master)](https://travis-ci.com/dtolnay/async-trait) [![Latest Version](https://img.shields.io/crates/v/async-trait.svg)](https://crates.io/crates/async-trait) [![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/async-trait) The async/await language feature is on track for an initial round of stabilizations in Rust 1.39 (tracking issue: [rust-lang/rust#62149]), but this does not include support for async fn in traits. Trying to include an async fn in a trait produces the following error: [rust-lang/rust#62149]: https://github.com/rust-lang/rust/issues/62149 ```rust trait MyTrait { async fn f() {} } ``` ```console error[E0706]: trait fns cannot be declared `async` --> src/main.rs:4:5 | 4 | async fn f() {} | ^^^^^^^^^^^^^^^ ``` This crate provides an attribute macro to make async fn in traits work. Please refer to [*why async fn in traits are hard*][hard] for a deeper analysis of how this implementation differs from what the compiler and language hope to deliver in the future. [hard]: https://smallcultfollowing.com/babysteps/blog/2019/10/26/async-fn-in-traits-are-hard/
## Example This example implements the core of a highly effective advertising platform using async fn in a trait. The only thing to notice here is that we write an `#[async_trait]` macro on top of traits and trait impls that contain async fn, and then they work. ```rust use async_trait::async_trait; #[async_trait] trait Advertisement { async fn run(&self); } struct Modal; #[async_trait] impl Advertisement for Modal { async fn run(&self) { self.render_fullscreen().await; for _ in 0..4u16 { remind_user_to_join_mailing_list().await; } self.hide_for_now().await; } } struct AutoplayingVideo { media_url: String, } #[async_trait] impl Advertisement for AutoplayingVideo { async fn run(&self) { let stream = connect(&self.media_url).await; stream.play().await; // Video probably persuaded user to join our mailing list! Modal.run().await; } } ```
## Supported features It is the intention that all features of Rust traits should work nicely with \#\[async_trait\], but the edge cases are numerous. *Please file an issue if you see unexpected borrow checker errors, type errors, or warnings.* There is no use of `unsafe` in the expanded code, so rest assured that if your code compiles it can't be that badly broken. - 👍 Self by value, by reference, by mut reference, or no self; - 👍 Any number of arguments, any return value; - 👍 Generic type parameters and lifetime parameters; - 👍 Associated types; - 👍 Having async and non-async functions in the same trait; - 👍 Default implementations provided by the trait; - 👍 Elided lifetimes; - 👍 Dyn-capable traits.
## Explanation Async fns get transformed into methods that return `Pin>` and delegate to a private async freestanding function. For example the `impl Advertisement for AutoplayingVideo` above would be expanded as: ```rust impl Advertisement for AutoplayingVideo { fn run<'async>( &'async self, ) -> Pin + Send + 'async>> where Self: Sync + 'async, { async fn run(_self: &AutoplayingVideo) { /* the original method body */ } Box::pin(run(self)) } } ```
## Non-threadsafe futures Not all async traits need futures that are `dyn Future + Send`. To avoid having Send and Sync bounds placed on the async trait methods, invoke the async trait macro as `#[async_trait(?Send)]` on both the trait and the impl blocks.
## Elided lifetimes Be aware that async fn syntax does not allow lifetime elision outside of `&` and `&mut` references. (This is true even when not using #\[async_trait\].) Lifetimes must be named or marked by the placeholder `'_`. Fortunately the compiler is able to diagnose missing lifetimes with a good error message. ```rust type Elided<'a> = &'a usize; #[async_trait] trait Test { async fn test(not_okay: Elided, okay: &usize) {} } ``` ```console error[E0726]: implicit elided lifetime not allowed here --> src/main.rs:9:29 | 9 | async fn test(not_okay: Elided, okay: &usize) {} | ^^^^^^- help: indicate the anonymous lifetime: `<'_>` ``` The fix is to name the lifetime or use `'_`. ```rust #[async_trait] trait Test { // either async fn test<'e>(elided: Elided<'e>) {} // or async fn test(elided: Elided<'_>) {} } ```
## Dyn traits Traits with async methods can be used as trait objects as long as they meet the usual requirements for dyn -- no methods with type parameters, no self by value, no associated types, etc. ```rust #[async_trait] pub trait ObjectSafe { async fn f(&self); async fn g(&mut self); } impl ObjectSafe for MyType {...} let value: MyType = ...; let object = &value as &dyn ObjectSafe; // make trait object ``` The one wrinkle is in traits that provide default implementations of async methods. In order for the default implementation to produce a future that is Send, the async\_trait macro must emit a bound of `Self: Sync` on trait methods that take `&self` and a bound `Self: Send` on trait methods that take `&mut self`. An example of the former is visible in the expanded code in the explanation section above. If you make a trait with async methods that have default implementations, everything will work except that the trait cannot be used as a trait object. Creating a value of type `&dyn Trait` will produce an error that looks like this: ```console error: the trait `Test` cannot be made into an object --> src/main.rs:8:5 | 8 | async fn cannot_dyn(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` For traits that need to be object safe and need to have default implementations for some async methods, there are two resolutions. Either you can add Send and/or Sync as supertraits (Send if there are `&mut self` methods with default implementations, Sync if there are `&self` methods with default implementions) to constrain all implementors of the trait such that the default implementations are applicable to them: ```rust #[async_trait] pub trait ObjectSafe: Sync { // added supertrait async fn can_dyn(&self) {} } let object = &value as &dyn ObjectSafe; ``` or you can strike the problematic methods from your trait object by bounding them with `Self: Sized`: ```rust #[async_trait] pub trait ObjectSafe { async fn cannot_dyn(&self) where Self: Sized {} // presumably other methods } let object = &value as &dyn ObjectSafe; ```
#### License Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. async-trait-0.1.24/src/args.rs010066400017500001750000000014211352061025400143440ustar0000000000000000use proc_macro2::Span; use syn::parse::{Error, Parse, ParseStream, Result}; use syn::Token; #[derive(Copy, Clone)] pub struct Args { pub local: bool, } mod kw { syn::custom_keyword!(Send); } impl Parse for Args { fn parse(input: ParseStream) -> Result { match try_parse(input) { Ok(args) if input.is_empty() => Ok(args), _ => Err(error()), } } } fn try_parse(input: ParseStream) -> Result { if input.peek(Token![?]) { input.parse::()?; input.parse::()?; Ok(Args { local: true }) } else { Ok(Args { local: false }) } } fn error() -> Error { let msg = "expected #[async_trait] or #[async_trait(?Send)]"; Error::new(Span::call_site(), msg) } async-trait-0.1.24/src/expand.rs010064400017500001750000000340751361621556600147150ustar0000000000000000use crate::lifetime::{has_async_lifetime, CollectLifetimes}; use crate::parse::Item; use crate::receiver::{ has_self_in_block, has_self_in_sig, has_self_in_where_predicate, ReplaceReceiver, }; use proc_macro2::{Span, TokenStream}; use quote::{format_ident, quote, quote_spanned, ToTokens}; use std::mem; use syn::punctuated::Punctuated; use syn::visit_mut::VisitMut; use syn::{ parse_quote, Block, FnArg, GenericParam, Generics, Ident, ImplItem, Lifetime, Pat, PatIdent, Path, Receiver, ReturnType, Signature, Token, TraitItem, Type, TypeParam, TypeParamBound, WhereClause, }; impl ToTokens for Item { fn to_tokens(&self, tokens: &mut TokenStream) { match self { Item::Trait(item) => item.to_tokens(tokens), Item::Impl(item) => item.to_tokens(tokens), } } } #[derive(Clone, Copy)] enum Context<'a> { Trait { name: &'a Ident, generics: &'a Generics, supertraits: &'a Supertraits, }, Impl { impl_generics: &'a Generics, receiver: &'a Type, as_trait: &'a Path, }, } impl Context<'_> { fn lifetimes<'a>(&'a self, used: &'a [Lifetime]) -> impl Iterator { let generics = match self { Context::Trait { generics, .. } => generics, Context::Impl { impl_generics, .. } => impl_generics, }; generics.params.iter().filter(move |param| { if let GenericParam::Lifetime(param) = param { used.contains(¶m.lifetime) } else { false } }) } } type Supertraits = Punctuated; pub fn expand(input: &mut Item, is_local: bool) { match input { Item::Trait(input) => { let context = Context::Trait { name: &input.ident, generics: &input.generics, supertraits: &input.supertraits, }; for inner in &mut input.items { if let TraitItem::Method(method) = inner { let sig = &mut method.sig; if sig.asyncness.is_some() { let block = &mut method.default; let mut has_self = has_self_in_sig(sig); if let Some(block) = block { has_self |= has_self_in_block(block); transform_block(context, sig, block, has_self, is_local); } let has_default = method.default.is_some(); transform_sig(context, sig, has_self, has_default, is_local); } } } } Item::Impl(input) => { let context = Context::Impl { impl_generics: &input.generics, receiver: &input.self_ty, as_trait: &input.trait_.as_ref().unwrap().1, }; for inner in &mut input.items { if let ImplItem::Method(method) = inner { let sig = &mut method.sig; if sig.asyncness.is_some() { let block = &mut method.block; let has_self = has_self_in_sig(sig) || has_self_in_block(block); transform_block(context, sig, block, has_self, is_local); transform_sig(context, sig, has_self, false, is_local); } } } } } } // Input: // async fn f(&self, x: &T) -> Ret; // // Output: // fn f<'life0, 'life1, 'async_trait, T>( // &'life0 self, // x: &'life1 T, // ) -> Pin + Send + 'async_trait>> // where // 'life0: 'async_trait, // 'life1: 'async_trait, // T: 'async_trait, // Self: Sync + 'async_trait; fn transform_sig( context: Context, sig: &mut Signature, has_self: bool, has_default: bool, is_local: bool, ) { sig.fn_token.span = sig.asyncness.take().unwrap().span; let ret = match &sig.output { ReturnType::Default => quote!(()), ReturnType::Type(_, ret) => quote!(#ret), }; let mut lifetimes = CollectLifetimes::new(); for arg in sig.inputs.iter_mut() { match arg { FnArg::Receiver(arg) => lifetimes.visit_receiver_mut(arg), FnArg::Typed(arg) => lifetimes.visit_type_mut(&mut arg.ty), } } let where_clause = sig .generics .where_clause .get_or_insert_with(|| WhereClause { where_token: Default::default(), predicates: Punctuated::new(), }); for param in sig .generics .params .iter() .chain(context.lifetimes(&lifetimes.explicit)) { match param { GenericParam::Type(param) => { let param = ¶m.ident; where_clause .predicates .push(parse_quote!(#param: 'async_trait)); } GenericParam::Lifetime(param) => { let param = ¶m.lifetime; where_clause .predicates .push(parse_quote!(#param: 'async_trait)); } GenericParam::Const(_) => {} } } for elided in lifetimes.elided { sig.generics.params.push(parse_quote!(#elided)); where_clause .predicates .push(parse_quote!(#elided: 'async_trait)); } sig.generics.params.push(parse_quote!('async_trait)); if has_self { let bound: Ident = match sig.inputs.iter().next() { Some(FnArg::Receiver(Receiver { reference: Some(_), mutability: None, .. })) => parse_quote!(Sync), _ => parse_quote!(Send), }; let assume_bound = match context { Context::Trait { supertraits, .. } => !has_default || has_bound(supertraits, &bound), Context::Impl { .. } => true, }; where_clause.predicates.push(if assume_bound || is_local { parse_quote!(Self: 'async_trait) } else { parse_quote!(Self: ::core::marker::#bound + 'async_trait) }); } for (i, arg) in sig.inputs.iter_mut().enumerate() { match arg { FnArg::Receiver(Receiver { reference: Some(_), .. }) => {} FnArg::Receiver(arg) => arg.mutability = None, FnArg::Typed(arg) => { if let Pat::Ident(ident) = &mut *arg.pat { ident.by_ref = None; ident.mutability = None; } else { let positional = positional_arg(i); *arg.pat = parse_quote!(#positional); } } } } let bounds = if is_local { quote!('async_trait) } else { quote!(::core::marker::Send + 'async_trait) }; sig.output = parse_quote! { -> ::core::pin::Pin + #bounds >> }; } // Input: // async fn f(&self, x: &T) -> Ret { // self + x // } // // Output: // async fn f(_self: &AsyncTrait, x: &T) -> Ret { // _self + x // } // Box::pin(async_trait_method::(self, x)) fn transform_block( context: Context, sig: &mut Signature, block: &mut Block, has_self: bool, is_local: bool, ) { if cfg!(feature = "support_old_nightly") { let brace = block.brace_token; *block = parse_quote!({ Box::pin(async move #block) }); block.brace_token = brace; return; } let inner = format_ident!("__{}", sig.ident); let args = sig.inputs.iter().enumerate().map(|(i, arg)| match arg { FnArg::Receiver(_) => quote!(self), FnArg::Typed(arg) => { if let Pat::Ident(PatIdent { ident, .. }) = &*arg.pat { quote!(#ident) } else { positional_arg(i).into_token_stream() } } }); let mut standalone = sig.clone(); standalone.ident = inner.clone(); let generics = match context { Context::Trait { generics, .. } => generics, Context::Impl { impl_generics, .. } => impl_generics, }; let mut outer_generics = generics.clone(); if !has_self { if let Some(mut where_clause) = outer_generics.where_clause { where_clause.predicates = where_clause .predicates .into_iter() .filter_map(|mut pred| { if has_self_in_where_predicate(&mut pred) { None } else { Some(pred) } }) .collect(); outer_generics.where_clause = Some(where_clause); } } let fn_generics = mem::replace(&mut standalone.generics, outer_generics); standalone.generics.params.extend(fn_generics.params); if let Some(where_clause) = fn_generics.where_clause { standalone .generics .make_where_clause() .predicates .extend(where_clause.predicates); } if has_async_lifetime(&mut standalone, block) { standalone.generics.params.push(parse_quote!('async_trait)); } let mut types = standalone .generics .type_params() .map(|param| param.ident.clone()) .collect::>(); let mut self_bound = None::; match standalone.inputs.iter_mut().next() { Some( arg @ FnArg::Receiver(Receiver { reference: Some(_), .. }), ) => { let (lifetime, mutability, self_token) = match arg { FnArg::Receiver(Receiver { reference: Some((_, lifetime)), mutability, self_token, .. }) => (lifetime, mutability, self_token), _ => unreachable!(), }; let under_self = Ident::new("_self", self_token.span); match context { Context::Trait { .. } => { self_bound = Some(match mutability { Some(_) => parse_quote!(::core::marker::Send), None => parse_quote!(::core::marker::Sync), }); *arg = parse_quote! { #under_self: &#lifetime #mutability AsyncTrait }; } Context::Impl { receiver, .. } => { *arg = parse_quote! { #under_self: &#lifetime #mutability #receiver }; } } } Some(arg @ FnArg::Receiver(_)) => { let (self_token, mutability) = match arg { FnArg::Receiver(Receiver { self_token, mutability, .. }) => (self_token, mutability), _ => unreachable!(), }; let under_self = Ident::new("_self", self_token.span); match context { Context::Trait { .. } => { self_bound = Some(parse_quote!(::core::marker::Send)); *arg = parse_quote! { #mutability #under_self: AsyncTrait }; } Context::Impl { receiver, .. } => { *arg = parse_quote! { #mutability #under_self: #receiver }; } } } Some(FnArg::Typed(arg)) => { if let Pat::Ident(arg) = &mut *arg.pat { if arg.ident == "self" { arg.ident = Ident::new("_self", arg.ident.span()); } } } _ => {} } if let Context::Trait { name, generics, .. } = context { if has_self { let (_, generics, _) = generics.split_for_impl(); let mut self_param: TypeParam = parse_quote!(AsyncTrait: ?Sized + #name #generics); if !is_local { self_param.bounds.extend(self_bound); } standalone .generics .params .push(GenericParam::Type(self_param)); types.push(Ident::new("Self", Span::call_site())); } } if let Some(where_clause) = &mut standalone.generics.where_clause { // Work around an input bound like `where Self::Output: Send` expanding // to `where ::Output: Send` which is illegal syntax because // `where` is reserved for future use... :( where_clause.predicates.insert(0, parse_quote!((): Sized)); } let mut replace = match context { Context::Trait { .. } => ReplaceReceiver::with(parse_quote!(AsyncTrait)), Context::Impl { receiver, as_trait, .. } => ReplaceReceiver::with_as_trait(receiver.clone(), as_trait.clone()), }; replace.visit_signature_mut(&mut standalone); replace.visit_block_mut(block); let mut generics = types; let consts = standalone .generics .const_params() .map(|param| param.ident.clone()); generics.extend(consts); let brace = block.brace_token; let box_pin = quote_spanned!(brace.span=> { #[allow( clippy::missing_docs_in_private_items, clippy::type_repetition_in_bounds, clippy::used_underscore_binding, )] #standalone #block Box::pin(#inner::<#(#generics),*>(#(#args),*)) }); *block = parse_quote!(#box_pin); block.brace_token = brace; } fn positional_arg(i: usize) -> Ident { format_ident!("__arg{}", i) } fn has_bound(supertraits: &Supertraits, marker: &Ident) -> bool { for bound in supertraits { if let TypeParamBound::Trait(bound) = bound { if bound.path.is_ident(marker) { return true; } } } false } async-trait-0.1.24/src/lib.rs010064400017500001750000000217531357167470700142110ustar0000000000000000//!
Type erasure for async trait methods
//! //! The async/await language feature is on track for an initial round of //! stabilizations in Rust 1.39 (tracking issue: [rust-lang/rust#62149]), but //! this does not include support for async fn in traits. Trying to include an //! async fn in a trait produces the following error: //! //! [rust-lang/rust#62149]: https://github.com/rust-lang/rust/issues/62149 //! //! ```compile_fail //! trait MyTrait { //! async fn f() {} //! } //! ``` //! //! ```text //! error[E0706]: trait fns cannot be declared `async` //! --> src/main.rs:4:5 //! | //! 4 | async fn f() {} //! | ^^^^^^^^^^^^^^^ //! ``` //! //! This crate provides an attribute macro to make async fn in traits work. //! //! Please refer to [*why async fn in traits are hard*][hard] for a deeper //! analysis of how this implementation differs from what the compiler and //! language hope to deliver in the future. //! //! [hard]: https://smallcultfollowing.com/babysteps/blog/2019/10/26/async-fn-in-traits-are-hard/ //! //!
//! //! # Example //! //! This example implements the core of a highly effective advertising platform //! using async fn in a trait. //! //! The only thing to notice here is that we write an `#[async_trait]` macro on //! top of traits and trait impls that contain async fn, and then they work. //! //! ``` //! use async_trait::async_trait; //! //! #[async_trait] //! trait Advertisement { //! async fn run(&self); //! } //! //! struct Modal; //! //! #[async_trait] //! impl Advertisement for Modal { //! async fn run(&self) { //! self.render_fullscreen().await; //! for _ in 0..4u16 { //! remind_user_to_join_mailing_list().await; //! } //! self.hide_for_now().await; //! } //! } //! //! struct AutoplayingVideo { //! media_url: String, //! } //! //! #[async_trait] //! impl Advertisement for AutoplayingVideo { //! async fn run(&self) { //! let stream = connect(&self.media_url).await; //! stream.play().await; //! //! // Video probably persuaded user to join our mailing list! //! Modal.run().await; //! } //! } //! # //! # impl Modal { //! # async fn render_fullscreen(&self) {} //! # async fn hide_for_now(&self) {} //! # } //! # //! # async fn remind_user_to_join_mailing_list() {} //! # //! # struct Stream; //! # async fn connect(_media_url: &str) -> Stream { Stream } //! # impl Stream { //! # async fn play(&self) {} //! # } //! ``` //! //!

//! //! # Supported features //! //! It is the intention that all features of Rust traits should work nicely with //! #\[async_trait\], but the edge cases are numerous. Please file an issue if //! you see unexpected borrow checker errors, type errors, or warnings. There is //! no use of `unsafe` in the expanded code, so rest assured that if your code //! compiles it can't be that badly broken. //! //! > ☑ Self by value, by reference, by mut reference, or no self;
//! > ☑ Any number of arguments, any return value;
//! > ☑ Generic type parameters and lifetime parameters;
//! > ☑ Associated types;
//! > ☑ Having async and non-async functions in the same trait;
//! > ☑ Default implementations provided by the trait;
//! > ☑ Elided lifetimes;
//! > ☑ Dyn-capable traits.
//! //!
//! //! # Explanation //! //! Async fns get transformed into methods that return `Pin>` and delegate to a private async freestanding function. //! //! For example the `impl Advertisement for AutoplayingVideo` above would be //! expanded as: //! //! ``` //! # const IGNORE: &str = stringify! { //! impl Advertisement for AutoplayingVideo { //! fn run<'async>( //! &'async self, //! ) -> Pin + Send + 'async>> //! where //! Self: Sync + 'async, //! { //! async fn run(_self: &AutoplayingVideo) { //! /* the original method body */ //! } //! //! Box::pin(run(self)) //! } //! } //! # }; //! ``` //! //!

//! //! # Non-threadsafe futures //! //! Not all async traits need futures that are `dyn Future + Send`. To avoid //! having Send and Sync bounds placed on the async trait methods, invoke the //! async trait macro as `#[async_trait(?Send)]` on both the trait and the impl //! blocks. //! //!
//! //! # Elided lifetimes //! //! Be aware that async fn syntax does not allow lifetime elision outside of `&` //! and `&mut` references. (This is true even when not using #\[async_trait\].) //! Lifetimes must be named or marked by the placeholder `'_`. //! //! Fortunately the compiler is able to diagnose missing lifetimes with a good //! error message. //! //! ```compile_fail //! # use async_trait::async_trait; //! # //! type Elided<'a> = &'a usize; //! //! #[async_trait] //! trait Test { //! async fn test(not_okay: Elided, okay: &usize) {} //! } //! ``` //! //! ```text //! error[E0726]: implicit elided lifetime not allowed here //! --> src/main.rs:9:29 //! | //! 9 | async fn test(not_okay: Elided, okay: &usize) {} //! | ^^^^^^- help: indicate the anonymous lifetime: `<'_>` //! ``` //! //! The fix is to name the lifetime or use `'_`. //! //! ``` //! # use async_trait::async_trait; //! # //! # type Elided<'a> = &'a usize; //! # //! #[async_trait] //! trait Test { //! // either //! async fn test<'e>(elided: Elided<'e>) {} //! # } //! # #[async_trait] //! # trait Test2 { //! // or //! async fn test(elided: Elided<'_>) {} //! } //! ``` //! //!

//! //! # Dyn traits //! //! Traits with async methods can be used as trait objects as long as they meet //! the usual requirements for dyn -- no methods with type parameters, no self //! by value, no associated types, etc. //! //! ``` //! # use async_trait::async_trait; //! # //! #[async_trait] //! pub trait ObjectSafe { //! async fn f(&self); //! async fn g(&mut self); //! } //! //! # const IGNORE: &str = stringify! { //! impl ObjectSafe for MyType {...} //! //! let value: MyType = ...; //! # }; //! # //! # struct MyType; //! # //! # #[async_trait] //! # impl ObjectSafe for MyType { //! # async fn f(&self) {} //! # async fn g(&mut self) {} //! # } //! # //! # let value: MyType = MyType; //! let object = &value as &dyn ObjectSafe; // make trait object //! ``` //! //! The one wrinkle is in traits that provide default implementations of async //! methods. In order for the default implementation to produce a future that is //! Send, the async_trait macro must emit a bound of `Self: Sync` on trait //! methods that take `&self` and a bound `Self: Send` on trait methods that //! take `&mut self`. An example of the former is visible in the expanded code //! in the explanation section above. //! //! If you make a trait with async methods that have default implementations, //! everything will work except that the trait cannot be used as a trait object. //! Creating a value of type `&dyn Trait` will produce an error that looks like //! this: //! //! ```text //! error: the trait `Test` cannot be made into an object //! --> src/main.rs:8:5 //! | //! 8 | async fn cannot_dyn(&self) {} //! | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //! ``` //! //! For traits that need to be object safe and need to have default //! implementations for some async methods, there are two resolutions. Either //! you can add Send and/or Sync as supertraits (Send if there are `&mut self` //! methods with default implementations, Sync if there are `&self` methods with //! default implementions) to constrain all implementors of the trait such that //! the default implementations are applicable to them: //! //! ``` //! # use async_trait::async_trait; //! # //! #[async_trait] //! pub trait ObjectSafe: Sync { // added supertrait //! async fn can_dyn(&self) {} //! } //! # //! # struct MyType; //! # //! # #[async_trait] //! # impl ObjectSafe for MyType {} //! # //! # let value = MyType; //! //! let object = &value as &dyn ObjectSafe; //! ``` //! //! or you can strike the problematic methods from your trait object by //! bounding them with `Self: Sized`: //! //! ``` //! # use async_trait::async_trait; //! # //! #[async_trait] //! pub trait ObjectSafe { //! async fn cannot_dyn(&self) where Self: Sized {} //! //! // presumably other methods //! } //! # //! # struct MyType; //! # //! # #[async_trait] //! # impl ObjectSafe for MyType {} //! # //! # let value = MyType; //! //! let object = &value as &dyn ObjectSafe; //! ``` extern crate proc_macro; mod args; mod expand; mod lifetime; mod parse; mod receiver; use crate::args::Args; use crate::expand::expand; use crate::parse::Item; use proc_macro::TokenStream; use quote::quote; use syn::parse_macro_input; #[proc_macro_attribute] pub fn async_trait(args: TokenStream, input: TokenStream) -> TokenStream { let args = parse_macro_input!(args as Args); let mut item = parse_macro_input!(input as Item); expand(&mut item, args.local); TokenStream::from(quote!(#item)) } async-trait-0.1.24/src/lifetime.rs010066400017500001750000000043141353760637700152350ustar0000000000000000use proc_macro2::Span; use syn::visit_mut::{self, VisitMut}; use syn::{Block, GenericArgument, Item, Lifetime, Receiver, Signature, TypeReference}; pub fn has_async_lifetime(sig: &mut Signature, block: &mut Block) -> bool { let mut visitor = HasAsyncLifetime(false); visitor.visit_signature_mut(sig); visitor.visit_block_mut(block); visitor.0 } struct HasAsyncLifetime(bool); impl VisitMut for HasAsyncLifetime { fn visit_lifetime_mut(&mut self, life: &mut Lifetime) { self.0 |= life.to_string() == "'async_trait"; } fn visit_item_mut(&mut self, _: &mut Item) { // Do not recurse into nested items. } } pub struct CollectLifetimes { pub elided: Vec, pub explicit: Vec, } impl CollectLifetimes { pub fn new() -> Self { CollectLifetimes { elided: Vec::new(), explicit: Vec::new(), } } fn visit_opt_lifetime(&mut self, lifetime: &mut Option) { match lifetime { None => *lifetime = Some(self.next_lifetime()), Some(lifetime) => self.visit_lifetime(lifetime), } } fn visit_lifetime(&mut self, lifetime: &mut Lifetime) { if lifetime.ident == "_" { *lifetime = self.next_lifetime(); } else { self.explicit.push(lifetime.clone()); } } fn next_lifetime(&mut self) -> Lifetime { let name = format!("'life{}", self.elided.len()); let life = Lifetime::new(&name, Span::call_site()); self.elided.push(life.clone()); life } } impl VisitMut for CollectLifetimes { fn visit_receiver_mut(&mut self, arg: &mut Receiver) { if let Some((_, lifetime)) = &mut arg.reference { self.visit_opt_lifetime(lifetime); } } fn visit_type_reference_mut(&mut self, ty: &mut TypeReference) { self.visit_opt_lifetime(&mut ty.lifetime); visit_mut::visit_type_reference_mut(self, ty); } fn visit_generic_argument_mut(&mut self, gen: &mut GenericArgument) { if let GenericArgument::Lifetime(lifetime) = gen { self.visit_lifetime(lifetime); } visit_mut::visit_generic_argument_mut(self, gen); } } async-trait-0.1.24/src/parse.rs010066400017500001750000000023331355066477600145520ustar0000000000000000use proc_macro2::Span; use syn::parse::{Error, Parse, ParseStream, Result}; use syn::{Attribute, ItemImpl, ItemTrait, Token}; pub struct Nothing; impl Parse for Nothing { fn parse(_input: ParseStream) -> Result { Ok(Nothing) } } pub enum Item { Trait(ItemTrait), Impl(ItemImpl), } impl Parse for Item { fn parse(input: ParseStream) -> Result { let attrs = input.call(Attribute::parse_outer)?; let mut lookahead = input.lookahead1(); if lookahead.peek(Token![unsafe]) { let ahead = input.fork(); ahead.parse::()?; lookahead = ahead.lookahead1(); } if lookahead.peek(Token![pub]) || lookahead.peek(Token![trait]) { let mut item: ItemTrait = input.parse()?; item.attrs = attrs; Ok(Item::Trait(item)) } else if lookahead.peek(Token![impl]) { let mut item: ItemImpl = input.parse()?; if item.trait_.is_none() { return Err(Error::new(Span::call_site(), "expected a trait impl")); } item.attrs = attrs; Ok(Item::Impl(item)) } else { Err(lookahead.error()) } } } async-trait-0.1.24/src/receiver.rs010064400017500001750000000165061361617117000152310ustar0000000000000000use proc_macro2::{Group, TokenStream, TokenTree}; use std::iter::FromIterator; use std::mem; use syn::punctuated::Punctuated; use syn::visit_mut::{self, VisitMut}; use syn::{ parse_quote, Block, Error, ExprPath, ExprStruct, Ident, Item, Macro, Path, PathArguments, QSelf, Receiver, Signature, Type, TypePath, WherePredicate, }; pub fn has_self_in_sig(sig: &mut Signature) -> bool { let mut visitor = HasSelf(false); visitor.visit_signature_mut(sig); visitor.0 } pub fn has_self_in_where_predicate(where_predicate: &mut WherePredicate) -> bool { let mut visitor = HasSelf(false); visitor.visit_where_predicate_mut(where_predicate); visitor.0 } pub fn has_self_in_block(block: &mut Block) -> bool { let mut visitor = HasSelf(false); visitor.visit_block_mut(block); visitor.0 } struct HasSelf(bool); impl VisitMut for HasSelf { fn visit_expr_path_mut(&mut self, expr: &mut ExprPath) { self.0 |= expr.path.segments[0].ident == "Self"; visit_mut::visit_expr_path_mut(self, expr); } fn visit_type_path_mut(&mut self, ty: &mut TypePath) { self.0 |= ty.path.segments[0].ident == "Self"; visit_mut::visit_type_path_mut(self, ty); } fn visit_receiver_mut(&mut self, _arg: &mut Receiver) { self.0 = true; } fn visit_item_mut(&mut self, _: &mut Item) { // Do not recurse into nested items. } } pub struct ReplaceReceiver { pub with: Type, pub as_trait: Option, } impl ReplaceReceiver { pub fn with(ty: Type) -> Self { ReplaceReceiver { with: ty, as_trait: None, } } pub fn with_as_trait(ty: Type, as_trait: Path) -> Self { ReplaceReceiver { with: ty, as_trait: Some(as_trait), } } fn self_to_qself_type(&self, qself: &mut Option, path: &mut Path) { let include_as_trait = true; self.self_to_qself(qself, path, include_as_trait); } fn self_to_qself_expr(&self, qself: &mut Option, path: &mut Path) { let include_as_trait = false; self.self_to_qself(qself, path, include_as_trait); } fn self_to_qself(&self, qself: &mut Option, path: &mut Path, include_as_trait: bool) { if path.leading_colon.is_some() { return; } let first = &path.segments[0]; if first.ident != "Self" || !first.arguments.is_empty() { return; } if path.segments.len() == 1 { self.self_to_expr_path(path); return; } *qself = Some(QSelf { lt_token: Default::default(), ty: Box::new(self.with.clone()), position: 0, as_token: None, gt_token: Default::default(), }); if include_as_trait && self.as_trait.is_some() { let as_trait = self.as_trait.as_ref().unwrap().clone(); path.leading_colon = as_trait.leading_colon; qself.as_mut().unwrap().position = as_trait.segments.len(); let segments = mem::replace(&mut path.segments, as_trait.segments); path.segments.push_punct(Default::default()); path.segments.extend(segments.into_pairs().skip(1)); } else { path.leading_colon = Some(**path.segments.pairs().next().unwrap().punct().unwrap()); let segments = mem::replace(&mut path.segments, Punctuated::new()); path.segments = segments.into_pairs().skip(1).collect(); } } fn self_to_expr_path(&self, path: &mut Path) { if let Type::Path(with) = &self.with { *path = with.path.clone(); for segment in &mut path.segments { if let PathArguments::AngleBracketed(bracketed) = &mut segment.arguments { if bracketed.colon2_token.is_none() && !bracketed.args.is_empty() { bracketed.colon2_token = Some(Default::default()); } } } } else { let span = path.segments[0].ident.span(); let msg = "Self type of this impl is unsupported in expression position"; let error = Error::new(span, msg).to_compile_error(); *path = parse_quote!(::core::marker::PhantomData::<#error>); } } } impl VisitMut for ReplaceReceiver { // `Self` -> `Receiver` fn visit_type_mut(&mut self, ty: &mut Type) { if let Type::Path(node) = ty { if node.qself.is_none() && node.path.is_ident("Self") { *ty = self.with.clone(); } else { self.visit_type_path_mut(node); } } else { visit_mut::visit_type_mut(self, ty); } } // `Self::Assoc` -> `::Assoc` fn visit_type_path_mut(&mut self, ty: &mut TypePath) { if ty.qself.is_none() { self.self_to_qself_type(&mut ty.qself, &mut ty.path); } visit_mut::visit_type_path_mut(self, ty); } // `Self::method` -> `::method` fn visit_expr_path_mut(&mut self, expr: &mut ExprPath) { if expr.qself.is_none() { prepend_underscore_to_self(&mut expr.path.segments[0].ident); self.self_to_qself_expr(&mut expr.qself, &mut expr.path); } visit_mut::visit_expr_path_mut(self, expr); } fn visit_expr_struct_mut(&mut self, expr: &mut ExprStruct) { if expr.path.is_ident("Self") { self.self_to_expr_path(&mut expr.path); } visit_mut::visit_expr_struct_mut(self, expr); } fn visit_item_mut(&mut self, _: &mut Item) { // Do not recurse into nested items. } fn visit_macro_mut(&mut self, i: &mut Macro) { // We can't tell in general whether `self` inside a macro invocation // refers to the self in the argument list or a different self // introduced within the macro. Heuristic: if the macro input contains // `fn`, then `self` is more likely to refer to something other than the // outer function's self argument. if !contains_fn(i.tokens.clone()) { fold_token_stream(&mut i.tokens); } } } fn contains_fn(tokens: TokenStream) -> bool { tokens.into_iter().any(|tt| match tt { TokenTree::Ident(ident) => ident == "fn", TokenTree::Group(group) => contains_fn(group.stream()), _ => false, }) } fn fold_token_stream(tokens: &mut TokenStream) -> bool { let mut out = Vec::new(); let mut modified = false; for tt in tokens.clone() { match tt { TokenTree::Ident(mut ident) => { modified |= prepend_underscore_to_self(&mut ident); out.push(TokenTree::Ident(ident)); } TokenTree::Group(group) => { let mut content = group.stream(); modified |= fold_token_stream(&mut content); let mut new = Group::new(group.delimiter(), content); new.set_span(group.span()); out.push(TokenTree::Group(new)); } other => out.push(other), } } if modified { *tokens = TokenStream::from_iter(out); } modified } fn prepend_underscore_to_self(ident: &mut Ident) -> bool { let modified = ident == "self"; if modified { *ident = Ident::new("_self", ident.span()); } modified } async-trait-0.1.24/tests/compiletest.rs010064400017500001750000000002141356406111300163120ustar0000000000000000#[rustversion::attr(not(nightly), ignore)] #[test] fn ui() { let t = trybuild::TestCases::new(); t.compile_fail("tests/ui/*.rs"); } async-trait-0.1.24/tests/executor/mod.rs010066400017500001750000000020011353757541200164110ustar0000000000000000use std::future::Future; use std::pin::Pin; use std::ptr; use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker}; // Executor for a future that resolves immediately (test only). pub fn block_on_simple(mut fut: F) -> F::Output { unsafe fn clone(_null: *const ()) -> RawWaker { unimplemented!() } unsafe fn wake(_null: *const ()) { unimplemented!() } unsafe fn wake_by_ref(_null: *const ()) { unimplemented!() } unsafe fn drop(_null: *const ()) {} let data = ptr::null(); let vtable = &RawWakerVTable::new(clone, wake, wake_by_ref, drop); let raw_waker = RawWaker::new(data, vtable); let waker = unsafe { Waker::from_raw(raw_waker) }; let mut cx = Context::from_waker(&waker); // fut does not move until it gets dropped. let fut = unsafe { Pin::new_unchecked(&mut fut) }; match fut.poll(&mut cx) { Poll::Ready(output) => output, Poll::Pending => panic!("future did not resolve immediately"), } } async-trait-0.1.24/tests/test.rs010064400017500001750000000240171361265107000147510ustar0000000000000000#![cfg_attr(async_trait_nightly_testing, feature(specialization, const_generics))] use async_trait::async_trait; pub mod executor; // Dummy module to check that the expansion refer to rust's core crate mod core {} #[async_trait] trait Trait { type Assoc; async fn selfvalue(self) where Self: Sized, { } async fn selfref(&self) {} async fn selfmut(&mut self) {} async fn required() -> Self::Assoc; async fn elided_lifetime(_x: &str) {} async fn explicit_lifetime<'a>(_x: &'a str) {} async fn generic_type_param(x: Box) -> T { *x } async fn calls(&self) { self.selfref().await; Self::elided_lifetime("").await; ::elided_lifetime("").await; } async fn calls_mut(&mut self) { self.selfmut().await; } } struct Struct; #[async_trait] impl Trait for Struct { type Assoc = (); async fn selfvalue(self) {} async fn selfref(&self) {} async fn selfmut(&mut self) {} async fn required() -> Self::Assoc {} async fn elided_lifetime(_x: &str) {} async fn explicit_lifetime<'a>(_x: &'a str) {} async fn generic_type_param(x: Box) -> T { *x } async fn calls(&self) { self.selfref().await; Self::elided_lifetime("").await; ::elided_lifetime("").await; } async fn calls_mut(&mut self) { self.selfmut().await; } } pub async fn test() { let mut s = Struct; s.selfref().await; s.selfmut().await; s.selfvalue().await; Struct::required().await; Struct::elided_lifetime("").await; Struct::explicit_lifetime("").await; Struct::generic_type_param(Box::new("")).await; let mut s = Struct; s.calls().await; s.calls_mut().await; } pub async fn test_object_safe_without_default() { #[async_trait] trait ObjectSafe { async fn f(&self); } #[async_trait] impl ObjectSafe for Struct { async fn f(&self) {} } let object = &Struct as &dyn ObjectSafe; object.f().await; } pub async fn test_object_safe_with_default() { #[async_trait] trait ObjectSafe: Sync { async fn f(&self) {} } #[async_trait] impl ObjectSafe for Struct { async fn f(&self) {} } let object = &Struct as &dyn ObjectSafe; object.f().await; } pub async fn test_object_no_send() { #[async_trait(?Send)] trait ObjectSafe: Sync { async fn f(&self) {} } #[async_trait(?Send)] impl ObjectSafe for Struct { async fn f(&self) {} } let object = &Struct as &dyn ObjectSafe; object.f().await; } #[async_trait] pub unsafe trait UnsafeTrait {} #[async_trait] unsafe impl UnsafeTrait for () {} #[async_trait] pub(crate) unsafe trait UnsafeTraitPubCrate {} #[async_trait] unsafe trait UnsafeTraitPrivate {} // https://github.com/dtolnay/async-trait/issues/1 pub mod issue1 { use async_trait::async_trait; #[async_trait] trait Issue1 { async fn f(&self); } #[async_trait] impl Issue1 for Vec { async fn f(&self) {} } } // https://github.com/dtolnay/async-trait/issues/2 pub mod issue2 { use async_trait::async_trait; use std::future::Future; #[async_trait] pub trait Issue2: Future { async fn flatten(self) -> ::Output where Self::Output: Future + Send, Self: Sized, { let nested_future = self.await; nested_future.await } } } // https://github.com/dtolnay/async-trait/issues/9 pub mod issue9 { use async_trait::async_trait; #[async_trait] pub trait Issue9: Sized + Send { async fn f(_x: Self) {} } } // https://github.com/dtolnay/async-trait/issues/11 pub mod issue11 { use async_trait::async_trait; use std::sync::Arc; #[async_trait] trait Issue11 { async fn example(self: Arc); } struct Struct; #[async_trait] impl Issue11 for Struct { async fn example(self: Arc) {} } } // https://github.com/dtolnay/async-trait/issues/15 pub mod issue15 { use async_trait::async_trait; use std::marker::PhantomData; trait Trait {} #[async_trait] trait Issue15 { async fn myfn(&self, _: PhantomData) {} } } // https://github.com/dtolnay/async-trait/issues/17 pub mod issue17 { use async_trait::async_trait; #[async_trait] trait Issue17 { async fn f(&self); } struct Struct { string: String, } #[async_trait] impl Issue17 for Struct { async fn f(&self) { println!("{}", self.string); } } } // https://github.com/dtolnay/async-trait/issues/23 pub mod issue23 { use async_trait::async_trait; #[async_trait] pub trait Issue23 { async fn f(self); async fn g(mut self) where Self: Sized, { do_something(&mut self); } } struct S {} #[async_trait] impl Issue23 for S { async fn f(mut self) { do_something(&mut self); } } fn do_something(_: &mut T) {} } // https://github.com/dtolnay/async-trait/issues/25 #[cfg(async_trait_nightly_testing)] pub mod issue25 { use crate::executor; use async_trait::async_trait; use std::fmt::{Display, Write}; #[async_trait] trait AsyncToString { async fn async_to_string(&self) -> String; } #[async_trait] impl AsyncToString for String { async fn async_to_string(&self) -> String { "special".to_owned() } } macro_rules! hide_from_stable_parser { ($($tt:tt)*) => { $($tt)* }; } hide_from_stable_parser! { #[async_trait] impl AsyncToString for T { default async fn async_to_string(&self) -> String { let mut buf = String::new(); buf.write_fmt(format_args!("{}", self)).unwrap(); buf } } } #[test] fn test() { let fut = true.async_to_string(); assert_eq!(executor::block_on_simple(fut), "true"); let string = String::new(); let fut = string.async_to_string(); assert_eq!(executor::block_on_simple(fut), "special"); } } // https://github.com/dtolnay/async-trait/issues/28 pub mod issue28 { use async_trait::async_trait; struct Str<'a>(&'a str); #[async_trait] trait Trait1<'a> { async fn f(x: Str<'a>) -> &'a str; async fn g(x: Str<'a>) -> &'a str { x.0 } } #[async_trait] impl<'a> Trait1<'a> for str { async fn f(x: Str<'a>) -> &'a str { x.0 } } #[async_trait] trait Trait2 { async fn f(); } #[async_trait] impl<'a> Trait2 for &'a () { async fn f() {} } #[async_trait] trait Trait3<'a, 'b> { async fn f(_: &'a &'b ()); // chain 'a and 'b async fn g(_: &'b ()); // chain 'b only async fn h(); // do not chain } } // https://github.com/dtolnay/async-trait/issues/31 pub mod issue31 { use async_trait::async_trait; pub struct Struct<'a> { pub name: &'a str, } #[async_trait] pub trait Trait<'a> { async fn hello(thing: Struct<'a>) -> String; async fn hello_twice(one: Struct<'a>, two: Struct<'a>) -> String { let str1 = Self::hello(one).await; let str2 = Self::hello(two).await; str1 + &str2 } } } // https://github.com/dtolnay/async-trait/issues/42 pub mod issue42 { use async_trait::async_trait; #[async_trait] pub trait Context: Sized { async fn from_parts() -> Self; } pub struct TokenContext; #[async_trait] impl Context for TokenContext { async fn from_parts() -> TokenContext { TokenContext } } } // https://github.com/dtolnay/async-trait/issues/44 pub mod issue44 { use async_trait::async_trait; #[async_trait] pub trait StaticWithWhereSelf where Box: Sized, Self: Sized + Send, { async fn get_one() -> u8 { 1 } } pub struct Struct; #[async_trait] impl StaticWithWhereSelf for Struct {} } // https://github.com/dtolnay/async-trait/issues/46 pub mod issue46 { use async_trait::async_trait; macro_rules! implement_commands { ($tyargs:tt : $ty:tt) => { #[async_trait] pub trait AsyncCommands: Sized { async fn f<$tyargs: $ty>(&mut self, x: $tyargs) { self.f(x).await } } }; } implement_commands!(K: Send); } // https://github.com/dtolnay/async-trait/issues/53 pub mod issue53 { use async_trait::async_trait; pub struct Unit; pub struct Tuple(u8); pub struct Struct { pub x: u8, } #[async_trait] pub trait Trait { async fn method(); } #[async_trait] impl Trait for Unit { async fn method() { let _ = Self; } } #[async_trait] impl Trait for Tuple { async fn method() { let _ = Self(0); } } #[async_trait] impl Trait for Struct { async fn method() { let _ = Self { x: 0 }; } } #[async_trait] impl Trait for std::marker::PhantomData { async fn method() { let _ = Self; } } } // https://github.com/dtolnay/async-trait/issues/57 #[cfg(async_trait_nightly_testing)] pub mod issue57 { use crate::executor; use async_trait::async_trait; #[async_trait] trait Trait { async fn const_generic(_: [T; C]) {} } struct Struct; #[async_trait] impl Trait for Struct { async fn const_generic(_: [T; C]) {} } #[test] fn test() { let fut = Struct::const_generic([0; 10]); executor::block_on_simple(fut); } } async-trait-0.1.24/tests/ui/delimiter-span.rs010064400017500001750000000003771361617117000173310ustar0000000000000000use async_trait::async_trait; macro_rules! picky { (ident) => {}; } #[async_trait] trait Trait { async fn method(); } struct Struct; #[async_trait] impl Trait for Struct { async fn method() { picky!({ 123 }); } } fn main() {} async-trait-0.1.24/tests/ui/delimiter-span.stderr010064400017500001750000000003741361617117000202050ustar0000000000000000error: no rules expected the token `{` --> $DIR/delimiter-span.rs:17:16 | 3 | macro_rules! picky { | ------------------ when calling this macro ... 17 | picky!({ 123 }); | ^ no rules expected this token in macro call async-trait-0.1.24/tests/ui/send-not-implemented.rs010064400017500001750000000003661356406125200204430ustar0000000000000000use async_trait::async_trait; use std::sync::Mutex; async fn f() {} #[async_trait] trait Test { async fn test(&self) { let mutex = Mutex::new(()); let _guard = mutex.lock().unwrap(); f().await; } } fn main() {} async-trait-0.1.24/tests/ui/send-not-implemented.stderr010064400017500001750000000017161357465766600213470ustar0000000000000000error: future cannot be sent between threads safely --> $DIR/send-not-implemented.rs:8:26 | 8 | async fn test(&self) { | __________________________^ 9 | | let mutex = Mutex::new(()); 10 | | let _guard = mutex.lock().unwrap(); 11 | | f().await; 12 | | } | |_____^ future returned by `__test` is not `Send` | = help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, ()>` note: future is not `Send` as this value is used across an await --> $DIR/send-not-implemented.rs:11:9 | 10 | let _guard = mutex.lock().unwrap(); | ------ has type `std::sync::MutexGuard<'_, ()>` 11 | f().await; | ^^^^^^^^^ await occurs here, with `_guard` maybe used later 12 | } | - `_guard` is later dropped here = note: required for the cast to the object type `dyn std::future::Future + std::marker::Send` async-trait-0.1.24/tests/ui/unsupported-self.rs010064400017500001750000000003131357170143000177170ustar0000000000000000use async_trait::async_trait; #[async_trait] pub trait Trait { async fn method(); } #[async_trait] impl Trait for &'static str { async fn method() { let _ = Self; } } fn main() {} async-trait-0.1.24/tests/ui/unsupported-self.stderr010064400017500001750000000002431357170143000206000ustar0000000000000000error: Self type of this impl is unsupported in expression position --> $DIR/unsupported-self.rs:11:17 | 11 | let _ = Self; | ^^^^ async-trait-0.1.24/.cargo_vcs_info.json0000644000000001121361621560700134550ustar00{ "git": { "sha1": "fe1f0ee75dec3bcea621cf31d26a26cd58115705" } }