roam-macros-core 7.3.0

Code generation core for roam RPC service macros
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//! Code generation core for roam RPC service macros.
//!
//! This crate contains all the code generation logic for the `#[service]` proc macro,
//! extracted into a regular library so it can be unit-tested with insta snapshots.

#![deny(unsafe_code)]

use ::quote::{format_ident, quote};
use heck::ToSnakeCase;
use proc_macro2::TokenStream as TokenStream2;

pub mod crate_name;

pub use roam_macros_parse::*;

use crate_name::FoundCrate;

/// Error type for validation/codegen errors.
#[derive(Debug, Clone)]
pub struct Error {
    pub span: proc_macro2::Span,
    pub message: String,
}

impl Error {
    pub fn new(span: proc_macro2::Span, message: impl Into<String>) -> Self {
        Self {
            span,
            message: message.into(),
        }
    }

    pub fn to_compile_error(&self) -> TokenStream2 {
        let msg = &self.message;
        let span = self.span;
        quote::quote_spanned! {span=> compile_error!(#msg); }
    }
}

impl From<ParseError> for Error {
    fn from(err: ParseError) -> Self {
        Self::new(proc_macro2::Span::call_site(), err.to_string())
    }
}

/// Parse a trait definition from a token stream, returning a codegen-friendly error.
pub fn parse(tokens: &TokenStream2) -> Result<ServiceTrait, Error> {
    parse_trait(tokens).map_err(Error::from)
}

/// Returns the token stream for accessing the `roam` crate.
pub fn roam_crate() -> TokenStream2 {
    match crate_name::crate_name("roam") {
        Ok(FoundCrate::Itself) => quote! { crate },
        Ok(FoundCrate::Name(name)) => {
            let ident = format_ident!("{}", name);
            quote! { ::#ident }
        }
        Err(_) => quote! { ::roam },
    }
}

/// Convert a parsed type into a token stream where every borrowed lifetime is `'static`.
///
/// This is used for descriptor hashing and client borrowed-return decode paths, where
/// we need a concrete `'static` shape type independent of method-local lifetimes.
fn to_static_type_tokens(ty: &Type) -> TokenStream2 {
    match ty {
        Type::Reference(TypeRef { mutable, inner, .. }) => {
            let inner = to_static_type_tokens(inner);
            if mutable.is_some() {
                quote! { &'static mut #inner }
            } else {
                quote! { &'static #inner }
            }
        }
        Type::Tuple(TypeTuple(group)) => {
            let elems: Vec<TokenStream2> = group
                .content
                .iter()
                .map(|entry| to_static_type_tokens(&entry.value))
                .collect();
            match elems.len() {
                0 => quote! { () },
                1 => {
                    let t = &elems[0];
                    quote! { (#t,) }
                }
                _ => quote! { (#(#elems),*) },
            }
        }
        Type::PathWithGenerics(PathWithGenerics { path, args, .. }) => {
            let path = path.to_token_stream();
            let args: Vec<TokenStream2> = args
                .iter()
                .map(|entry| match &entry.value {
                    GenericArgument::Lifetime(_) => quote! { 'static },
                    GenericArgument::Type(inner) => to_static_type_tokens(inner),
                })
                .collect();
            quote! { #path < #(#args),* > }
        }
        Type::Path(path) => path.to_token_stream(),
    }
}

// r[service-macro.is-source-of-truth]
// r[impl rpc]
// r[impl rpc.service]
// r[impl rpc.service.methods]
/// Generate all service code for a parsed trait.
///
/// Takes a `roam` token stream (the path to the roam crate) so that this function
/// can be called from tests with a fixed path like `::roam`.
pub fn generate_service(parsed: &ServiceTrait, roam: &TokenStream2) -> Result<TokenStream2, Error> {
    // r[impl rpc.channel.placement]
    // Validate: channels are only allowed in method args.
    for method in parsed.methods() {
        let return_type = method.return_type();
        if return_type.contains_channel() {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                format!(
                    "method `{}` has Channel (Tx/Rx) in return type - channels are only allowed in method arguments",
                    method.name()
                ),
            ));
        }

        let (ok_ty, err_ty) = method_ok_and_err_types(&return_type);
        if ok_ty.has_elided_reference_lifetime() {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                format!(
                    "method `{}` return type uses an elided reference lifetime; use explicit `'roam` (for example `&'roam str`)",
                    method.name()
                ),
            ));
        }
        if ok_ty.has_non_named_lifetime("roam") {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                format!(
                    "method `{}` return type may only use lifetime `'roam` for borrowed response data",
                    method.name()
                ),
            ));
        }
        if let Some(err_ty) = err_ty
            && (err_ty.has_lifetime() || err_ty.has_elided_reference_lifetime())
        {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                format!(
                    "method `{}` error type must be owned (no lifetimes), because client errors are not wrapped in SelfRef",
                    method.name()
                ),
            ));
        }
    }

    let service_descriptor_fn = generate_service_descriptor_fn(parsed, roam);
    let service_trait = generate_service_trait(parsed, roam);
    let dispatcher = generate_dispatcher(parsed, roam);
    let client = generate_client(parsed, roam);
    Ok(quote! {
        #service_descriptor_fn
        #service_trait
        #dispatcher
        #client
    })
}

// ============================================================================
// Service Descriptor Generation
// ============================================================================

fn generate_service_descriptor_fn(parsed: &ServiceTrait, roam: &TokenStream2) -> TokenStream2 {
    let service_name = parsed.name();
    let descriptor_fn_name = format_ident!("{}_service_descriptor", service_name.to_snake_case());

    // Build method descriptor expressions
    let method_descriptors: Vec<TokenStream2> = parsed
        .methods()
        .map(|m| {
            let method_name_str = m.name();

            // Build args tuple type and return type
            let arg_types: Vec<TokenStream2> =
                m.args().map(|arg| to_static_type_tokens(&arg.ty)).collect();
            let args_tuple_ty = quote! { (#(#arg_types,)*) };
            let arg_name_strs: Vec<String> = m.args().map(|arg| arg.name().to_string()).collect();

            let return_type = m.return_type();
            let return_ty_tokens = to_static_type_tokens(&return_type);

            let method_doc_expr = match m.doc() {
                Some(d) => quote! { Some(#d) },
                None => quote! { None },
            };

            quote! {
                #roam::hash::method_descriptor::<#args_tuple_ty, #return_ty_tokens>(
                    #service_name,
                    #method_name_str,
                    &[#(#arg_name_strs),*],
                    #method_doc_expr,
                )
            }
        })
        .collect();

    let service_doc_expr = match parsed.doc() {
        Some(d) => quote! { Some(#d) },
        None => quote! { None },
    };

    quote! {
        #[allow(non_snake_case, clippy::all)]
        pub fn #descriptor_fn_name() -> &'static #roam::session::ServiceDescriptor {
            static DESCRIPTOR: std::sync::OnceLock<&'static #roam::session::ServiceDescriptor> = std::sync::OnceLock::new();
            DESCRIPTOR.get_or_init(|| {
                let methods: Vec<&'static #roam::session::MethodDescriptor> = vec![
                    #(#method_descriptors),*
                ];
                Box::leak(Box::new(#roam::session::ServiceDescriptor {
                    service_name: #service_name,
                    methods: Box::leak(methods.into_boxed_slice()),
                    doc: #service_doc_expr,
                }))
            })
        }
    }
}

// ============================================================================
// Service Trait Generation
// ============================================================================

fn generate_service_trait(parsed: &ServiceTrait, roam: &TokenStream2) -> TokenStream2 {
    let trait_name = parsed.name.clone();
    let trait_doc = parsed.doc().map(|d| quote! { #[doc = #d] });

    let methods: Vec<TokenStream2> = parsed
        .methods()
        .map(|m| generate_trait_method(m, roam))
        .collect();

    quote! {
        #trait_doc
        pub trait #trait_name
        where
            Self: Send + Sync,
        {
            #(#methods)*
        }
    }
}

fn generate_trait_method(method: &ServiceMethod, roam: &TokenStream2) -> TokenStream2 {
    let method_name = format_ident!("{}", method.name().to_snake_case());
    let method_doc = method.doc().map(|d| quote! { #[doc = #d] });

    let return_type = method.return_type();
    let (ok_ty_ref, err_ty_ref) = method_ok_and_err_types(&return_type);
    let ok_has_roam_lifetime = ok_ty_ref.has_named_lifetime("roam");
    let method_lifetime = if ok_has_roam_lifetime {
        quote! { <'roam> }
    } else {
        quote! {}
    };

    let params: Vec<TokenStream2> = method
        .args()
        .map(|arg| {
            let name = format_ident!("{}", arg.name().to_snake_case());
            let ty = arg.ty.to_token_stream();
            quote! { #name: #ty }
        })
        .collect();

    if ok_has_roam_lifetime {
        let ok_ty = ok_ty_ref.to_token_stream();
        let err_ty = err_ty_ref
            .map(Type::to_token_stream)
            .unwrap_or_else(|| quote! { ::core::convert::Infallible });
        quote! {
            #method_doc
            fn #method_name #method_lifetime (&self, call: impl #roam::Call<'roam, #ok_ty, #err_ty>, #(#params),*) -> impl std::future::Future<Output = ()> + Send;
        }
    } else {
        let output_ty = return_type.to_token_stream();
        quote! {
            #method_doc
            fn #method_name (&self, #(#params),*) -> impl std::future::Future<Output = #output_ty> + Send;
        }
    }
}

// ============================================================================
// Dispatcher Generation
// ============================================================================

fn generate_dispatcher(parsed: &ServiceTrait, roam: &TokenStream2) -> TokenStream2 {
    let trait_name = parsed.name.clone();
    let dispatcher_name = format_ident!("{}Dispatcher", parsed.name());
    let descriptor_fn_name = format_ident!("{}_service_descriptor", parsed.name().to_snake_case());

    // Generate the if-else dispatch arms inside handle()
    let dispatch_arms: Vec<TokenStream2> = parsed
        .methods()
        .enumerate()
        .map(|(i, m)| generate_dispatch_arm(m, i, roam, &descriptor_fn_name))
        .collect();

    let no_methods = dispatch_arms.is_empty();

    let dispatch_body = if no_methods {
        quote! {
            let _ = (call, reply);
        }
    } else {
        // r[impl rpc.unknown-method]
        quote! {
            let method_id = call.method_id;
            let args_bytes = match &call.args {
                #roam::Payload::Incoming(bytes) => bytes,
                _ => {
                    reply.send_error(#roam::RoamError::<::core::convert::Infallible>::InvalidPayload).await;
                    return;
                }
            };
            #(#dispatch_arms)*
            reply.send_error(#roam::RoamError::<::core::convert::Infallible>::UnknownMethod).await;
        }
    };

    quote! {
        /// Dispatcher for this service.
        ///
        /// Wraps a handler and implements [`#roam::Handler`] by routing incoming
        /// calls to the appropriate trait method by method ID.
        #[derive(Clone)]
        pub struct #dispatcher_name<H> {
            handler: H,
        }

        impl<H> #dispatcher_name<H>
        where
            H: #trait_name + Clone + Send + Sync + 'static,
        {
            /// Create a new dispatcher wrapping the given handler.
            pub fn new(handler: H) -> Self {
                Self { handler }
            }
        }

        impl<H, R> #roam::Handler<R> for #dispatcher_name<H>
        where
            H: #trait_name + Clone + Send + Sync + 'static,
            R: #roam::ReplySink,
        {
            async fn handle(&self, call: #roam::SelfRef<#roam::RequestCall<'static>>, reply: R) {
                #dispatch_body
            }
        }
    }
}

fn generate_dispatch_arm(
    method: &ServiceMethod,
    method_index: usize,
    roam: &TokenStream2,
    descriptor_fn_name: &proc_macro2::Ident,
) -> TokenStream2 {
    let method_fn = format_ident!("{}", method.name().to_snake_case());
    let idx = method_index;

    // Build args tuple type for deserialization
    let arg_types: Vec<TokenStream2> = method
        .args()
        .map(|a| to_static_type_tokens(&a.ty))
        .collect();
    let args_tuple_type = match arg_types.len() {
        0 => quote! { () },
        1 => {
            let t = &arg_types[0];
            quote! { (#t,) }
        }
        _ => quote! { (#(#arg_types),*) },
    };

    // Destructure args tuple into named bindings
    let arg_names: Vec<proc_macro2::Ident> = method
        .args()
        .map(|a| format_ident!("{}", a.name().to_snake_case()))
        .collect();
    let destructure = match arg_names.len() {
        0 => quote! { let () = args; },
        1 => {
            let n = &arg_names[0];
            quote! { let (#n,) = args; }
        }
        _ => quote! { let (#(#arg_names),*) = args; },
    };

    let _ = idx;

    let has_channels = method.args().any(|a| a.ty.contains_channel());

    let channel_binding = if has_channels {
        quote! {
            #[cfg(not(target_arch = "wasm32"))]
            {
                if let Some(binder) = reply.channel_binder() {
                    let plan = #roam::RpcPlan::for_type::<#args_tuple_type>();
                    if !plan.channel_locations.is_empty() {
                        // SAFETY: args is a valid, initialized value of type #args_tuple_type
                        // and we have exclusive access to it via &mut.
                        #[allow(unsafe_code)]
                        unsafe {
                            #roam::bind_channels_callee_args(
                                &mut args as *mut _ as *mut u8,
                                plan,
                                &call.channels,
                                binder,
                            );
                        }
                    }
                }
            }
        }
    } else {
        quote! {}
    };

    // When there are channels, args must be mut for binding
    let args_let = if has_channels {
        quote! { let mut args: #args_tuple_type }
    } else {
        quote! { let args: #args_tuple_type }
    };

    let return_type = method.return_type();
    let (ok_ty_ref, err_ty_ref) = method_ok_and_err_types(&return_type);
    let ok_has_roam_lifetime = ok_ty_ref.has_named_lifetime("roam");
    let is_fallible = return_type.as_result().is_some();
    let ok_ty = ok_ty_ref.to_token_stream();
    let err_ty = err_ty_ref
        .map(Type::to_token_stream)
        .unwrap_or_else(|| quote! { ::core::convert::Infallible });

    let invoke_and_reply = if ok_has_roam_lifetime {
        quote! {
            let sink_call = #roam::SinkCall::new(reply);
            self.handler.#method_fn(sink_call, #(#arg_names),*).await;
        }
    } else if is_fallible {
        quote! {
            let result = self.handler.#method_fn(#(#arg_names),*).await;
            let sink_call = #roam::SinkCall::new(reply);
            #roam::Call::<'_, #ok_ty, #err_ty>::reply(sink_call, result).await;
        }
    } else {
        quote! {
            let value = self.handler.#method_fn(#(#arg_names),*).await;
            let sink_call = #roam::SinkCall::new(reply);
            #roam::Call::<'_, #ok_ty, #err_ty>::ok(sink_call, value).await;
        }
    };

    quote! {
        if method_id == #descriptor_fn_name().methods[#idx].id {
            #args_let = match #roam::facet_postcard::from_slice_borrowed(args_bytes) {
                Ok(v) => v,
                Err(_) => {
                    reply.send_error(#roam::RoamError::<::core::convert::Infallible>::InvalidPayload).await;
                    return;
                }
            };
            #channel_binding
            #destructure
            #invoke_and_reply
            return;
        }
    }
}

// ============================================================================
// Client Generation
// ============================================================================

// r[impl rpc.caller]
fn generate_client(parsed: &ServiceTrait, roam: &TokenStream2) -> TokenStream2 {
    let client_name = format_ident!("{}Client", parsed.name());
    let descriptor_fn_name = format_ident!("{}_service_descriptor", parsed.name().to_snake_case());
    let service_name = parsed.name();

    let client_doc = format!(
        "Client for the `{service_name}` service.\n\n\
        Stores a type-erased [`Caller`]({roam}::Caller) implementation.",
    );

    let client_methods: Vec<TokenStream2> = parsed
        .methods()
        .enumerate()
        .map(|(i, m)| generate_client_method(m, i, &descriptor_fn_name, roam))
        .collect();

    quote! {
        #[doc = #client_doc]
        #[must_use = "Dropping this client may close the connection if it is the last caller."]
        #[derive(Clone)]
        pub struct #client_name {
            caller: #roam::ErasedCaller,
        }

        impl #client_name {
            /// Create a new client wrapping the given caller.
            pub fn new(caller: impl #roam::Caller) -> Self {
                Self {
                    caller: #roam::ErasedCaller::new(caller),
                }
            }

            /// Resolve when the underlying connection closes.
            pub async fn closed(&self) {
                #roam::Caller::closed(&self.caller).await;
            }

            /// Return whether the underlying connection is still considered connected.
            pub fn is_connected(&self) -> bool {
                #roam::Caller::is_connected(&self.caller)
            }

            #(#client_methods)*
        }

        impl From<#roam::DriverCaller> for #client_name {
            fn from(caller: #roam::DriverCaller) -> Self {
                Self::new(caller)
            }
        }
    }
}

// r[impl zerocopy.send.borrowed]
// r[impl zerocopy.send.borrowed-in-struct]
// r[impl zerocopy.send.lifetime]
fn generate_client_method(
    method: &ServiceMethod,
    method_index: usize,
    descriptor_fn_name: &proc_macro2::Ident,
    roam: &TokenStream2,
) -> TokenStream2 {
    let method_name = format_ident!("{}", method.name().to_snake_case());
    let method_doc = method.doc().map(|d| quote! { #[doc = #d] });
    let idx = method_index;

    let params: Vec<TokenStream2> = method
        .args()
        .map(|arg| {
            let name = format_ident!("{}", arg.name().to_snake_case());
            let ty = arg.ty.to_token_stream();
            quote! { #name: #ty }
        })
        .collect();
    let arg_names: Vec<proc_macro2::Ident> = method
        .args()
        .map(|arg| format_ident!("{}", arg.name().to_snake_case()))
        .collect();

    // Args tuple type (for RpcPlan::for_type)
    let arg_types: Vec<TokenStream2> = method
        .args()
        .map(|a| to_static_type_tokens(&a.ty))
        .collect();
    let args_tuple_type = match arg_types.len() {
        0 => quote! { () },
        1 => {
            let t = &arg_types[0];
            quote! { (#t,) }
        }
        _ => quote! { (#(#arg_types),*) },
    };

    // Args tuple value (for serialization)
    let args_tuple = match arg_names.len() {
        0 => quote! { () },
        1 => {
            let n = &arg_names[0];
            quote! { (#n,) }
        }
        _ => quote! { (#(#arg_names),*) },
    };

    // r[impl rpc.fallible]
    // r[impl rpc.fallible.caller-signature]
    let return_type = method.return_type();
    let (ok_type_for_lifetimes, _) = method_ok_and_err_types(&return_type);
    let ok_uses_roam_lifetime = ok_type_for_lifetimes.has_named_lifetime("roam");
    let (ok_ty_decode, err_ty, client_return) = if let Some((ok, err)) = return_type.as_result() {
        let ok_t = ok.to_token_stream();
        let ok_t_static = to_static_type_tokens(ok);
        let err_t = err.to_token_stream();
        (
            if ok_uses_roam_lifetime {
                ok_t_static.clone()
            } else {
                ok_t.clone()
            },
            err_t.clone(),
            if ok_uses_roam_lifetime {
                quote! { Result<#roam::SelfRef<#ok_t_static>, #roam::RoamError<#err_t>> }
            } else {
                quote! { Result<#ok_t, #roam::RoamError<#err_t>> }
            },
        )
    } else {
        let t = return_type.to_token_stream();
        let t_static = to_static_type_tokens(&return_type);
        (
            if ok_uses_roam_lifetime {
                t_static.clone()
            } else {
                t.clone()
            },
            quote! { ::core::convert::Infallible },
            if ok_uses_roam_lifetime {
                quote! { Result<#roam::SelfRef<#t_static>, #roam::RoamError> }
            } else {
                quote! { Result<#t, #roam::RoamError> }
            },
        )
    };

    let has_channels = method.args().any(|a| a.ty.contains_channel());

    let (args_binding, channel_binding) = if has_channels {
        (
            quote! { let mut args = #args_tuple; },
            quote! {
                #[cfg(not(target_arch = "wasm32"))]
                let channels = if let Some(binder) = #roam::Caller::channel_binder(&self.caller) {
                    let plan = #roam::RpcPlan::for_type::<#args_tuple_type>();
                    // SAFETY: args is a valid, initialized value of the args tuple type
                    // and we have exclusive access to it via &mut.
                    #[allow(unsafe_code)]
                    unsafe {
                        #roam::bind_channels_caller_args(
                            &mut args as *mut _ as *mut u8,
                            plan,
                            binder,
                        )
                    }
                } else {
                    vec![]
                };
                #[cfg(target_arch = "wasm32")]
                let channels: Vec<#roam::ChannelId> = vec![];
            },
        )
    } else {
        (
            quote! { let args = #args_tuple; },
            quote! { let channels = vec![]; },
        )
    };

    if ok_uses_roam_lifetime {
        quote! {
            #method_doc
            pub async fn #method_name(&self, #(#params),*) -> #client_return {
                let method_id = #descriptor_fn_name().methods[#idx].id;
                #args_binding
                #channel_binding
                let req = #roam::RequestCall {
                    method_id,
                    args: #roam::Payload::outgoing(&args),
                    channels,
                    metadata: Default::default(),
                };
                let response = #roam::Caller::call(&self.caller, req).await.map_err(|e| match e {
                    #roam::RoamError::UnknownMethod => #roam::RoamError::<#err_ty>::UnknownMethod,
                    #roam::RoamError::InvalidPayload => #roam::RoamError::<#err_ty>::InvalidPayload,
                    #roam::RoamError::Cancelled => #roam::RoamError::<#err_ty>::Cancelled,
                    #roam::RoamError::User(never) => match never {},
                })?;
                response.try_repack(|resp, _bytes| {
                    let ret_bytes = match &resp.ret {
                        #roam::Payload::Incoming(bytes) => bytes,
                        _ => return Err(#roam::RoamError::<#err_ty>::InvalidPayload),
                    };
                    let result: Result<#ok_ty_decode, #roam::RoamError<#err_ty>> =
                        #roam::facet_postcard::from_slice_borrowed(ret_bytes)
                            .map_err(|_| #roam::RoamError::<#err_ty>::InvalidPayload)?;
                    let ret = result?;
                    Ok(ret)
                })
            }
        }
    } else {
        quote! {
            #method_doc
            pub async fn #method_name(&self, #(#params),*) -> #client_return {
                let method_id = #descriptor_fn_name().methods[#idx].id;
                #args_binding
                #channel_binding
                let req = #roam::RequestCall {
                    method_id,
                    args: #roam::Payload::outgoing(&args),
                    channels,
                    metadata: Default::default(),
                };
                let response = #roam::Caller::call(&self.caller, req).await.map_err(|e| match e {
                    #roam::RoamError::UnknownMethod => #roam::RoamError::<#err_ty>::UnknownMethod,
                    #roam::RoamError::InvalidPayload => #roam::RoamError::<#err_ty>::InvalidPayload,
                    #roam::RoamError::Cancelled => #roam::RoamError::<#err_ty>::Cancelled,
                    #roam::RoamError::User(never) => match never {},
                })?;
                let ret_bytes = match &response.ret {
                    #roam::Payload::Incoming(bytes) => bytes,
                    _ => return Err(#roam::RoamError::<#err_ty>::InvalidPayload),
                };
                let result: Result<#ok_ty_decode, #roam::RoamError<#err_ty>> =
                    #roam::facet_postcard::from_slice(ret_bytes)
                        .map_err(|_| #roam::RoamError::<#err_ty>::InvalidPayload)?;
                result
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_snapshot;
    use quote::quote;

    fn prettyprint(ts: proc_macro2::TokenStream) -> String {
        use std::io::Write;
        use std::process::{Command, Stdio};

        let mut child = Command::new("rustfmt")
            .args(["--edition", "2024"])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::inherit())
            .spawn()
            .expect("failed to spawn rustfmt");

        child
            .stdin
            .take()
            .unwrap()
            .write_all(ts.to_string().as_bytes())
            .unwrap();

        let output = child.wait_with_output().expect("rustfmt failed");
        assert!(
            output.status.success(),
            "rustfmt exited with {}",
            output.status
        );
        String::from_utf8(output.stdout).expect("rustfmt output not UTF-8")
    }

    fn generate(input: proc_macro2::TokenStream) -> String {
        let parsed = roam_macros_parse::parse_trait(&input).unwrap();
        let roam = quote! { ::roam };
        let ts = crate::generate_service(&parsed, &roam).unwrap();
        prettyprint(ts)
    }

    #[test]
    fn adder_infallible() {
        assert_snapshot!(generate(quote! {
            pub trait Adder { async fn add(&self, a: i32, b: i32) -> i32; }
        }));
    }

    #[test]
    fn fallible() {
        assert_snapshot!(generate(quote! {
            trait Calc { async fn div(&self, a: f64, b: f64) -> Result<f64, DivError>; }
        }));
    }

    #[test]
    fn no_args() {
        assert_snapshot!(generate(quote! {
            trait Ping { async fn ping(&self) -> u64; }
        }));
    }

    #[test]
    fn unit_return() {
        assert_snapshot!(generate(quote! {
            trait Notifier { async fn notify(&self, msg: String); }
        }));
    }

    #[test]
    fn streaming_tx() {
        assert_snapshot!(generate(quote! {
            trait Streamer { async fn count_up(&self, start: i32, output: Tx<i32>) -> i32; }
        }));
    }

    #[test]
    fn rejects_channels_in_return_type() {
        let parsed = roam_macros_parse::parse_trait(&quote! {
            trait Streamer { async fn stream(&self) -> Rx<i32>; }
        })
        .unwrap();
        let roam = quote! { ::roam };
        let err = crate::generate_service(&parsed, &roam).unwrap_err();
        assert_eq!(
            err.message,
            "method `stream` has Channel (Tx/Rx) in return type - channels are only allowed in method arguments"
        );
    }

    #[test]
    fn rejects_non_roam_return_lifetime() {
        let parsed = roam_macros_parse::parse_trait(&quote! {
            trait Svc { async fn bad(&self) -> &'a str; }
        })
        .unwrap();
        let roam = quote! { ::roam };
        let err = crate::generate_service(&parsed, &roam).unwrap_err();
        assert_eq!(
            err.message,
            "method `bad` return type may only use lifetime `'roam` for borrowed response data"
        );
    }

    #[test]
    fn rejects_elided_return_lifetime() {
        let parsed = roam_macros_parse::parse_trait(&quote! {
            trait Svc { async fn bad(&self) -> &str; }
        })
        .unwrap();
        let roam = quote! { ::roam };
        let err = crate::generate_service(&parsed, &roam).unwrap_err();
        assert_eq!(
            err.message,
            "method `bad` return type uses an elided reference lifetime; use explicit `'roam` (for example `&'roam str`)"
        );
    }

    #[test]
    fn rejects_borrowed_error_type() {
        let parsed = roam_macros_parse::parse_trait(&quote! {
            trait Svc { async fn bad(&self) -> Result<u32, &'roam str>; }
        })
        .unwrap();
        let roam = quote! { ::roam };
        let err = crate::generate_service(&parsed, &roam).unwrap_err();
        assert_eq!(
            err.message,
            "method `bad` error type must be owned (no lifetimes), because client errors are not wrapped in SelfRef"
        );
    }

    #[test]
    fn borrowed_roam_return() {
        assert_snapshot!(generate(quote! {
            trait Hasher { async fn hash(&self, payload: String) -> &'roam str; }
        }));
    }

    #[test]
    fn borrowed_roam_return_call_style() {
        assert_snapshot!(generate(quote! {
            trait Hasher { async fn hash(&self, payload: String) -> &'roam str; }
        }));
    }

    #[test]
    fn borrowed_roam_cow_return() {
        assert_snapshot!(generate(quote! {
            trait TextSvc {
                async fn normalize(&self, input: String) -> ::std::borrow::Cow<'roam, str>;
            }
        }));
    }

    #[test]
    fn borrowed_return_mixed_with_borrowed_args_and_channels_compiles_to_expected_shapes() {
        assert_snapshot!(generate(quote! {
            trait WordLab {
                async fn is_short(&self, word: &str) -> bool;
                async fn classify(&self, word: String) -> &'roam str;
                async fn transform(&self, prefix: &str, input: Rx<String>, output: Tx<String>) -> u32;
            }
        }));
    }
}