1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under both the MIT license found in the
 * LICENSE-MIT file in the root directory of this source tree and the Apache
 * License, Version 2.0 found in the LICENSE-APACHE file in the root directory
 * of this source tree.
 */

// cxx-async/macro/src/lib.rs
//
//! The definition of the `#[bridge]` macro.
//!
//! Don't depend on this crate directly; just use the reexported macro in `cxx-async`.

use proc_macro::TokenStream;
use quote::quote;
use syn::parse::Parse;
use syn::parse::ParseStream;
use syn::parse::Result as ParseResult;
use syn::spanned::Spanned;
use syn::Error as SynError;
use syn::Ident;
use syn::ImplItem;
use syn::ItemImpl;
use syn::Lit;
use syn::LitStr;
use syn::Path;
use syn::Result as SynResult;
use syn::Token;
use syn::Type;
use syn::TypePath;
use syn::Visibility;

/// Defines a future or stream type that can be awaited from both Rust and C++.
///
/// Invoke this macro like so:
///
/// ```ignore
/// #[cxx_async::bridge]
/// unsafe impl Future for RustFutureString {
///     type Output = String;
/// }
/// ```
///
/// Here, `RustFutureString` is the name of the future type declared inside the
/// `extern Rust { ... }` block inside the `#[cxx::bridge]` module. `String` is the Rust type that
/// the future yields when awaited; on the Rust side it will be automatically wrapped in a
/// `CxxAsyncResult`. On the C++ side it will be converted to the appropriate type, following the
/// `cxx` rules. Err returns are translated into C++ exceptions.
///
/// If the future is inside a C++ namespace, add a `namespace = ...` attribute to the
/// `#[cxx_async::bridge]` attribute like so:
///
/// ```ignore
/// #[cxx::bridge]
/// #[namespace = mycompany::myproject]
/// mod ffi {
///     extern "Rust" {
///         type RustFutureStringNamespaced;
///     }
/// }
///
/// #[cxx_async::bridge(namespace = mycompany::myproject)]
/// unsafe impl Future for RustFutureStringNamespaced {
///     type Output = String;
/// }
/// ```
///
/// ## Safety
///
/// It's the programmer's responsibility to ensure that the specified `Output` type correctly
/// reflects the type of the value that any returned C++ future resolves to. `cxx_async` can't
/// currently check to ensure that these types match. If the types don't match, undefined behavior
/// can result. See the [cxx documentation] for information on the mapping between Rust types and
/// C++ types.
///
/// [cxx documentation]: https://cxx.rs/bindings.html
#[proc_macro_attribute]
pub fn bridge(attribute: TokenStream, item: TokenStream) -> TokenStream {
    match AstPieces::from_token_streams(attribute, item) {
        Err(error) => error.into_compile_error().into(),
        Ok(
            pieces @ AstPieces {
                bridge_trait: BridgeTrait::Future,
                ..
            },
        ) => bridge_future(pieces),
        Ok(
            pieces @ AstPieces {
                bridge_trait: BridgeTrait::Stream,
                ..
            },
        ) => bridge_stream(pieces),
    }
}

fn bridge_future(pieces: AstPieces) -> TokenStream {
    let AstPieces {
        bridge_trait: _,
        future,
        qualified_name,
        output,
        trait_path,
        vtable_glue_ident,
        vtable_glue_link_name,
    } = pieces;
    (quote! {
        /// A future shared between Rust and C++.
        #[repr(transparent)]
        pub struct #future {
            future: ::cxx_async::private::BoxFuture<'static, ::cxx_async::CxxAsyncResult<#output>>,
        }

        impl #future {
            // SAFETY: See: https://docs.rs/pin-utils/0.1.0/pin_utils/macro.unsafe_pinned.html
            // 1. The struct doesn't move fields in Drop. Our hogging the Drop implementation
            //    ensures this.
            // 2. The inner field implements Unpin. We ensure this in the `assert_field_is_unpin`
            //    method.
            // 3. The struct isn't `repr(packed)`. We define the struct and don't have this
            //    attribute.
            ::cxx_async::unsafe_pinned!(future: ::cxx_async::private::BoxFuture<'static,
                ::cxx_async::CxxAsyncResult<#output>>);

            #[doc(hidden)]
            fn assert_field_is_unpin() {
                fn check<T>() where T: Unpin {}
                check::<#output>()
            }
        }

        // Define a Drop implementation so that end users don't. If end users are allowed to define
        // Drop, that could make our use of `unsafe_pinned!` unsafe.
        impl Drop for #future {
            fn drop(&mut self) {}
        }

        // Define how to box up a future.
        impl ::cxx_async::IntoCxxAsyncFuture for #future {
            type Output = #output;
            fn fallible<Fut>(future: Fut) -> Self where Fut: ::std::future::Future<Output =
                    ::cxx_async::CxxAsyncResult<#output>> + Send + 'static {
                #future {
                    future: Box::pin(future),
                }
            }
        }

        // Implement the Rust Future trait.
        impl #trait_path for #future {
            type Output = ::cxx_async::CxxAsyncResult<#output>;
            fn poll(self: ::std::pin::Pin<&mut Self>, cx: &mut ::std::task::Context<'_>)
                    -> ::std::task::Poll<Self::Output> {
                self.future().poll(cx)
            }
        }

        // Define how to wrap concrete receivers in the future type we're defining.
        impl ::std::convert::From<::cxx_async::CxxAsyncReceiver<#output>> for #future {
            fn from(receiver: ::cxx_async::CxxAsyncReceiver<#output>) -> Self {
                Self {
                    future: Box::pin(receiver),
                }
            }
        }

        // Make sure that the future type can be returned by value.
        // See: https://github.com/dtolnay/cxx/pull/672
        unsafe impl ::cxx::ExternType for #future {
            type Id = ::cxx::type_id!(#qualified_name);
            type Kind = ::cxx::kind::Trivial;
        }

        // Convenience wrappers so that client code doesn't have to import `IntoCxxAsyncFuture`.
        impl #future {
            pub fn infallible<Fut>(future: Fut) -> Self
                    where Fut: ::std::future::Future<Output = #output> + Send + 'static {
                <#future as ::cxx_async::IntoCxxAsyncFuture>::infallible(future)
            }

            pub fn fallible<Fut>(future: Fut) -> Self
                    where Fut: ::std::future::Future<Output =
                        ::cxx_async::CxxAsyncResult<#output>> + Send + 'static {
                <#future as ::cxx_async::IntoCxxAsyncFuture>::fallible(future)
            }
        }

        #[doc(hidden)]
        #[allow(non_snake_case)]
        #[export_name = #vtable_glue_link_name]
        pub unsafe extern "C" fn #vtable_glue_ident() -> *const ::cxx_async::CxxAsyncVtable {
            static VTABLE: ::cxx_async::CxxAsyncVtable = ::cxx_async::CxxAsyncVtable {
                channel: ::cxx_async::future_channel::<#future, #output> as *mut u8,
                sender_send: ::cxx_async::sender_future_send::<#output> as *mut u8,
                sender_drop: ::cxx_async::sender_drop::<#output> as *mut u8,
                future_poll: ::cxx_async::future_poll::<#future, #output> as *mut u8,
                future_drop: ::cxx_async::future_drop::<#future> as *mut u8,
            };
            return &VTABLE;
        }
    })
    .into()
}

/// Defines a C++ stream type that can be awaited from Rust.
///
/// The syntax to use is:
///
/// ```ignore
/// #[cxx_async::bridge_stream]
/// unsafe impl Stream for RustStreamString {
///     type Item = String;
/// }
/// ```
///
/// Here, `RustStreamString` is the name of the stream type declared inside the
/// `extern Rust { ... }` block inside the `#[cxx::bridge]` module. `String` is the Rust type that
/// the stream yields when consumed; on the Rust side it will be automatically wrapped in a
/// `CxxAsyncResult`. On the C++ side it will be converted to the appropriate type, following the
/// `cxx` rules. Err returns are translated into C++ exceptions.
///
/// If the stream is inside a C++ namespace, add a `namespace = ...` attribute to the
/// `#[cxx_async::bridge_stream]` attribute like so:
///
/// ```ignore
/// #[cxx::bridge]
/// #[namespace = mycompany::myproject]
/// mod ffi {
///     extern "Rust" {
///         type RustStreamStringNamespaced;
///     }
/// }
///
/// #[cxx_async::bridge_stream(namespace = mycompany::myproject)]
/// unsafe impl Stream for RustStreamStringNamespaced {
///     type Item = String;
/// }
/// ```
///
/// ## Safety
///
/// It's the programmer's responsibility to ensure that the specified `Item` type correctly
/// reflects the type of the values that any returned C++ streams resolves to. `cxx_async` can't
/// currently check to ensure that these types match. If the types don't match, undefined behavior
/// can result. See the [cxx documentation] for information on the mapping between Rust types and
/// C++ types.
///
/// [cxx documentation]: https://cxx.rs/bindings.html
fn bridge_stream(pieces: AstPieces) -> TokenStream {
    let AstPieces {
        bridge_trait: _,
        future: stream,
        qualified_name,
        output: item,
        trait_path,
        vtable_glue_ident,
        vtable_glue_link_name,
    } = pieces;
    (quote! {
        /// A multi-shot stream shared between Rust and C++.
        #[repr(transparent)]
        pub struct #stream {
            stream: ::cxx_async::private::BoxStream<'static, ::cxx_async::CxxAsyncResult<#item>>,
        }

        impl #stream {
            // SAFETY: See: https://docs.rs/pin-utils/0.1.0/pin_utils/macro.unsafe_pinned.html
            // 1. The struct doesn't move fields in Drop. Our hogging the Drop implementation
            //    ensures this.
            // 2. The inner field implements Unpin. We ensure this in the `assert_field_is_unpin`
            //    method.
            // 3. The struct isn't `repr(packed)`. We define the struct and don't have this
            //    attribute.
            ::cxx_async::unsafe_pinned!(stream: ::cxx_async::private::BoxStream<'static,
                ::cxx_async::CxxAsyncResult<#item>>);

            #[doc(hidden)]
            fn assert_field_is_unpin() {
                fn check<T>() where T: Unpin {}
                check::<#item>()
            }
        }

        // Define a Drop implementation so that end users don't. If end users are allowed to define
        // Drop, that could make our use of `unsafe_pinned!` unsafe.
        impl Drop for #stream {
            fn drop(&mut self) {}
        }

        // Define how to box up a future.
        impl ::cxx_async::IntoCxxAsyncStream for #stream {
            type Item = #item;
            fn fallible<Stm>(stream: Stm) -> Self where Stm: #trait_path<Item =
                    ::cxx_async::CxxAsyncResult<#item>> + Send + 'static {
                #stream {
                    stream: Box::pin(stream),
                }
            }
        }

        // Implement the Rust Stream trait.
        impl #trait_path for #stream {
            type Item = ::cxx_async::CxxAsyncResult<#item>;
            fn poll_next(mut self: ::std::pin::Pin<&mut Self>, cx: &mut ::std::task::Context<'_>)
                    -> ::std::task::Poll<Option<Self::Item>> {
                ::std::pin::Pin::new(&mut self.stream).poll_next(cx)
            }
        }

        // Define how to wrap concrete receivers in the stream type we're defining.
        impl ::std::convert::From<::cxx_async::CxxAsyncReceiver<#item>> for #stream {
            fn from(receiver: ::cxx_async::CxxAsyncReceiver<#item>) -> Self {
                Self {
                    stream: Box::pin(receiver),
                }
            }
        }

        // Make sure that the stream type can be returned by value.
        // See: https://github.com/dtolnay/cxx/pull/672
        unsafe impl ::cxx::ExternType for #stream {
            type Id = ::cxx::type_id!(#qualified_name);
            type Kind = ::cxx::kind::Trivial;
        }

        // Convenience wrappers so that client code doesn't have to import `IntoCxxAsyncFuture`.
        impl #stream {
            pub fn infallible<Stm>(stream: Stm) -> Self
                    where Stm: #trait_path<Item = #item> + Send + 'static {
                <#stream as ::cxx_async::IntoCxxAsyncStream>::infallible(stream)
            }

            pub fn fallible<Stm>(stream: Stm) -> Self
                    where Stm: #trait_path<Item =
                        ::cxx_async::CxxAsyncResult<#item>> + Send + 'static {
                <#stream as ::cxx_async::IntoCxxAsyncStream>::fallible(stream)
            }
        }

        #[doc(hidden)]
        #[allow(non_snake_case)]
        #[export_name = #vtable_glue_link_name]
        pub unsafe extern "C" fn #vtable_glue_ident() -> *const ::cxx_async::CxxAsyncVtable {
            // TODO(pcwalton): Support C++ calling Rust Streams.
            static VTABLE: ::cxx_async::CxxAsyncVtable = ::cxx_async::CxxAsyncVtable {
                channel: ::cxx_async::stream_channel::<#stream, #item> as *mut u8,
                sender_send: ::cxx_async::sender_stream_send::<#item> as *mut u8,
                sender_drop: ::cxx_async::sender_drop::<#item> as *mut u8,
                future_poll: ::std::ptr::null_mut(),
                future_drop: ::cxx_async::future_drop::<#stream> as *mut u8,
            };
            return &VTABLE;
        }
    })
    .into()
}

// Whether the programmer is implementing `Future` or `Stream`.
enum BridgeTrait {
    Future,
    Stream,
}

// AST pieces generated for the `#[bridge]` macro.
struct AstPieces {
    // Whether the programmer is implementing `Future` or `Stream`.
    bridge_trait: BridgeTrait,
    // The name of the future or stream type.
    future: Ident,
    // The fully-qualified name (i.e. including C++ namespace if any) of the future or stream type,
    // as a quoted string.
    qualified_name: Lit,
    // The output type of the future or the item type of the stream.
    output: Type,
    // The path to the trait being implemented, which must be `std::future::Future` or
    // `futures::Stream`.
    trait_path: Path,
    // The internal Rust symbol name of the future/stream vtable.
    vtable_glue_ident: Ident,
    // The external C++ link name of the future/stream vtable.
    vtable_glue_link_name: String,
}

impl AstPieces {
    // Parses the macro arguments and returns the pieces, returning a `syn::Error` on error.
    fn from_token_streams(attribute: TokenStream, item: TokenStream) -> SynResult<AstPieces> {
        let namespace: NamespaceAttribute = syn::parse(attribute).map_err(|error| {
            SynError::new(error.span(), "expected possible namespace attribute")
        })?;

        let impl_item: ItemImpl = syn::parse(item).map_err(|error| {
            SynError::new(
                error.span(),
                "expected implementation of `Future` or `Stream`",
            )
        })?;
        if impl_item.unsafety.is_none() {
            return Err(SynError::new(
                impl_item.span(),
                "implementation must be marked `unsafe`",
            ));
        }
        if impl_item.defaultness.is_some() {
            return Err(SynError::new(
                impl_item.span(),
                "implementation must not be marked default",
            ));
        }
        if !impl_item.generics.params.is_empty() {
            return Err(SynError::new(
                impl_item.generics.params[0].span(),
                "generic bridged futures are unsupported",
            ));
        }
        if let Some(where_clause) = impl_item.generics.where_clause {
            return Err(SynError::new(
                where_clause.where_token.span,
                "generic bridged futures are unsupported",
            ));
        }

        // We don't check to make sure that `path` is `std::future::Future` or `futures::Stream`
        // here, even though that's ultimately a requirement, because we would have to perform name
        // resolution here in the macro to do that. Instead, we take advantage of the fact that
        // we're going to be generating an implementation of the appropriate trait anyway and simply
        // supply whatever the user wrote as the name of the trait to be implemented in our final
        // macro expansion. That way, the Rust compiler ends up checking that the trait that the
        // user wrote is the right one.
        let trait_path = match impl_item.trait_ {
            Some((None, ref path, _)) => (*path).clone(),
            _ => {
                return Err(SynError::new(
                    impl_item.span(),
                    "must implement the `Future` or `Stream` trait",
                ));
            }
        };

        if impl_item.items.len() != 1 {
            return Err(SynError::new(
                impl_item.span(),
                "expected implementation to contain a single item, `type Output = ...` or \
                 `type Item = ...`",
            ));
        }

        let (bridge_trait, output);
        match impl_item.items[0] {
            ImplItem::Type(ref impl_type) => {
                if !impl_type.attrs.is_empty() {
                    return Err(SynError::new(
                        impl_type.attrs[0].span(),
                        "attributes on the `type Output = ...` or `type Item = ...` declaration \
                         are not supported",
                    ));
                }
                match impl_type.vis {
                    Visibility::Inherited => {}
                    _ => {
                        return Err(SynError::new(
                            impl_type.vis.span(),
                            "`pub` or `crate` visibility modifiers on the `type Output = ...` \
                            or `type Item = ...` declaration are not supported",
                        ));
                    }
                }
                if let Some(defaultness) = impl_type.defaultness {
                    return Err(SynError::new(
                        defaultness.span(),
                        "`default` specifier on the `type Output = ...` or `type Item = ...` \
                         declaration is not supported",
                    ));
                }
                if !impl_type.generics.params.is_empty() {
                    return Err(SynError::new(
                        impl_type.generics.params[0].span(),
                        "generics on the `type Output = ...` or `type Item = ...` declaration are \
                         not supported",
                    ));
                }

                // We use the name of the associated type to disambiguate between a Future and a
                // Stream implementation.
                bridge_trait = if impl_type.ident == "Output" {
                    BridgeTrait::Future
                } else if impl_type.ident == "Item" {
                    BridgeTrait::Stream
                } else {
                    return Err(SynError::new(
                        impl_type.ident.span(),
                        "implementation must contain an associated type definition named \
                        `Output` or `Item`",
                    ));
                };
                output = impl_type.ty.clone();
            }
            _ => {
                return Err(SynError::new(
                    impl_item.span(),
                    "expected implementation to contain a single item, `type Output = ...` \
                    or `type Stream = ...`",
                ));
            }
        };

        let future = match *impl_item.self_ty {
            Type::Path(TypePath { qself: None, path }) => {
                path.get_ident().cloned().ok_or_else(|| {
                    SynError::new(
                        path.span(),
                        "expected `impl` declaration to implement a single type",
                    )
                })?
            }
            _ => {
                return Err(SynError::new(
                    impl_item.self_ty.span(),
                    "expected `impl` declaration to implement a single type",
                ));
            }
        };

        let qualified_name = Lit::Str(LitStr::new(
            &format!(
                "{}{}",
                namespace
                    .0
                    .iter()
                    .map(|piece| format!("{}::", piece))
                    .collect::<String>(),
                future
            ),
            future.span(),
        ));

        let vtable_glue_ident = Ident::new(
            &format!(
                "cxxasync_{}{}_vtable",
                namespace
                    .0
                    .iter()
                    .map(|piece| format!("{}_", piece))
                    .collect::<String>(),
                future
            ),
            future.span(),
        );
        let vtable_glue_link_name = format!(
            "cxxasync_{}{}_vtable",
            namespace
                .0
                .iter()
                .map(|piece| format!("{}$", piece))
                .collect::<String>(),
            future
        );

        Ok(AstPieces {
            bridge_trait,
            future,
            qualified_name,
            output,
            trait_path,
            vtable_glue_ident,
            vtable_glue_link_name,
        })
    }
}

mod keywords {
    use syn::custom_keyword;
    custom_keyword!(namespace);
}

struct NamespaceAttribute(Vec<String>);

impl Parse for NamespaceAttribute {
    fn parse(input: ParseStream) -> ParseResult<Self> {
        if input.is_empty() {
            return Ok(NamespaceAttribute(vec![]));
        }
        input.parse::<keywords::namespace>()?;
        input.parse::<Token![=]>()?;
        let path = input.call(Path::parse_mod_style)?;
        Ok(NamespaceAttribute(
            path.segments
                .iter()
                .map(|segment| segment.ident.to_string())
                .collect(),
        ))
    }
}