Skip to content

Commit 1296f52

Browse files
committed
Add $Rust.imports
Relates to capnproto#411 and capnproto#233 This api is quite awkward but I'm intending it as a stopgap until cargo supports non-linking DEP environment variables. But it allows people to use generated code in external crates without us needing to add an annotation to the external `.capnp` file. This is important because without the change to cargo it would be awkward to require users use a modified `json.capnp`.
1 parent ae6eb67 commit 1296f52

File tree

16 files changed

+405
-6
lines changed

16 files changed

+405
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = [
1919
"async-byte-channel",
2020
"benchmark",
2121
"capnpc/test",
22+
"capnpc/test/external-crate",
2223
"capnpc/test-edition-2015",
2324
"capnpc/test-edition-2018",
2425
"capnpc/test-edition-2021",

capnp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub mod message;
5454
pub mod primitive_list;
5555
pub mod private;
5656
pub mod raw;
57+
pub mod rust_capnp;
5758
pub mod schema;
5859
pub mod serialize;
5960
pub mod serialize_packed;

capnp/src/rust_capnp.rs

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
// @generated by the capnpc-rust plugin to the Cap'n Proto schema compiler.
2+
// DO NOT EDIT.
3+
// source: rust.capnp
4+
5+
pub mod name {
6+
pub const ID: u64 = 0xc2fe4c6d100166d0;
7+
pub fn get_type() -> crate::introspect::Type { <crate::text::Owned as crate::introspect::Introspect>::introspect() }
8+
}
9+
pub mod parent_module {
10+
pub const ID: u64 = 0xabee386cd1450364;
11+
pub fn get_type() -> crate::introspect::Type { <crate::text::Owned as crate::introspect::Introspect>::introspect() }
12+
}
13+
pub mod imports {
14+
pub const ID: u64 = 0xc3b9fe42a83105cd;
15+
pub fn get_type() -> crate::introspect::Type { <crate::struct_list::Owned<crate::rust_capnp::import::Owned> as crate::introspect::Introspect>::introspect() }
16+
}
17+
18+
pub mod import {
19+
#[derive(Copy, Clone)]
20+
pub struct Owned(());
21+
impl crate::introspect::Introspect for Owned { fn introspect() -> crate::introspect::Type { crate::introspect::TypeVariant::Struct(crate::introspect::RawBrandedStructSchema { generic: &_private::RAW_SCHEMA, field_types: _private::get_field_types, annotation_types: _private::get_annotation_types }).into() } }
22+
impl crate::traits::Owned for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
23+
impl crate::traits::OwnedStruct for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
24+
impl crate::traits::Pipelined for Owned { type Pipeline = Pipeline; }
25+
26+
pub struct Reader<'a> { reader: crate::private::layout::StructReader<'a> }
27+
impl <'a,> ::core::marker::Copy for Reader<'a,> {}
28+
impl <'a,> ::core::clone::Clone for Reader<'a,> {
29+
fn clone(&self) -> Self { *self }
30+
}
31+
32+
impl <'a,> crate::traits::HasTypeId for Reader<'a,> {
33+
const TYPE_ID: u64 = _private::TYPE_ID;
34+
}
35+
impl <'a,> ::core::convert::From<crate::private::layout::StructReader<'a>> for Reader<'a,> {
36+
fn from(reader: crate::private::layout::StructReader<'a>) -> Self {
37+
Self { reader, }
38+
}
39+
}
40+
41+
impl <'a,> ::core::convert::From<Reader<'a,>> for crate::dynamic_value::Reader<'a> {
42+
fn from(reader: Reader<'a,>) -> Self {
43+
Self::Struct(crate::dynamic_struct::Reader::new(reader.reader, crate::schema::StructSchema::new(crate::introspect::RawBrandedStructSchema { generic: &_private::RAW_SCHEMA, field_types: _private::get_field_types::<>, annotation_types: _private::get_annotation_types::<>})))
44+
}
45+
}
46+
47+
impl <'a,> ::core::fmt::Debug for Reader<'a,> {
48+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::result::Result<(), ::core::fmt::Error> {
49+
core::fmt::Debug::fmt(&::core::convert::Into::<crate::dynamic_value::Reader<'_>>::into(*self), f)
50+
}
51+
}
52+
53+
impl <'a,> crate::traits::FromPointerReader<'a> for Reader<'a,> {
54+
fn get_from_pointer(reader: &crate::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [crate::Word]>) -> crate::Result<Self> {
55+
::core::result::Result::Ok(reader.get_struct(default)?.into())
56+
}
57+
}
58+
59+
impl <'a,> crate::traits::IntoInternalStructReader<'a> for Reader<'a,> {
60+
fn into_internal_struct_reader(self) -> crate::private::layout::StructReader<'a> {
61+
self.reader
62+
}
63+
}
64+
65+
impl <'a,> crate::traits::Imbue<'a> for Reader<'a,> {
66+
fn imbue(&mut self, cap_table: &'a crate::private::layout::CapTable) {
67+
self.reader.imbue(crate::private::layout::CapTableReader::Plain(cap_table))
68+
}
69+
}
70+
71+
impl <'a,> Reader<'a,> {
72+
pub fn reborrow(&self) -> Reader<'_,> {
73+
Self { .. *self }
74+
}
75+
76+
pub fn total_size(&self) -> crate::Result<crate::MessageSize> {
77+
self.reader.total_size()
78+
}
79+
#[inline]
80+
pub fn get_path(self) -> crate::Result<crate::text::Reader<'a>> {
81+
crate::traits::FromPointerReader::get_from_pointer(&self.reader.get_pointer_field(0), ::core::option::Option::None)
82+
}
83+
#[inline]
84+
pub fn has_path(&self) -> bool {
85+
!self.reader.get_pointer_field(0).is_null()
86+
}
87+
#[inline]
88+
pub fn get_crate(self) -> crate::Result<crate::text::Reader<'a>> {
89+
crate::traits::FromPointerReader::get_from_pointer(&self.reader.get_pointer_field(1), ::core::option::Option::None)
90+
}
91+
#[inline]
92+
pub fn has_crate(&self) -> bool {
93+
!self.reader.get_pointer_field(1).is_null()
94+
}
95+
}
96+
97+
pub struct Builder<'a> { builder: crate::private::layout::StructBuilder<'a> }
98+
impl <'a,> crate::traits::HasStructSize for Builder<'a,> {
99+
const STRUCT_SIZE: crate::private::layout::StructSize = crate::private::layout::StructSize { data: 0, pointers: 2 };
100+
}
101+
impl <'a,> crate::traits::HasTypeId for Builder<'a,> {
102+
const TYPE_ID: u64 = _private::TYPE_ID;
103+
}
104+
impl <'a,> ::core::convert::From<crate::private::layout::StructBuilder<'a>> for Builder<'a,> {
105+
fn from(builder: crate::private::layout::StructBuilder<'a>) -> Self {
106+
Self { builder, }
107+
}
108+
}
109+
110+
impl <'a,> ::core::convert::From<Builder<'a,>> for crate::dynamic_value::Builder<'a> {
111+
fn from(builder: Builder<'a,>) -> Self {
112+
Self::Struct(crate::dynamic_struct::Builder::new(builder.builder, crate::schema::StructSchema::new(crate::introspect::RawBrandedStructSchema { generic: &_private::RAW_SCHEMA, field_types: _private::get_field_types::<>, annotation_types: _private::get_annotation_types::<>})))
113+
}
114+
}
115+
116+
impl <'a,> crate::traits::ImbueMut<'a> for Builder<'a,> {
117+
fn imbue_mut(&mut self, cap_table: &'a mut crate::private::layout::CapTable) {
118+
self.builder.imbue(crate::private::layout::CapTableBuilder::Plain(cap_table))
119+
}
120+
}
121+
122+
impl <'a,> crate::traits::FromPointerBuilder<'a> for Builder<'a,> {
123+
fn init_pointer(builder: crate::private::layout::PointerBuilder<'a>, _size: u32) -> Self {
124+
builder.init_struct(<Self as crate::traits::HasStructSize>::STRUCT_SIZE).into()
125+
}
126+
fn get_from_pointer(builder: crate::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [crate::Word]>) -> crate::Result<Self> {
127+
::core::result::Result::Ok(builder.get_struct(<Self as crate::traits::HasStructSize>::STRUCT_SIZE, default)?.into())
128+
}
129+
}
130+
131+
impl <'a,> crate::traits::SetPointerBuilder for Reader<'a,> {
132+
fn set_pointer_builder(mut pointer: crate::private::layout::PointerBuilder<'_>, value: Self, canonicalize: bool) -> crate::Result<()> { pointer.set_struct(&value.reader, canonicalize) }
133+
}
134+
135+
impl <'a,> Builder<'a,> {
136+
pub fn into_reader(self) -> Reader<'a,> {
137+
self.builder.into_reader().into()
138+
}
139+
pub fn reborrow(&mut self) -> Builder<'_,> {
140+
Builder { builder: self.builder.reborrow() }
141+
}
142+
pub fn reborrow_as_reader(&self) -> Reader<'_,> {
143+
self.builder.as_reader().into()
144+
}
145+
146+
pub fn total_size(&self) -> crate::Result<crate::MessageSize> {
147+
self.builder.as_reader().total_size()
148+
}
149+
#[inline]
150+
pub fn get_path(self) -> crate::Result<crate::text::Builder<'a>> {
151+
crate::traits::FromPointerBuilder::get_from_pointer(self.builder.get_pointer_field(0), ::core::option::Option::None)
152+
}
153+
#[inline]
154+
pub fn set_path(&mut self, value: crate::text::Reader<'_>) {
155+
self.builder.reborrow().get_pointer_field(0).set_text(value);
156+
}
157+
#[inline]
158+
pub fn init_path(self, size: u32) -> crate::text::Builder<'a> {
159+
self.builder.get_pointer_field(0).init_text(size)
160+
}
161+
#[inline]
162+
pub fn has_path(&self) -> bool {
163+
!self.builder.is_pointer_field_null(0)
164+
}
165+
#[inline]
166+
pub fn get_crate(self) -> crate::Result<crate::text::Builder<'a>> {
167+
crate::traits::FromPointerBuilder::get_from_pointer(self.builder.get_pointer_field(1), ::core::option::Option::None)
168+
}
169+
#[inline]
170+
pub fn set_crate(&mut self, value: crate::text::Reader<'_>) {
171+
self.builder.reborrow().get_pointer_field(1).set_text(value);
172+
}
173+
#[inline]
174+
pub fn init_crate(self, size: u32) -> crate::text::Builder<'a> {
175+
self.builder.get_pointer_field(1).init_text(size)
176+
}
177+
#[inline]
178+
pub fn has_crate(&self) -> bool {
179+
!self.builder.is_pointer_field_null(1)
180+
}
181+
}
182+
183+
pub struct Pipeline { _typeless: crate::any_pointer::Pipeline }
184+
impl crate::capability::FromTypelessPipeline for Pipeline {
185+
fn new(typeless: crate::any_pointer::Pipeline) -> Self {
186+
Self { _typeless: typeless, }
187+
}
188+
}
189+
impl Pipeline {
190+
}
191+
mod _private {
192+
pub static ENCODED_NODE: [crate::Word; 47] = [
193+
crate::word(0, 0, 0, 0, 5, 0, 6, 0),
194+
crate::word(98, 59, 209, 191, 187, 136, 72, 196),
195+
crate::word(11, 0, 0, 0, 1, 0, 0, 0),
196+
crate::word(131, 208, 141, 60, 76, 193, 179, 131),
197+
crate::word(2, 0, 7, 0, 0, 0, 0, 0),
198+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
199+
crate::word(21, 0, 0, 0, 146, 0, 0, 0),
200+
crate::word(29, 0, 0, 0, 7, 0, 0, 0),
201+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
202+
crate::word(25, 0, 0, 0, 119, 0, 0, 0),
203+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
204+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
205+
crate::word(114, 117, 115, 116, 46, 99, 97, 112),
206+
crate::word(110, 112, 58, 73, 109, 112, 111, 114),
207+
crate::word(116, 0, 0, 0, 0, 0, 0, 0),
208+
crate::word(0, 0, 0, 0, 1, 0, 1, 0),
209+
crate::word(8, 0, 0, 0, 3, 0, 4, 0),
210+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
211+
crate::word(0, 0, 1, 0, 0, 0, 0, 0),
212+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
213+
crate::word(41, 0, 0, 0, 42, 0, 0, 0),
214+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
215+
crate::word(36, 0, 0, 0, 3, 0, 1, 0),
216+
crate::word(48, 0, 0, 0, 2, 0, 1, 0),
217+
crate::word(1, 0, 0, 0, 1, 0, 0, 0),
218+
crate::word(0, 0, 1, 0, 1, 0, 0, 0),
219+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
220+
crate::word(45, 0, 0, 0, 50, 0, 0, 0),
221+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
222+
crate::word(40, 0, 0, 0, 3, 0, 1, 0),
223+
crate::word(52, 0, 0, 0, 2, 0, 1, 0),
224+
crate::word(112, 97, 116, 104, 0, 0, 0, 0),
225+
crate::word(12, 0, 0, 0, 0, 0, 0, 0),
226+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
227+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
228+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
229+
crate::word(12, 0, 0, 0, 0, 0, 0, 0),
230+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
231+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
232+
crate::word(99, 114, 97, 116, 101, 0, 0, 0),
233+
crate::word(12, 0, 0, 0, 0, 0, 0, 0),
234+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
235+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
236+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
237+
crate::word(12, 0, 0, 0, 0, 0, 0, 0),
238+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
239+
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
240+
];
241+
pub fn get_field_types(index: u16) -> crate::introspect::Type {
242+
match index {
243+
0 => <crate::text::Owned as crate::introspect::Introspect>::introspect(),
244+
1 => <crate::text::Owned as crate::introspect::Introspect>::introspect(),
245+
_ => panic!("invalid field index {}", index),
246+
}
247+
}
248+
pub fn get_annotation_types(child_index: Option<u16>, index: u32) -> crate::introspect::Type {
249+
panic!("invalid annotation indices ({:?}, {}) ", child_index, index)
250+
}
251+
pub static RAW_SCHEMA: crate::introspect::RawStructSchema = crate::introspect::RawStructSchema {
252+
encoded_node: &ENCODED_NODE,
253+
nonunion_members: NONUNION_MEMBERS,
254+
members_by_discriminant: MEMBERS_BY_DISCRIMINANT,
255+
};
256+
pub static NONUNION_MEMBERS : &[u16] = &[0,1];
257+
pub static MEMBERS_BY_DISCRIMINANT : &[u16] = &[];
258+
pub const TYPE_ID: u64 = 0xc448_88bb_bfd1_3b62;
259+
}
260+
}
261+
pub mod crate_ {
262+
pub const ID: u64 = 0xc1763f46d790815c;
263+
pub fn get_type() -> crate::introspect::Type { <crate::text::Owned as crate::introspect::Introspect>::introspect() }
264+
}
265+
pub mod option {
266+
pub const ID: u64 = 0xabfef22c4ee1964e;
267+
pub fn get_type() -> crate::introspect::Type { <() as crate::introspect::Introspect>::introspect() }
268+
}

capnpc/rust.capnp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ annotation parentModule @0xabee386cd1450364 (file) :Text;
2727
# }
2828
# }
2929

30+
annotation imports @0xc3b9fe42a83105cd (file) :List(Import);
31+
# Allows specifying that the generated code for an import is located in another
32+
# crate.
33+
#
34+
# You only need this if your code uses imported types from another crate. You
35+
# can only use this annotation once in all the files compiled together.
36+
#
37+
# using Json = import "/capnp/compat/json.capnp";
38+
#
39+
# $Rust.imports([
40+
# (path = "/capnp/compat/json.capnp", crate = "capnp_json")
41+
# ]);
42+
43+
struct Import {
44+
path @0 :Text;
45+
crate @1 :Text;
46+
}
47+
48+
annotation crate @0xc1763f46d790815c (file) :Text;
49+
# The Rust crate that provides the generated code.
50+
#
51+
# You need this if you're providing a library to be used by other crates. If
52+
# you're only using the generated code in your own crate then you don't need to
53+
# change from the default.
54+
3055
annotation option @0xabfef22c4ee1964e (field) :Void;
3156
# Make the generated getters return Option<T> instead of T. Supported on
3257
# pointer types (e.g. structs, lists, and blobs).

0 commit comments

Comments
 (0)