Skip to content

Commit e41be2f

Browse files
committed
Begin development on async fn support
1 parent 97d7d53 commit e41be2f

File tree

7 files changed

+22
-3
lines changed

7 files changed

+22
-3
lines changed

gen/build/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ categories = ["development-tools::ffi"]
1414

1515
[features]
1616
parallel = ["cc/parallel"]
17+
# incomplete features that are not covered by a compatibility guarantee:
18+
experimental-async-fn = []
1719

1820
[dependencies]
1921
cc = "1.0.49"

gen/cmd/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ categories = ["development-tools::ffi"]
1616
name = "cxxbridge"
1717
path = "src/main.rs"
1818

19+
[features]
20+
# incomplete features that are not covered by a compatibility guarantee:
21+
experimental-async-fn = []
22+
1923
[dependencies]
2024
clap = { version = "3.0", default-features = false, features = ["std", "suggestions"] }
2125
codespan-reporting = "0.11"

macro/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ categories = ["development-tools::ffi"]
1616
proc-macro = true
1717

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

2123
[dependencies]
2224
proc-macro2 = "1.0"
2325
quote = "1.0.4"
2426
syn = { version = "1.0.70", features = ["full"] }
2527

26-
# optional dependencies
28+
# optional dependencies:
2729
clang-ast = { version = "0.1", optional = true }
2830
flate2 = { version = "1.0", optional = true }
2931
memmap = { version = "0.7", optional = true }

syntax/impls.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ impl Eq for Signature {}
309309
impl PartialEq for Signature {
310310
fn eq(&self, other: &Self) -> bool {
311311
let Signature {
312+
asyncness,
312313
unsafety,
313314
fn_token: _,
314315
generics: _,
@@ -320,6 +321,7 @@ impl PartialEq for Signature {
320321
throws_tokens: _,
321322
} = self;
322323
let Signature {
324+
asyncness: asyncness2,
323325
unsafety: unsafety2,
324326
fn_token: _,
325327
generics: _,
@@ -330,7 +332,8 @@ impl PartialEq for Signature {
330332
paren_token: _,
331333
throws_tokens: _,
332334
} = other;
333-
unsafety.is_some() == unsafety2.is_some()
335+
asyncness.is_some() == asyncness2.is_some()
336+
&& unsafety.is_some() == unsafety2.is_some()
334337
&& receiver == receiver2
335338
&& ret == ret2
336339
&& throws == throws2
@@ -362,6 +365,7 @@ impl PartialEq for Signature {
362365
impl Hash for Signature {
363366
fn hash<H: Hasher>(&self, state: &mut H) {
364367
let Signature {
368+
asyncness,
365369
unsafety,
366370
fn_token: _,
367371
generics: _,
@@ -372,6 +376,7 @@ impl Hash for Signature {
372376
paren_token: _,
373377
throws_tokens: _,
374378
} = self;
379+
asyncness.is_some().hash(state);
375380
unsafety.is_some().hash(state);
376381
receiver.hash(state);
377382
for arg in args {

syntax/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub struct Lifetimes {
179179
}
180180

181181
pub struct Signature {
182+
pub asyncness: Option<Token![async]>,
182183
pub unsafety: Option<Token![unsafe]>,
183184
pub fn_token: Token![fn],
184185
pub generics: Generics,

syntax/parse.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ fn parse_extern_fn(
562562
));
563563
}
564564

565-
if foreign_fn.sig.asyncness.is_some() {
565+
if foreign_fn.sig.asyncness.is_some() && !cfg!(feature = "experimental-async-fn") {
566566
return Err(Error::new_spanned(
567567
foreign_fn,
568568
"async function is not directly supported yet, but see https://cxx.rs/async.html \
@@ -664,6 +664,7 @@ fn parse_extern_fn(
664664
let mut throws_tokens = None;
665665
let ret = parse_return_type(&foreign_fn.sig.output, &mut throws_tokens)?;
666666
let throws = throws_tokens.is_some();
667+
let asyncness = foreign_fn.sig.asyncness;
667668
let unsafety = foreign_fn.sig.unsafety;
668669
let fn_token = foreign_fn.sig.fn_token;
669670
let inherited_span = unsafety.map_or(fn_token.span, |unsafety| unsafety.span);
@@ -684,6 +685,7 @@ fn parse_extern_fn(
684685
visibility,
685686
name,
686687
sig: Signature {
688+
asyncness,
687689
unsafety,
688690
fn_token,
689691
generics,
@@ -1400,13 +1402,15 @@ fn parse_type_fn(ty: &TypeBareFn) -> Result<Type> {
14001402
let ret = parse_return_type(&ty.output, &mut throws_tokens)?;
14011403
let throws = throws_tokens.is_some();
14021404

1405+
let asyncness = None;
14031406
let unsafety = ty.unsafety;
14041407
let fn_token = ty.fn_token;
14051408
let generics = Generics::default();
14061409
let receiver = None;
14071410
let paren_token = ty.paren_token;
14081411

14091412
Ok(Type::Fn(Box::new(Signature {
1413+
asyncness,
14101414
unsafety,
14111415
fn_token,
14121416
generics,

syntax/tokens.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl ToTokens for Lifetimes {
248248
impl ToTokens for Signature {
249249
fn to_tokens(&self, tokens: &mut TokenStream) {
250250
let Signature {
251+
asyncness: _,
251252
unsafety: _,
252253
fn_token,
253254
generics: _,

0 commit comments

Comments
 (0)