Skip to content

Commit 579b4f4

Browse files
committed
allow no-alloc run-time reflection
1 parent eedb1a8 commit 579b4f4

23 files changed

+98
-225
lines changed

capnp/src/any_pointer.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ impl crate::introspect::Introspect for Owned {
4646
}
4747
}
4848

49-
#[cfg(feature = "alloc")]
5049
impl crate::traits::Pipelined for Owned {
5150
type Pipeline = Pipeline;
5251
}
@@ -214,30 +213,38 @@ impl<'a> crate::traits::ImbueMut<'a> for Builder<'a> {
214213
}
215214
}
216215

217-
#[cfg(feature = "alloc")]
218216
pub struct Pipeline {
219217
// XXX this should not be public
218+
#[cfg(feature = "alloc")]
220219
pub hook: Box<dyn PipelineHook>,
221220

221+
#[cfg(feature = "alloc")]
222222
ops: Vec<PipelineOp>,
223223
}
224224

225-
#[cfg(feature = "alloc")]
226225
impl Pipeline {
226+
#[cfg(feature = "alloc")]
227227
pub fn new(hook: Box<dyn PipelineHook>) -> Self {
228228
Self {
229229
hook,
230230
ops: Vec::new(),
231231
}
232232
}
233233

234+
#[cfg(feature = "alloc")]
234235
pub fn noop(&self) -> Self {
235236
Self {
236237
hook: self.hook.add_ref(),
237238
ops: self.ops.clone(),
238239
}
239240
}
240241

242+
#[cfg(not(feature = "alloc"))]
243+
pub fn noop(&self) -> Self {
244+
Self {}
245+
}
246+
247+
#[cfg(feature = "alloc")]
241248
pub fn get_pointer_field(&self, pointer_index: u16) -> Self {
242249
let mut new_ops = Vec::with_capacity(self.ops.len() + 1);
243250
for op in &self.ops {
@@ -250,26 +257,29 @@ impl Pipeline {
250257
}
251258
}
252259

260+
#[cfg(not(feature = "alloc"))]
261+
pub fn get_pointer_field(&self, _pointer_index: u16) -> Self {
262+
Self {}
263+
}
264+
265+
#[cfg(feature = "alloc")]
253266
pub fn as_cap(&self) -> Box<dyn ClientHook> {
254267
self.hook.get_pipelined_cap(&self.ops)
255268
}
256269
}
257270

258-
#[cfg(feature = "alloc")]
259271
impl crate::capability::FromTypelessPipeline for Pipeline {
260272
fn new(typeless: Pipeline) -> Self {
261273
typeless
262274
}
263275
}
264276

265-
#[cfg(feature = "alloc")]
266277
impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
267278
fn from(a: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
268279
crate::dynamic_value::Reader::AnyPointer(a)
269280
}
270281
}
271282

272-
#[cfg(feature = "alloc")]
273283
impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
274284
fn from(a: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
275285
crate::dynamic_value::Builder::AnyPointer(a)

capnp/src/any_pointer_list.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ impl<'a> core::iter::IntoIterator for Reader<'a> {
189189
}
190190
}
191191

192-
#[cfg(feature = "alloc")]
193192
impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
194193
fn from(t: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
195194
crate::dynamic_value::Reader::List(crate::dynamic_list::Reader::new(
@@ -199,7 +198,6 @@ impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
199198
}
200199
}
201200

202-
#[cfg(feature = "alloc")]
203201
impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
204202
fn from(t: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
205203
crate::dynamic_value::Builder::List(crate::dynamic_list::Builder::new(

capnp/src/capability.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,48 @@
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")]
2727
use alloc::boxed::Box;
28+
#[cfg(feature = "alloc")]
2829
use core::future::Future;
30+
#[cfg(feature = "alloc")]
2931
use core::marker::{PhantomData, Unpin};
3032
#[cfg(feature = "rpc_try")]
3133
use core::ops::Try;
34+
#[cfg(feature = "alloc")]
3235
use core::pin::Pin;
36+
#[cfg(feature = "alloc")]
3337
use core::task::Poll;
3438

39+
use crate::any_pointer;
40+
#[cfg(feature = "alloc")]
3541
use crate::private::capability::{ClientHook, ParamsHook, RequestHook, ResponseHook, ResultsHook};
42+
#[cfg(feature = "alloc")]
3643
use 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"]
4251
pub struct Promise<T, E> {
4352
inner: PromiseInner<T, E>,
4453
}
4554

55+
#[cfg(feature = "alloc")]
4656
enum 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")]
5364
impl<T, E> Unpin for PromiseInner<T, E> {}
5465

66+
#[cfg(feature = "alloc")]
5567
impl<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")]
7891
impl<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")]
96110
impl<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")]
110125
impl<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]
121137
pub struct RemotePromise<Results>
122138
where
@@ -127,11 +143,13 @@ where
127143
}
128144

129145
/// A response from a method call, as seen by the client.
146+
#[cfg(feature = "alloc")]
130147
pub struct Response<Results> {
131148
pub marker: PhantomData<Results>,
132149
pub hook: Box<dyn ResponseHook>,
133150
}
134151

152+
#[cfg(feature = "alloc")]
135153
impl<Results> Response<Results>
136154
where
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")]
151170
pub struct Request<Params, Results> {
152171
pub marker: PhantomData<(Params, Results)>,
153172
pub hook: Box<dyn RequestHook>,
154173
}
155174

175+
#[cfg(feature = "alloc")]
156176
impl<Params, Results> Request<Params, Results>
157177
where
158178
Params: Owned,
@@ -173,6 +193,7 @@ where
173193
}
174194
}
175195

196+
#[cfg(feature = "alloc")]
176197
impl<Params, Results> Request<Params, Results>
177198
where
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")]
199221
pub struct Params<T> {
200222
pub marker: PhantomData<T>,
201223
pub hook: Box<dyn ParamsHook>,
202224
}
203225

226+
#[cfg(feature = "alloc")]
204227
impl<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")]
220244
pub struct Results<T> {
221245
pub marker: PhantomData<T>,
222246
pub hook: Box<dyn ResultsHook>,
223247
}
224248

249+
#[cfg(feature = "alloc")]
225250
impl<T> Results<T>
226251
where
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")]
250276
pub 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")]
272299
pub struct Client {
273300
pub hook: Box<dyn ClientHook>,
274301
}
275302

303+
#[cfg(feature = "alloc")]
276304
impl 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")]
305334
pub 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")]
316346
pub 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")]
325356
pub async fn get_resolved_cap<C: FromClientHook>(cap: C) -> C {
326357
let mut hook = cap.into_client_hook();
327358
let _ = hook.when_resolved().await;

capnp/src/data.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,12 @@ impl<'a> crate::traits::SetPointerBuilder for Reader<'a> {
8282
}
8383
}
8484

85-
#[cfg(feature = "alloc")]
8685
impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
8786
fn from(d: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
8887
crate::dynamic_value::Reader::Data(d)
8988
}
9089
}
9190

92-
#[cfg(feature = "alloc")]
9391
impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
9492
fn from(d: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
9593
crate::dynamic_value::Builder::Data(d)

capnp/src/data_list.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ impl<'a> ::core::iter::IntoIterator for Reader<'a> {
199199
}
200200
}
201201

202-
#[cfg(feature = "alloc")]
203202
impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
204203
fn from(t: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
205204
crate::dynamic_value::Reader::List(crate::dynamic_list::Reader {
@@ -209,7 +208,6 @@ impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
209208
}
210209
}
211210

212-
#[cfg(feature = "alloc")]
213211
impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
214212
fn from(t: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
215213
crate::dynamic_value::Builder::List(crate::dynamic_list::Builder {

capnp/src/dynamic_list.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Dynamically-typed lists.
22
3-
#![cfg(feature = "alloc")]
43
use crate::dynamic_value;
54
use crate::introspect::{Type, TypeVariant};
65
use crate::private::layout::{self, PrimitiveElement};

capnp/src/dynamic_struct.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Dynamically-typed structs.
22
3-
#![cfg(feature = "alloc")]
43
use crate::introspect::TypeVariant;
54
use crate::private::layout;
65
use crate::schema::{Field, StructSchema};

capnp/src/dynamic_value.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Dynamically typed values.
22
3-
#![cfg(feature = "alloc")]
43
use crate::introspect::{self, TypeVariant};
54
use crate::schema_capnp::value;
65
use crate::Result;

capnp/src/enum_list.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ impl<'a, T: TryFrom<u16, Error = NotInSchema>> ::core::iter::IntoIterator for Re
214214
}
215215
}
216216

217-
#[cfg(feature = "alloc")]
218217
impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> From<Reader<'a, T>>
219218
for crate::dynamic_value::Reader<'a>
220219
{
@@ -226,7 +225,6 @@ impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> F
226225
}
227226
}
228227

229-
#[cfg(feature = "alloc")]
230228
impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> From<Builder<'a, T>>
231229
for crate::dynamic_value::Builder<'a>
232230
{

capnp/src/introspect.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Traits and types to support run-time type introspection, i.e. reflection.
22
3-
#[cfg(feature = "alloc")]
43
use crate::private::layout::ElementSize;
54

65
/// A type that supports reflection. All types that can appear in a Cap'n Proto message
@@ -71,7 +70,6 @@ impl Type {
7170

7271
/// If this type T appears as List(T), then what is the expected
7372
/// element size of the list?
74-
#[cfg(feature = "alloc")]
7573
pub(crate) fn expected_element_size(&self) -> ElementSize {
7674
if self.list_count > 0 {
7775
ElementSize::Pointer

0 commit comments

Comments
 (0)