medea-macro 0.3.4

Internal macros and codegen for Medea media server project.
Documentation
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#![cfg_attr(any(doc, test), doc = include_str!("../README.md"))]
#![cfg_attr(not(any(doc, test)), doc = env!("CARGO_PKG_NAME"))]
#![deny(nonstandard_style, rustdoc::all, trivial_casts, trivial_numeric_casts)]
#![forbid(non_ascii_idents, unsafe_code)]
#![warn(
    clippy::absolute_paths,
    clippy::allow_attributes,
    clippy::allow_attributes_without_reason,
    clippy::as_conversions,
    clippy::as_pointer_underscore,
    clippy::as_ptr_cast_mut,
    clippy::assertions_on_result_states,
    clippy::branches_sharing_code,
    clippy::cfg_not_test,
    clippy::clear_with_drain,
    clippy::clone_on_ref_ptr,
    clippy::coerce_container_to_any,
    clippy::collection_is_never_read,
    clippy::create_dir,
    clippy::dbg_macro,
    clippy::debug_assert_with_mut_call,
    clippy::decimal_literal_representation,
    clippy::default_union_representation,
    clippy::derive_partial_eq_without_eq,
    clippy::doc_include_without_cfg,
    clippy::doc_paragraphs_missing_punctuation,
    clippy::empty_drop,
    clippy::empty_structs_with_brackets,
    clippy::equatable_if_let,
    clippy::empty_enum_variants_with_brackets,
    clippy::exit,
    clippy::expect_used,
    clippy::fallible_impl_from,
    clippy::filetype_is_file,
    clippy::float_cmp_const,
    clippy::fn_to_numeric_cast_any,
    clippy::get_unwrap,
    clippy::if_then_some_else_none,
    clippy::imprecise_flops,
    clippy::infinite_loop,
    clippy::iter_on_empty_collections,
    clippy::iter_on_single_items,
    clippy::iter_over_hash_type,
    clippy::iter_with_drain,
    clippy::large_include_file,
    clippy::large_stack_frames,
    clippy::let_underscore_untyped,
    clippy::literal_string_with_formatting_args,
    clippy::lossy_float_literal,
    clippy::map_err_ignore,
    clippy::map_with_unused_argument_over_ranges,
    clippy::mem_forget,
    clippy::missing_assert_message,
    clippy::missing_asserts_for_indexing,
    clippy::missing_const_for_fn,
    clippy::missing_docs_in_private_items,
    clippy::module_name_repetitions,
    clippy::multiple_inherent_impl,
    clippy::multiple_unsafe_ops_per_block,
    clippy::mutex_atomic,
    clippy::mutex_integer,
    clippy::needless_collect,
    clippy::needless_pass_by_ref_mut,
    clippy::needless_raw_strings,
    clippy::needless_type_cast,
    clippy::non_zero_suggestions,
    clippy::nonstandard_macro_braces,
    clippy::option_if_let_else,
    clippy::or_fun_call,
    clippy::panic_in_result_fn,
    clippy::partial_pub_fields,
    clippy::pathbuf_init_then_push,
    clippy::pedantic,
    clippy::precedence_bits,
    clippy::print_stderr,
    clippy::print_stdout,
    clippy::pub_without_shorthand,
    clippy::rc_buffer,
    clippy::rc_mutex,
    clippy::read_zero_byte_vec,
    clippy::redundant_clone,
    clippy::redundant_test_prefix,
    clippy::redundant_type_annotations,
    clippy::renamed_function_params,
    clippy::ref_patterns,
    clippy::rest_pat_in_fully_bound_structs,
    clippy::return_and_then,
    clippy::same_name_method,
    clippy::semicolon_inside_block,
    clippy::set_contains_or_insert,
    clippy::shadow_unrelated,
    clippy::significant_drop_in_scrutinee,
    clippy::significant_drop_tightening,
    clippy::single_option_map,
    clippy::str_to_string,
    clippy::string_add,
    clippy::string_lit_as_bytes,
    clippy::string_lit_chars_any,
    clippy::string_slice,
    clippy::suboptimal_flops,
    clippy::suspicious_operation_groupings,
    clippy::suspicious_xor_used_as_pow,
    clippy::tests_outside_test_module,
    clippy::todo,
    clippy::too_long_first_doc_paragraph,
    clippy::trailing_empty_array,
    clippy::transmute_undefined_repr,
    clippy::trivial_regex,
    clippy::try_err,
    clippy::undocumented_unsafe_blocks,
    clippy::unimplemented,
    clippy::uninhabited_references,
    clippy::unnecessary_safety_comment,
    clippy::unnecessary_safety_doc,
    clippy::unnecessary_self_imports,
    clippy::unnecessary_struct_initialization,
    clippy::unused_peekable,
    clippy::unused_result_ok,
    clippy::unused_trait_names,
    clippy::unwrap_in_result,
    clippy::unwrap_used,
    clippy::use_debug,
    clippy::use_self,
    clippy::useless_let_if_seq,
    clippy::verbose_file_reads,
    clippy::volatile_composites,
    clippy::while_float,
    clippy::wildcard_enum_match_arm,
    ambiguous_negative_literals,
    closure_returning_async_block,
    future_incompatible,
    impl_trait_redundant_captures,
    let_underscore_drop,
    macro_use_extern_crate,
    meta_variable_misuse,
    missing_copy_implementations,
    missing_debug_implementations,
    missing_docs,
    redundant_lifetimes,
    rust_2018_idioms,
    single_use_lifetimes,
    unit_bindings,
    unnameable_types,
    unreachable_pub,
    unstable_features,
    unused,
    variant_size_differences
)]

mod caused;
mod dart_bridge;
#[cfg(feature = "dart-codegen")]
mod dart_codegen;
mod dispatchable;
mod enum_delegate;
mod watchers;

#[cfg(test)]
use async_trait as _;
#[cfg(test)]
use medea_jason as _;
use proc_macro::TokenStream;

/// Delegates function calls to enum variants field.
/// Variants are expected to have only one field.
///
/// # How to use
///
/// ```rust
/// use medea_macro::enum_delegate;
///
/// #[enum_delegate(pub fn as_str(&self) -> &str)]
/// #[enum_delegate(pub fn push_str(&mut self, arg: &str))]
/// enum MyEnum {
///     Foo(String),
///     Bar(String),
/// }
///
/// let mut foo = MyEnum::Foo(String::from("foo"));
/// foo.push_str("_bar");
/// assert_eq!(foo.as_str(), "foo_bar")
/// ```
///
/// # Extended example
///
/// ```rust
/// use medea_macro::enum_delegate;
///
/// struct SomeState;
/// struct AnotherState;
///
/// struct Context {
///     some_value: i32,
/// }
///
/// struct Peer<S> {
///     context: Context,
///     state: S,
/// }
///
/// impl<T> Peer<T> {
///     pub fn some_value(&self) -> i32 {
///         self.context.some_value
///     }
///
///     pub fn function_with_additional_args(&self, some_arg: i32) -> i32 {
///         some_arg
///     }
///
///     pub fn mutable_function(&mut self) -> i32 {
///         let old_value = self.context.some_value;
///         self.context.some_value = 1000;
///         old_value
///     }
/// }
///
/// #[enum_delegate(pub fn some_value(&self) -> i32)]
/// #[enum_delegate(
///     pub fn function_with_additional_args(&self, some_arg: i32) -> i32
/// )]
/// #[enum_delegate(pub fn mutable_function(&mut self) -> i32)]
/// enum PeerStateMachine {
///     SomeState(Peer<SomeState>),
///     AnotherState(Peer<AnotherState>),
/// }
///
/// let mut peer = PeerStateMachine::SomeState(Peer {
///     context: Context { some_value: 10 },
///     state: SomeState,
/// });
///
/// assert_eq!(peer.some_value(), 10);
///
/// assert_eq!(peer.function_with_additional_args(100), 100);
///
/// assert_eq!(peer.mutable_function(), 10);
/// assert_eq!(peer.some_value(), 1000);
/// ```
#[proc_macro_attribute]
pub fn enum_delegate(args: TokenStream, input: TokenStream) -> TokenStream {
    enum_delegate::derive(&args, input)
        .unwrap_or_else(|e| e.to_compile_error().into())
}

/// Generates a `*Handler` trait and dispatching function for some event,
/// represented as `enum`.
///
/// # How to use
///
/// ### 1. Declare `enum` for event variants and a `struct` to handle them.
///
/// ```rust
/// use medea_macro::dispatchable;
///
/// #[dispatchable]
/// enum Event {
///     Some { new_bar: i32 },
///     Another,
///     UnnamedVariant(i32, i32),
/// }
///
/// struct Foo {
///     bar: i32,
///     baz: i32,
/// }
/// ```
///
/// ### 2. Implement handler for your `struct`.
///
/// For the given `enum` macro generates a unique trait by adding `Handler`
/// to the end of its name. Each method of trait is created by `snake_case`'ing
/// `enum` variants and adding `on_` prefix.
///
/// `type Output` is a type which will be returned from all functions of
/// `EventHandler` trait.
///
/// ```rust
/// # use medea_macro::dispatchable;
/// #
/// # #[dispatchable]
/// # enum Event {
/// #     Some { new_bar: i32 },
/// #     Another,
/// #     UnnamedVariant(i32, i32),
/// # }
/// #
/// # struct Foo {
/// #     bar: i32,
/// #     baz: i32,
/// # }
/// #
/// impl EventHandler for Foo {
///     type Output = i32;
///
///     fn on_some(&mut self, new_bar: i32) -> Self::Output {
///         self.bar = new_bar;
///         self.bar
///     }
///
///     fn on_another(&mut self) -> Self::Output {
///         self.bar = 2;
///         self.bar
///     }
///
///     fn on_unnamed_variant(&mut self, data: (i32, i32)) -> Self::Output {
///         self.bar = data.0;
///         self.baz = data.1;
///         self.bar
///     }
/// }
/// ```
///
/// ### 3. Dispatch event with handler
///
/// For the given `enum` macro generates `dispatch_with()` method to dispatch
/// `enum` with a given handler.
///
/// ```rust
/// # use medea_macro::dispatchable;
/// #
/// # #[dispatchable]
/// # enum Event {
/// #     Some { new_bar: i32 },
/// #     Another,
/// #     UnnamedVariant(i32, i32),
/// # }
/// #
/// # struct Foo {
/// #     bar: i32,
/// #     baz: i32,
/// # }
/// #
/// # impl EventHandler for Foo {
/// #    type Output = i32;
/// #
/// #    fn on_some(&mut self, new_bar: i32) -> Self::Output {
/// #        self.bar = new_bar;
/// #        self.bar
/// #    }
/// #
/// #    fn on_another(&mut self) -> Self::Output {
/// #        self.bar = 2;
/// #        self.bar
/// #    }
/// #
/// #    fn on_unnamed_variant(&mut self, data: (i32, i32)) -> Self::Output {
/// #        self.bar = data.0;
/// #        self.baz = data.1;
/// #        self.bar
/// #    }
/// # }
/// #
/// #
/// let mut foo = Foo { bar: 0, baz: 0 };
///
/// let bar = Event::Some { new_bar: 1 }.dispatch_with(&mut foo);
/// assert_eq!(foo.bar, 1);
/// assert_eq!(bar, 1);
///
/// let bar = Event::Another.dispatch_with(&mut foo);
/// assert_eq!(foo.bar, 2);
/// assert_eq!(bar, 2);
///
/// let bar = Event::UnnamedVariant(3, 3).dispatch_with(&mut foo);
/// assert_eq!(foo.bar, 3);
/// assert_eq!(foo.baz, 3);
/// assert_eq!(bar, 3);
/// ```
///
/// # Customize `self` type in handler functions (optional)
///
/// By default, all handler functions take `&mut Self`, if this doesn't suit
/// your case, then you can specify the method receiver manually:
/// `#[dispatchable(self: Rc<Self>)]`, `#[dispatchable(self: &Self)]`.
///
/// You can use any type that is a valid `self` receiver, e.g. `self`, `&self`,
/// `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or
/// `self: Pin<P>` (where P is one of the previous, except `Self`).
///
/// ```rust
/// # use std::rc::Rc;
/// use medea_macro::dispatchable;
///
/// #[dispatchable(self: Rc<Self>)]
/// enum Event {
///     Variant,
/// }
///
/// struct Foo;
/// impl EventHandler for Foo {
///     type Output = ();
///
///     fn on_variant(self: Rc<Self>) {}
/// }
///
/// let foo = Rc::new(Foo);
///
/// Event::Variant.dispatch_with(foo);
/// ```
///
/// # Async handlers (optional)
///
/// It's possible to make handler methods `async`. Rust doesn't support `async`
/// trait methods at the moment, that's why [`async_trait`] is used.
///
/// ```rust
/// use async_trait::async_trait;
/// use medea_macro::dispatchable;
///
/// #[dispatchable(async_trait(?Send))]
/// enum Event {
///     Variant,
/// }
///
/// struct Foo;
/// #[async_trait(?Send)]
/// impl EventHandler for Foo {
///     type Output = ();
///
///     async fn on_variant(&mut self) {}
/// }
///
/// let mut foo = Foo;
///
/// Event::Variant.dispatch_with(&mut foo);
/// ```
///
/// [`async_trait`]: https://docs.rs/async-trait
#[proc_macro_attribute]
pub fn dispatchable(args: TokenStream, input: TokenStream) -> TokenStream {
    let enum_item = syn::parse_macro_input!(input as dispatchable::Item);
    let args = syn::parse_macro_input!(args as dispatchable::Args);
    dispatchable::expand(enum_item, &args)
}

/// Generates `ComponentState` implementation on provided `impl`.
///
/// # Usage
///
/// ```rust,ignore
/// use std::rc::Rc;
///
/// use medea_jason::utils::Component;
/// use medea_macro::{watchers, watch};
///
/// struct SenderState {
///     muted: ObservableCell<bool>,
///     enabled: ObservableCell<bool>,
/// }
///
/// struct Sender;
///
/// type SenderComponent = Component<SenderState, Sender>;
///
/// #[watchers]
/// impl SenderComponent {
///     #[watch(self.muted.subscribe())]
///     async fn muted_change_watcher(
///         ctx: Rc<Sender>,
///         state: Rc<SenderState>,
///         new_muted_val: bool
///     ) -> Result<(), ()> {
///         Ok(())
///     }
///
///     #[watch(self.enabled.subscribe())]
///     async fn enabled_change_watcher(
///         ctx: Rc<Sender>,
///         state: Rc<SenderState>,
///         new_enabled_val: bool,
///     ) -> Result<(), ()> {
///         Ok(())
///     }
/// }
/// ```
///
/// ## `SenderComponent` implementation after macro expansion
///
/// ```rust,ignore
/// impl SenderComponent {
///     async fn muted_change_watcher(
///         sender: Rc<Sender>,
///         state: Rc<SenderState>,
///         new_muted_val: bool
///     ) -> Result<(), ()> {
///         Ok(())
///     }
///
///     async fn enabled_change_watcher(
///         sender: Rc<Sender>,
///         state: Rc<SenderState>,
///         new_enabled_val: bool,
///     ) -> Result<(), ()> {
///         Ok(())
///     }
/// }
///
/// impl ComponentState<Sender> for SenderState {
///     fn spawn_watchers(&self, s: &mut WatchersSpawner<SenderState, Sender>) {
///         s.spawn(
///             self.muted.subscribe(),
///             SenderComponent::muted_change_watcher,
///         );
///         s.spawn(
///             self.enabled.subscribe(),
///             SenderComponent::enabled_change_watcher,
///         );
///     }
/// }
/// ```
///
/// __Note__: `ComponentState` implementation is simplified in this example
///           for better readability.
///
/// In reality object and state types will be obtained by casting
/// `SenderComponent` to the `ComponentTypes` trait and getting types from it.
#[proc_macro_attribute]
pub fn watchers(_: TokenStream, input: TokenStream) -> TokenStream {
    watchers::expand(syn::parse_macro_input!(input))
        .unwrap_or_else(|e| e.to_compile_error().into())
}

/// Generate implementation of `Caused` trait for errors represented as enum.
///
/// # How to use
///
/// ### 1. Declare custom error and enum for error variants.
///
/// The `cause()` method returns error if nested error has its type declared
/// as an argument of the attribute `#[cause(error = path::to::Error)]` or the
/// error type is assumed to be imported as `Error`.
///
/// ```rust
/// use medea_jason::utils::Caused;
///
/// struct MyError;
///
/// #[derive(Caused)]
/// #[cause(error = MyError)]
/// enum FooError {
///     Internal,
///     MyError(MyError),
/// }
///
/// let err = FooError::Internal;
/// assert!(err.cause().is_none());
///
/// let err = FooError::MyError(MyError {});
/// assert!(err.cause().is_some());
/// ```
///
/// If enum variant has attribute `#[cause]` it will call the `cause()`
/// method on nested error.
///
/// ```rust
/// # use medea_jason::utils::Caused;
/// #
/// # struct MyError;
/// #
/// # #[derive(Caused)]
/// # #[cause(error = MyError)]
/// # enum FooError {
/// #     Internal,
/// #     MyError(MyError),
/// # }
/// #
/// #[derive(Caused)]
/// #[cause(error = MyError)]
/// enum BarError {
///     Foo(#[cause] FooError),
/// }
///
/// let err = BarError::Foo(FooError::Internal);
/// assert!(err.cause().is_none());
///
/// let err = BarError::Foo(FooError::MyError(MyError {}));
/// assert!(err.cause().is_some());
/// ```
#[proc_macro_derive(Caused, attributes(cause))]
pub fn derive_caused(input: TokenStream) -> TokenStream {
    syn::parse::<syn::DeriveInput>(input)
        .and_then(|i| {
            let mut s = synstructure::Structure::try_new(&i)?;
            caused::derive(&mut s)
        })
        .unwrap_or_else(|e| e.to_compile_error())
        .into()
}

/// Generates code for `extern` Dart functions registration and calling.
///
/// # Usage
///
/// ## Macro call
///
/// ```ignore
/// // Code will be generated in the `peer_connection` module, also you can
/// // control visibility of this module with a visibility modifier (`pub`).
/// //
/// // Module name will be used as a prefix for a registration function.
/// #[dart_bridge]
/// mod peer_connection {
///     use dart_sys::Dart_Handle;
///
///     use crate::platform::Error;
///
///     extern "C" {
///         // This documentation will be injected to the generated
///         // extern function caller:
///
///         /// Returns newly created SDP offer of the provided
///         /// `PeerConnection`.
///         fn create_offer(peer: Dart_Handle) -> Result<Dart_Handle, Error>;
///
///         /// Returns a newly created SDP answer of the provided
///         /// `PeerConnection`.
///         fn create_answer(peer: Dart_Handle) -> Result<Dart_Handle, Error>;
///     }
/// }
/// ```
///
/// ## Example of the generated code
///
/// ```ignore
/// #[automatically_derived]
/// mod peer_connection {
///     use dart_sys::Dart_Handle;
///
///     use crate::platform::Error;
///
///     type PeerConnectionCreateOfferFunction =
///         extern "C" fn(peer: Dart_Handle) -> Dart_Handle;
///     type PeerConnectionCreateAnswerFunction =
///         extern "C" fn(peer: Dart_Handle) -> Dart_Handle;
///
///     static PEER_CONNECTION__CREATE_OFFER__FUNCTION: ::std::sync::OnceLock<
///         ::send_wrapper::SendWrapper<PeerConnectionCreateOfferFunction>,
///     > = ::std::sync::OnceLock::new();
///
///     static PEER_CONNECTION__CREATE_ANSWER__FUNCTION: ::std::sync::OnceLock<
///         ::send_wrapper::SendWrapper<PeerConnectionCreateAnswerFunction>,
///     > = ::std::sync::OnceLock::new();
///
///     static PEER_CONNECTION__CREATE_OFFER__ERROR: ::std::sync::LazyLock<
///         ::send_wrapper::SendWrapper<::std::cell::RefCell<Option<Error>>>,
///     > = ::std::sync::LazyLock::new(|| {
///         ::send_wrapper::SendWrapper::new(::std::cell::RefCell::new(None))
///     });
///
///     static PEER_CONNECTION__CREATE_ANSWER__ERROR: ::std::sync::LazyLock<
///         ::send_wrapper::SendWrapper<::std::cell::RefCell<Option<Error>>>,
///     > = ::std::sync::LazyLock::new(|| {
///         ::send_wrapper::SendWrapper::new(::std::cell::RefCell::new(None))
///     });
///
///     #[unsafe(no_mangle)]
///     pub unsafe extern "C" fn register_peer_connection(
///         create_offer: PeerConnectionCreateOfferFunction,
///         create_answer: PeerConnectionCreateAnswerFunction,
///     ) {
///         PEER_CONNECTION__CREATE_OFFER__FUNCTION
///             .set(::send_wrapper::SendWrapper::new(create_offer))
///             .expect(
///                 "PEER_CONNECTION__CREATE_OFFER__FUNCTION \
///                 can only be set once",
///             );
///         PEER_CONNECTION__CREATE_ANSWER__FUNCTION
///             .set(::send_wrapper::SendWrapper::new(create_answer))
///             .expect(
///                 "PEER_CONNECTION__CREATE_ANSWER__FUNCTION \
///                 can only be set once",
///             );
///     }
///
///     /// Error setter for the `create_offer` function
///     #[unsafe(no_mangle)]
///     pub unsafe extern "C" fn peer_connection__create_offer__set_error(
///         err: Dart_Handle,
///     ) {
///         _ = PEER_CONNECTION__CREATE_OFFER__ERROR.replace(
///             Some(Error::from_handle(err))
///         );
///     }
///
///     /// Error setter for the `create_answer` function.
///     #[unsafe(no_mangle)]
///     pub unsafe extern "C" fn peer_connection__create_answer__set_error(
///         err: Dart_Handle,
///     ) {
///         _ = PEER_CONNECTION__CREATE_ANSWER__ERROR.replace(
///             Some(Error::from_handle(err))
///         );
///     }
///
///     /// Returns newly created SDP offer of the provided `PeerConnection`.
///     pub unsafe fn create_offer(
///         peer: Dart_Handle,
///     ) -> Result<Dart_Handle, Error> {
///         let res = (PEER_CONNECTION__CREATE_OFFER__FUNCTION
///             .get()
///             .as_ref()
///             .expect("PEER_CONNECTION__CREATE_OFFER__FUNCTION is not set"))
///             (peer);
///         if let Some(e) = PEER_CONNECTION__CREATE_OFFER__ERROR
///             .borrow_mut()
///             .take()
///         {
///             Err(e)
///         } else {
///             Ok(res)
///         }
///     }
///
///     /// Returns a newly created SDP answer of the provided
///     /// `PeerConnection`.
///     pub unsafe fn create_answer(
///         peer: Dart_Handle,
///     ) -> Result<Dart_Handle, Error> {
///         let res = (PEER_CONNECTION__CREATE_ANSWER__FUNCTION
///             .get()
///             .as_ref()
///             .expect("PEER_CONNECTION__CREATE_ANSWER__FUNCTION is not set"))
///             (peer);
///         if let Some(e) = PEER_CONNECTION__CREATE_ANSWER__ERROR
///             .borrow_mut()
///             .take()
///         {
///             Err(e)
///         } else {
///             Ok(res)
///         }
///     }
/// }
/// ```
///
/// ## Generated code usage
///
/// ```ignore
/// struct PeerConnection(Dart_Handle);
///
/// impl PeerConnection {
///     pub async fn create_offer(&self) -> RtcPeerConnectionResult<String> {
///         let fut = unsafe {
///             peer_connection::create_offer(self.handle.get())
///         }.unwrap();
///
///         unsafe { FutureFromDart::execute(fut) }
///             .await
///             .map_err(RtcPeerConnectionError::CreateOfferFailed)
///     }
///
///     pub async fn create_answer(&self) -> RtcPeerConnectionResult<String> {
///         let fut = unsafe {
///             peer_connection::create_offer(self.handle.get())
///         }.unwrap();
///
///         unsafe { FutureFromDart::execute(fut) }
///             .await
///             .map_err(RtcPeerConnectionError::CreateAnswerFailed)
///     }
/// }
///
/// pub enum RtcPeerConnectionError {
///     CreateOfferFailed(platform::Error),
///     CreateAnswerFailed(platform::Error),
/// }
///
/// pub type RtcPeerConnectionResult<T> = Result<T, RtcPeerConnectionError>;
/// ```
/// ## Dart side code
///
/// Also, you need to call registration functions on Dart side:
/// ```dart
/// dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>(
///         'register_peer_connection')(
///     createOffer_native,
///     createAnswer_native,
/// );
/// ```
#[proc_macro_attribute]
pub fn dart_bridge(args: TokenStream, input: TokenStream) -> TokenStream {
    dart_bridge::expand(args.into(), input.into())
        .map_or_else(|e| e.to_compile_error().into(), Into::into)
}