22
33use std:: cell:: RefCell ;
44use std:: marker:: PhantomData ;
5- use std:: sync:: atomic:: AtomicU32 ;
65
76use super :: * ;
87
9- #[ repr( C ) ]
10- pub ( super ) struct HandleCounters {
11- pub ( super ) token_stream : AtomicU32 ,
12- pub ( super ) span : AtomicU32 ,
13- }
14-
15- static COUNTERS : HandleCounters =
16- HandleCounters { token_stream : AtomicU32 :: new ( 1 ) , span : AtomicU32 :: new ( 1 ) } ;
17-
188pub ( crate ) struct TokenStream {
199 handle : handle:: Handle ,
2010}
@@ -47,6 +37,18 @@ impl<S> Decode<'_, '_, S> for TokenStream {
4737 }
4838}
4939
40+ impl Encode < ( ) > for crate :: TokenStream {
41+ fn encode ( self , w : & mut Buffer , s : & mut ( ) ) {
42+ self . 0 . encode ( w, s)
43+ }
44+ }
45+
46+ impl Decode < ' _ , ' _ , ( ) > for crate :: TokenStream {
47+ fn decode ( r : & mut & [ u8 ] , s : & mut ( ) ) -> Self {
48+ crate :: TokenStream ( Some ( Decode :: decode ( r, s) ) )
49+ }
50+ }
51+
5052#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
5153pub ( crate ) struct Span {
5254 handle : handle:: Handle ,
@@ -209,8 +211,6 @@ pub(crate) fn is_available() -> bool {
209211/// and forcing the use of APIs that take/return `S::TokenStream`, server-side.
210212#[ repr( C ) ]
211213pub struct Client < I , O > {
212- pub ( super ) handle_counters : & ' static HandleCounters ,
213-
214214 pub ( super ) run : extern "C" fn ( BridgeConfig < ' _ > ) -> Buffer ,
215215
216216 pub ( super ) _marker : PhantomData < fn ( I ) -> O > ,
@@ -243,14 +243,13 @@ fn maybe_install_panic_hook(force_show_panics: bool) {
243243
244244/// Client-side helper for handling client panics, entering the bridge,
245245/// deserializing input and serializing output.
246- // FIXME(eddyb) maybe replace `Bridge::enter` with this?
247- fn run_client < A : for < ' a , ' s > Decode < ' a , ' s , ( ) > , R : Encode < ( ) > > (
246+ fn run_client < A : for < ' a , ' s > Decode < ' a , ' s , ( ) > > (
248247 config : BridgeConfig < ' _ > ,
249- f : impl FnOnce ( A ) -> R ,
248+ f : impl FnOnce ( A ) -> crate :: TokenStream ,
250249) -> Buffer {
251250 let BridgeConfig { input : mut buf, dispatch, force_show_panics, .. } = config;
252251
253- panic:: catch_unwind ( panic:: AssertUnwindSafe ( || {
252+ let res = panic:: catch_unwind ( panic:: AssertUnwindSafe ( || {
254253 maybe_install_panic_hook ( force_show_panics) ;
255254
256255 // Make sure the symbol store is empty before decoding inputs.
@@ -267,23 +266,12 @@ fn run_client<A: for<'a, 's> Decode<'a, 's, ()>, R: Encode<()>>(
267266 // Take the `cached_buffer` back out, for the output value.
268267 buf = RefCell :: into_inner ( state) . cached_buffer ;
269268
270- // HACK(eddyb) Separate encoding a success value (`Ok(output)`)
271- // from encoding a panic (`Err(e: PanicMessage)`) to avoid
272- // having handles outside the `bridge.enter(|| ...)` scope, and
273- // to catch panics that could happen while encoding the success.
274- //
275- // Note that panics should be impossible beyond this point, but
276- // this is defensively trying to avoid any accidental panicking
277- // reaching the `extern "C"` (which should `abort` but might not
278- // at the moment, so this is also potentially preventing UB).
279- buf. clear ( ) ;
280- Ok :: < _ , ( ) > ( output) . encode ( & mut buf, & mut ( ) ) ;
281- } ) )
282- . map_err ( PanicMessage :: from)
283- . unwrap_or_else ( |e| {
284- buf. clear ( ) ;
285- Err :: < ( ) , _ > ( e) . encode ( & mut buf, & mut ( ) ) ;
286- } ) ;
269+ output
270+ } ) ) ;
271+
272+ // Serialize response of type `Result<R, PanicMessage>`.
273+ buf. clear ( ) ;
274+ res. map_err ( PanicMessage :: from) . encode ( & mut buf, & mut ( ) ) ;
287275
288276 // Now that a response has been serialized, invalidate all symbols
289277 // registered with the interner.
@@ -294,9 +282,8 @@ fn run_client<A: for<'a, 's> Decode<'a, 's, ()>, R: Encode<()>>(
294282impl Client < crate :: TokenStream , crate :: TokenStream > {
295283 pub const fn expand1 ( f : impl Fn ( crate :: TokenStream ) -> crate :: TokenStream + Copy ) -> Self {
296284 Client {
297- handle_counters : & COUNTERS ,
298285 run : super :: selfless_reify:: reify_to_extern_c_fn_hrt_bridge ( move |bridge| {
299- run_client ( bridge, |input| f ( crate :: TokenStream ( Some ( input) ) ) . 0 )
286+ run_client ( bridge, |input| f ( input) )
300287 } ) ,
301288 _marker : PhantomData ,
302289 }
@@ -308,11 +295,8 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
308295 f : impl Fn ( crate :: TokenStream , crate :: TokenStream ) -> crate :: TokenStream + Copy ,
309296 ) -> Self {
310297 Client {
311- handle_counters : & COUNTERS ,
312298 run : super :: selfless_reify:: reify_to_extern_c_fn_hrt_bridge ( move |bridge| {
313- run_client ( bridge, |( input, input2) | {
314- f ( crate :: TokenStream ( Some ( input) ) , crate :: TokenStream ( Some ( input2) ) ) . 0
315- } )
299+ run_client ( bridge, |( input, input2) | f ( input, input2) )
316300 } ) ,
317301 _marker : PhantomData ,
318302 }
0 commit comments