Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
use syn::parse::{Parse, ParseStream, Result};
use proc_macro2::Span;
use syn::parse::{Error, Parse, ParseStream, Result};
use syn::Token;

#[derive(Copy, Clone)]
pub struct Args {
pub local: bool
pub local: bool,
}

mod kw {
syn::custom_keyword!(local);
syn::custom_keyword!(Send);
}

impl Parse for Args {
fn parse(input: ParseStream) -> Result<Self> {
let local: Option<kw::local> = input.parse()?;
Ok(Args {
local: local.is_some(),
})
match try_parse(input) {
Ok(args) if input.is_empty() => Ok(args),
_ => Err(error()),
}
}
}

fn try_parse(input: ParseStream) -> Result<Args> {
if input.peek(Token![?]) {
input.parse::<Token![?]>()?;
input.parse::<kw::Send>()?;
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)
}
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ pub async fn test_object_safe_with_default() {
}

pub async fn test_object_no_send() {
#[async_trait(local)]
#[async_trait(?Send)]
trait ObjectSafe: Sync {
async fn f(&self) {}
}

#[async_trait(local)]
#[async_trait(?Send)]
impl ObjectSafe for Struct {
async fn f(&self) {}
}
Expand Down