@@ -357,22 +357,33 @@ impl Bridge<'_> {
357
357
}
358
358
}
359
359
360
- /// A client-side "global object" (usually a function pointer),
361
- /// which may be using a different `proc_macro` from the one
362
- /// used by the server, but can be interacted with compatibly.
360
+ /// A client-side RPC entry-point, which may be using a different `proc_macro`
361
+ /// from the one used by the server, but can be invoked compatibly.
363
362
///
364
- /// N.B., `F` must have FFI-friendly memory layout (e.g., a pointer).
365
- /// The call ABI of function pointers used for `F` doesn't
366
- /// need to match between server and client, since it's only
367
- /// passed between them and (eventually) called by the client.
363
+ /// Note that the (phantom) `I` ("input") and `O` ("output") type parameters
364
+ /// decorate the `Client<I, O>` with the RPC "interface" of the entry-point, but
365
+ /// do not themselves participate in ABI, at all, only facilitate type-checking.
366
+ ///
367
+ /// E.g. `Client<TokenStream, TokenStream>` is the common proc macro interface,
368
+ /// used for `#[proc_macro] fn foo(input: TokenStream) -> TokenStream`,
369
+ /// indicating that the RPC input and output will be serialized token streams,
370
+ /// and forcing the use of APIs that take/return `S::TokenStream`, server-side.
368
371
#[ repr( C ) ]
369
- #[ derive( Copy , Clone ) ]
370
- pub struct Client < F > {
372
+ pub struct Client < I , O > {
371
373
// FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
372
374
// a wrapper `fn` pointer, once `const fn` can reference `static`s.
373
375
pub ( super ) get_handle_counters : extern "C" fn ( ) -> & ' static HandleCounters ,
374
- pub ( super ) run : extern "C" fn ( Bridge < ' _ > , F ) -> Buffer ,
375
- pub ( super ) f : F ,
376
+
377
+ pub ( super ) run : extern "C" fn ( Bridge < ' _ > ) -> Buffer ,
378
+
379
+ pub ( super ) _marker : PhantomData < fn ( I ) -> O > ,
380
+ }
381
+
382
+ impl < I , O > Copy for Client < I , O > { }
383
+ impl < I , O > Clone for Client < I , O > {
384
+ fn clone ( & self ) -> Self {
385
+ * self
386
+ }
376
387
}
377
388
378
389
/// Client-side helper for handling client panics, entering the bridge,
@@ -419,31 +430,31 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
419
430
buf
420
431
}
421
432
422
- impl Client < fn ( crate :: TokenStream ) -> crate :: TokenStream > {
423
- pub const fn expand1 ( f : fn ( crate :: TokenStream ) -> crate :: TokenStream ) -> Self {
424
- extern "C" fn run (
425
- bridge : Bridge < ' _ > ,
426
- f : impl FnOnce ( crate :: TokenStream ) -> crate :: TokenStream ,
427
- ) -> Buffer {
428
- run_client ( bridge, |input| f ( crate :: TokenStream ( input) ) . 0 )
433
+ impl Client < crate :: TokenStream , crate :: TokenStream > {
434
+ pub const fn expand1 ( f : impl Fn ( crate :: TokenStream ) -> crate :: TokenStream + Copy ) -> Self {
435
+ Client {
436
+ get_handle_counters : HandleCounters :: get,
437
+ run : super :: selfless_reify:: reify_to_extern_c_fn_hrt_bridge ( move |bridge| {
438
+ run_client ( bridge, |input| f ( crate :: TokenStream ( input) ) . 0 )
439
+ } ) ,
440
+ _marker : PhantomData ,
429
441
}
430
- Client { get_handle_counters : HandleCounters :: get, run, f }
431
442
}
432
443
}
433
444
434
- impl Client < fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream > {
445
+ impl Client < ( crate :: TokenStream , crate :: TokenStream ) , crate :: TokenStream > {
435
446
pub const fn expand2 (
436
- f : fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream ,
447
+ f : impl Fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream + Copy ,
437
448
) -> Self {
438
- extern "C" fn run (
439
- bridge : Bridge < ' _ > ,
440
- f : impl FnOnce ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream ,
441
- ) -> Buffer {
442
- run_client ( bridge, |( input, input2) | {
443
- f ( crate :: TokenStream ( input) , crate :: TokenStream ( input2) ) . 0
444
- } )
449
+ Client {
450
+ get_handle_counters : HandleCounters :: get,
451
+ run : super :: selfless_reify:: reify_to_extern_c_fn_hrt_bridge ( move |bridge| {
452
+ run_client ( bridge, |( input, input2) | {
453
+ f ( crate :: TokenStream ( input) , crate :: TokenStream ( input2) ) . 0
454
+ } )
455
+ } ) ,
456
+ _marker : PhantomData ,
445
457
}
446
- Client { get_handle_counters : HandleCounters :: get, run, f }
447
458
}
448
459
}
449
460
@@ -453,17 +464,17 @@ pub enum ProcMacro {
453
464
CustomDerive {
454
465
trait_name : & ' static str ,
455
466
attributes : & ' static [ & ' static str ] ,
456
- client : Client < fn ( crate :: TokenStream ) -> crate :: TokenStream > ,
467
+ client : Client < crate :: TokenStream , crate :: TokenStream > ,
457
468
} ,
458
469
459
470
Attr {
460
471
name : & ' static str ,
461
- client : Client < fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream > ,
472
+ client : Client < ( crate :: TokenStream , crate :: TokenStream ) , crate :: TokenStream > ,
462
473
} ,
463
474
464
475
Bang {
465
476
name : & ' static str ,
466
- client : Client < fn ( crate :: TokenStream ) -> crate :: TokenStream > ,
477
+ client : Client < crate :: TokenStream , crate :: TokenStream > ,
467
478
} ,
468
479
}
469
480
@@ -479,21 +490,21 @@ impl ProcMacro {
479
490
pub const fn custom_derive (
480
491
trait_name : & ' static str ,
481
492
attributes : & ' static [ & ' static str ] ,
482
- expand : fn ( crate :: TokenStream ) -> crate :: TokenStream ,
493
+ expand : impl Fn ( crate :: TokenStream ) -> crate :: TokenStream + Copy ,
483
494
) -> Self {
484
495
ProcMacro :: CustomDerive { trait_name, attributes, client : Client :: expand1 ( expand) }
485
496
}
486
497
487
498
pub const fn attr (
488
499
name : & ' static str ,
489
- expand : fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream ,
500
+ expand : impl Fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream + Copy ,
490
501
) -> Self {
491
502
ProcMacro :: Attr { name, client : Client :: expand2 ( expand) }
492
503
}
493
504
494
505
pub const fn bang (
495
506
name : & ' static str ,
496
- expand : fn ( crate :: TokenStream ) -> crate :: TokenStream ,
507
+ expand : impl Fn ( crate :: TokenStream ) -> crate :: TokenStream + Copy ,
497
508
) -> Self {
498
509
ProcMacro :: Bang { name, client : Client :: expand1 ( expand) }
499
510
}
0 commit comments