@@ -357,22 +357,33 @@ impl Bridge<'_> {
357357 }
358358}
359359
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.
363362///
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.
368371#[ repr( C ) ]
369- #[ derive( Copy , Clone ) ]
370- pub struct Client < F > {
372+ pub struct Client < I , O > {
371373 // FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
372374 // a wrapper `fn` pointer, once `const fn` can reference `static`s.
373375 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+ }
376387}
377388
378389/// 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<()>>(
419430 buf
420431}
421432
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 ,
429441 }
430- Client { get_handle_counters : HandleCounters :: get, run, f }
431442 }
432443}
433444
434- impl Client < fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream > {
445+ impl Client < ( crate :: TokenStream , crate :: TokenStream ) , crate :: TokenStream > {
435446 pub const fn expand2 (
436- f : fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream ,
447+ f : impl Fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream + Copy ,
437448 ) -> 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 ,
445457 }
446- Client { get_handle_counters : HandleCounters :: get, run, f }
447458 }
448459}
449460
@@ -453,17 +464,17 @@ pub enum ProcMacro {
453464 CustomDerive {
454465 trait_name : & ' static str ,
455466 attributes : & ' static [ & ' static str ] ,
456- client : Client < fn ( crate :: TokenStream ) -> crate :: TokenStream > ,
467+ client : Client < crate :: TokenStream , crate :: TokenStream > ,
457468 } ,
458469
459470 Attr {
460471 name : & ' static str ,
461- client : Client < fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream > ,
472+ client : Client < ( crate :: TokenStream , crate :: TokenStream ) , crate :: TokenStream > ,
462473 } ,
463474
464475 Bang {
465476 name : & ' static str ,
466- client : Client < fn ( crate :: TokenStream ) -> crate :: TokenStream > ,
477+ client : Client < crate :: TokenStream , crate :: TokenStream > ,
467478 } ,
468479}
469480
@@ -479,21 +490,21 @@ impl ProcMacro {
479490 pub const fn custom_derive (
480491 trait_name : & ' static str ,
481492 attributes : & ' static [ & ' static str ] ,
482- expand : fn ( crate :: TokenStream ) -> crate :: TokenStream ,
493+ expand : impl Fn ( crate :: TokenStream ) -> crate :: TokenStream + Copy ,
483494 ) -> Self {
484495 ProcMacro :: CustomDerive { trait_name, attributes, client : Client :: expand1 ( expand) }
485496 }
486497
487498 pub const fn attr (
488499 name : & ' static str ,
489- expand : fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream ,
500+ expand : impl Fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream + Copy ,
490501 ) -> Self {
491502 ProcMacro :: Attr { name, client : Client :: expand2 ( expand) }
492503 }
493504
494505 pub const fn bang (
495506 name : & ' static str ,
496- expand : fn ( crate :: TokenStream ) -> crate :: TokenStream ,
507+ expand : impl Fn ( crate :: TokenStream ) -> crate :: TokenStream + Copy ,
497508 ) -> Self {
498509 ProcMacro :: Bang { name, client : Client :: expand1 ( expand) }
499510 }
0 commit comments