Skip to content

Commit

Permalink
Begin development on async fn support
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 19, 2022
1 parent 97d7d53 commit e41be2f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions gen/build/Cargo.toml
Expand Up @@ -14,6 +14,8 @@ categories = ["development-tools::ffi"]

[features]
parallel = ["cc/parallel"]
# incomplete features that are not covered by a compatibility guarantee:
experimental-async-fn = []

[dependencies]
cc = "1.0.49"
Expand Down
4 changes: 4 additions & 0 deletions gen/cmd/Cargo.toml
Expand Up @@ -16,6 +16,10 @@ categories = ["development-tools::ffi"]
name = "cxxbridge"
path = "src/main.rs"

[features]
# incomplete features that are not covered by a compatibility guarantee:
experimental-async-fn = []

[dependencies]
clap = { version = "3.0", default-features = false, features = ["std", "suggestions"] }
codespan-reporting = "0.11"
Expand Down
4 changes: 3 additions & 1 deletion macro/Cargo.toml
Expand Up @@ -16,14 +16,16 @@ categories = ["development-tools::ffi"]
proc-macro = true

[features]
# incomplete features that are not covered by a compatibility guarantee:
experimental-async-fn = []
experimental-enum-variants-from-header = ["clang-ast", "flate2", "memmap", "serde", "serde_json"]

[dependencies]
proc-macro2 = "1.0"
quote = "1.0.4"
syn = { version = "1.0.70", features = ["full"] }

# optional dependencies
# optional dependencies:
clang-ast = { version = "0.1", optional = true }
flate2 = { version = "1.0", optional = true }
memmap = { version = "0.7", optional = true }
Expand Down
7 changes: 6 additions & 1 deletion syntax/impls.rs
Expand Up @@ -309,6 +309,7 @@ impl Eq for Signature {}
impl PartialEq for Signature {
fn eq(&self, other: &Self) -> bool {
let Signature {
asyncness,
unsafety,
fn_token: _,
generics: _,
Expand All @@ -320,6 +321,7 @@ impl PartialEq for Signature {
throws_tokens: _,
} = self;
let Signature {
asyncness: asyncness2,
unsafety: unsafety2,
fn_token: _,
generics: _,
Expand All @@ -330,7 +332,8 @@ impl PartialEq for Signature {
paren_token: _,
throws_tokens: _,
} = other;
unsafety.is_some() == unsafety2.is_some()
asyncness.is_some() == asyncness2.is_some()
&& unsafety.is_some() == unsafety2.is_some()
&& receiver == receiver2
&& ret == ret2
&& throws == throws2
Expand Down Expand Up @@ -362,6 +365,7 @@ impl PartialEq for Signature {
impl Hash for Signature {
fn hash<H: Hasher>(&self, state: &mut H) {
let Signature {
asyncness,
unsafety,
fn_token: _,
generics: _,
Expand All @@ -372,6 +376,7 @@ impl Hash for Signature {
paren_token: _,
throws_tokens: _,
} = self;
asyncness.is_some().hash(state);
unsafety.is_some().hash(state);
receiver.hash(state);
for arg in args {
Expand Down
1 change: 1 addition & 0 deletions syntax/mod.rs
Expand Up @@ -179,6 +179,7 @@ pub struct Lifetimes {
}

pub struct Signature {
pub asyncness: Option<Token![async]>,
pub unsafety: Option<Token![unsafe]>,
pub fn_token: Token![fn],
pub generics: Generics,
Expand Down
6 changes: 5 additions & 1 deletion syntax/parse.rs
Expand Up @@ -562,7 +562,7 @@ fn parse_extern_fn(
));
}

if foreign_fn.sig.asyncness.is_some() {
if foreign_fn.sig.asyncness.is_some() && !cfg!(feature = "experimental-async-fn") {
return Err(Error::new_spanned(
foreign_fn,
"async function is not directly supported yet, but see https://cxx.rs/async.html \
Expand Down Expand Up @@ -664,6 +664,7 @@ fn parse_extern_fn(
let mut throws_tokens = None;
let ret = parse_return_type(&foreign_fn.sig.output, &mut throws_tokens)?;
let throws = throws_tokens.is_some();
let asyncness = foreign_fn.sig.asyncness;
let unsafety = foreign_fn.sig.unsafety;
let fn_token = foreign_fn.sig.fn_token;
let inherited_span = unsafety.map_or(fn_token.span, |unsafety| unsafety.span);
Expand All @@ -684,6 +685,7 @@ fn parse_extern_fn(
visibility,
name,
sig: Signature {
asyncness,
unsafety,
fn_token,
generics,
Expand Down Expand Up @@ -1400,13 +1402,15 @@ fn parse_type_fn(ty: &TypeBareFn) -> Result<Type> {
let ret = parse_return_type(&ty.output, &mut throws_tokens)?;
let throws = throws_tokens.is_some();

let asyncness = None;
let unsafety = ty.unsafety;
let fn_token = ty.fn_token;
let generics = Generics::default();
let receiver = None;
let paren_token = ty.paren_token;

Ok(Type::Fn(Box::new(Signature {
asyncness,
unsafety,
fn_token,
generics,
Expand Down
1 change: 1 addition & 0 deletions syntax/tokens.rs
Expand Up @@ -248,6 +248,7 @@ impl ToTokens for Lifetimes {
impl ToTokens for Signature {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Signature {
asyncness: _,
unsafety: _,
fn_token,
generics: _,
Expand Down

0 comments on commit e41be2f

Please sign in to comment.