2222//! Hooks for for the RPC system.
2323//!
2424//! Roughly corresponds to capability.h in the C++ implementation.
25- #![ cfg( feature = "alloc" ) ]
2625
26+ #[ cfg( feature = "alloc" ) ]
2727use alloc:: boxed:: Box ;
28+ #[ cfg( feature = "alloc" ) ]
2829use core:: future:: Future ;
30+ #[ cfg( feature = "alloc" ) ]
2931use core:: marker:: { PhantomData , Unpin } ;
3032#[ cfg( feature = "rpc_try" ) ]
3133use core:: ops:: Try ;
34+ #[ cfg( feature = "alloc" ) ]
3235use core:: pin:: Pin ;
36+ #[ cfg( feature = "alloc" ) ]
3337use core:: task:: Poll ;
3438
39+ use crate :: any_pointer;
40+ #[ cfg( feature = "alloc" ) ]
3541use crate :: private:: capability:: { ClientHook , ParamsHook , RequestHook , ResponseHook , ResultsHook } ;
42+ #[ cfg( feature = "alloc" ) ]
3643use crate :: traits:: { Owned , Pipelined } ;
37- use crate :: { any_pointer, Error , MessageSize } ;
44+ #[ cfg( feature = "alloc" ) ]
45+ use crate :: { Error , MessageSize } ;
3846
3947/// A computation that might eventually resolve to a value of type `T` or to an error
4048/// of type `E`. Dropping the promise cancels the computation.
49+ #[ cfg( feature = "alloc" ) ]
4150#[ must_use = "futures do nothing unless polled" ]
4251pub struct Promise < T , E > {
4352 inner : PromiseInner < T , E > ,
4453}
4554
55+ #[ cfg( feature = "alloc" ) ]
4656enum PromiseInner < T , E > {
4757 Immediate ( Result < T , E > ) ,
4858 Deferred ( Pin < Box < dyn Future < Output = core:: result:: Result < T , E > > + ' static > > ) ,
4959 Empty ,
5060}
5161
5262// Allow Promise<T,E> to be Unpin, regardless of whether T and E are.
63+ #[ cfg( feature = "alloc" ) ]
5364impl < T , E > Unpin for PromiseInner < T , E > { }
5465
66+ #[ cfg( feature = "alloc" ) ]
5567impl < T , E > Promise < T , E > {
5668 pub fn ok ( value : T ) -> Self {
5769 Self {
@@ -75,6 +87,7 @@ impl<T, E> Promise<T, E> {
7587 }
7688}
7789
90+ #[ cfg( feature = "alloc" ) ]
7891impl < T , E > Future for Promise < T , E > {
7992 type Output = core:: result:: Result < T , E > ;
8093
@@ -92,6 +105,7 @@ impl<T, E> Future for Promise<T, E> {
92105 }
93106}
94107
108+ #[ cfg( feature = "alloc" ) ]
95109#[ cfg( feature = "rpc_try" ) ]
96110impl < T > std:: ops:: Try for Promise < T , crate :: Error > {
97111 type Output = Self ;
@@ -106,6 +120,7 @@ impl<T> std::ops::Try for Promise<T, crate::Error> {
106120 }
107121}
108122
123+ #[ cfg( feature = "alloc" ) ]
109124#[ cfg( feature = "rpc_try" ) ]
110125impl < T > std:: ops:: FromResidual for Promise < T , crate :: Error > {
111126 fn from_residual ( residual : <Self as Try >:: Residual ) -> Self {
@@ -117,6 +132,7 @@ impl<T> std::ops::FromResidual for Promise<T, crate::Error> {
117132}
118133
119134/// A promise for a result from a method call.
135+ #[ cfg( feature = "alloc" ) ]
120136#[ must_use]
121137pub struct RemotePromise < Results >
122138where
@@ -127,11 +143,13 @@ where
127143}
128144
129145/// A response from a method call, as seen by the client.
146+ #[ cfg( feature = "alloc" ) ]
130147pub struct Response < Results > {
131148 pub marker : PhantomData < Results > ,
132149 pub hook : Box < dyn ResponseHook > ,
133150}
134151
152+ #[ cfg( feature = "alloc" ) ]
135153impl < Results > Response < Results >
136154where
137155 Results : Pipelined + Owned ,
@@ -148,11 +166,13 @@ where
148166}
149167
150168/// A method call that has not been sent yet.
169+ #[ cfg( feature = "alloc" ) ]
151170pub struct Request < Params , Results > {
152171 pub marker : PhantomData < ( Params , Results ) > ,
153172 pub hook : Box < dyn RequestHook > ,
154173}
155174
175+ #[ cfg( feature = "alloc" ) ]
156176impl < Params , Results > Request < Params , Results >
157177where
158178 Params : Owned ,
@@ -173,6 +193,7 @@ where
173193 }
174194}
175195
196+ #[ cfg( feature = "alloc" ) ]
176197impl < Params , Results > Request < Params , Results >
177198where
178199 Results : Pipelined + Owned + ' static + Unpin ,
@@ -196,11 +217,13 @@ where
196217}
197218
198219/// The values of the parameters passed to a method call, as seen by the server.
220+ #[ cfg( feature = "alloc" ) ]
199221pub struct Params < T > {
200222 pub marker : PhantomData < T > ,
201223 pub hook : Box < dyn ParamsHook > ,
202224}
203225
226+ #[ cfg( feature = "alloc" ) ]
204227impl < T > Params < T > {
205228 pub fn new ( hook : Box < dyn ParamsHook > ) -> Self {
206229 Self {
@@ -217,11 +240,13 @@ impl<T> Params<T> {
217240}
218241
219242/// The return values of a method, written in-place by the method body.
243+ #[ cfg( feature = "alloc" ) ]
220244pub struct Results < T > {
221245 pub marker : PhantomData < T > ,
222246 pub hook : Box < dyn ResultsHook > ,
223247}
224248
249+ #[ cfg( feature = "alloc" ) ]
225250impl < T > Results < T >
226251where
227252 T : Owned ,
@@ -247,6 +272,7 @@ pub trait FromTypelessPipeline {
247272}
248273
249274/// Trait implemented (via codegen) by all user-defined capability client types.
275+ #[ cfg( feature = "alloc" ) ]
250276pub trait FromClientHook {
251277 /// Wraps a client hook to create a new client.
252278 fn new ( hook : Box < dyn ClientHook > ) -> Self ;
@@ -269,10 +295,12 @@ pub trait FromClientHook {
269295}
270296
271297/// An untyped client.
298+ #[ cfg( feature = "alloc" ) ]
272299pub struct Client {
273300 pub hook : Box < dyn ClientHook > ,
274301}
275302
303+ #[ cfg( feature = "alloc" ) ]
276304impl Client {
277305 pub fn new ( hook : Box < dyn ClientHook > ) -> Self {
278306 Self { hook }
@@ -302,6 +330,7 @@ impl Client {
302330}
303331
304332/// An untyped server.
333+ #[ cfg( feature = "alloc" ) ]
305334pub trait Server {
306335 fn dispatch_call (
307336 & mut self ,
@@ -313,6 +342,7 @@ pub trait Server {
313342}
314343
315344/// Trait to track the relationship between generated Server traits and Client structs.
345+ #[ cfg( feature = "alloc" ) ]
316346pub trait FromServer < S > : FromClientHook {
317347 // Implemented by the generated ServerDispatch struct.
318348 type Dispatch : Server + ' static + core:: ops:: DerefMut < Target = S > ;
@@ -322,6 +352,7 @@ pub trait FromServer<S>: FromClientHook {
322352
323353/// Gets the "resolved" version of a capability. One place this is useful is for pre-resolving
324354/// the argument to `capnp_rpc::CapabilityServerSet::get_local_server_of_resolved()`.
355+ #[ cfg( feature = "alloc" ) ]
325356pub async fn get_resolved_cap < C : FromClientHook > ( cap : C ) -> C {
326357 let mut hook = cap. into_client_hook ( ) ;
327358 let _ = hook. when_resolved ( ) . await ;
0 commit comments