Skip to content

Commit

Permalink
Add $Rust.imports
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
dzfranklin committed Jun 12, 2023
1 parent ae6eb67 commit a0c37e8
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"async-byte-channel",
"benchmark",
"capnpc/test",
"capnpc/test/external-crate",
"capnpc/test-edition-2015",
"capnpc/test-edition-2018",
"capnpc/test-edition-2021",
Expand Down
1 change: 1 addition & 0 deletions capnp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub mod message;
pub mod primitive_list;
pub mod private;
pub mod raw;
pub mod rust_capnp;
pub mod schema;
pub mod serialize;
pub mod serialize_packed;
Expand Down
268 changes: 268 additions & 0 deletions capnp/src/rust_capnp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
// @generated by the capnpc-rust plugin to the Cap'n Proto schema compiler.
// DO NOT EDIT.
// source: rust.capnp

pub mod name {
pub const ID: u64 = 0xc2fe4c6d100166d0;
pub fn get_type() -> crate::introspect::Type { <crate::text::Owned as crate::introspect::Introspect>::introspect() }
}
pub mod parent_module {
pub const ID: u64 = 0xabee386cd1450364;
pub fn get_type() -> crate::introspect::Type { <crate::text::Owned as crate::introspect::Introspect>::introspect() }
}
pub mod imports {
pub const ID: u64 = 0xc3b9fe42a83105cd;
pub fn get_type() -> crate::introspect::Type { <crate::struct_list::Owned<crate::rust_capnp::import::Owned> as crate::introspect::Introspect>::introspect() }
}

pub mod import {
#[derive(Copy, Clone)]
pub struct Owned(());
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() } }
impl crate::traits::Owned for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
impl crate::traits::OwnedStruct for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
impl crate::traits::Pipelined for Owned { type Pipeline = Pipeline; }

pub struct Reader<'a> { reader: crate::private::layout::StructReader<'a> }
impl <'a,> ::core::marker::Copy for Reader<'a,> {}
impl <'a,> ::core::clone::Clone for Reader<'a,> {
fn clone(&self) -> Self { *self }
}

impl <'a,> crate::traits::HasTypeId for Reader<'a,> {
const TYPE_ID: u64 = _private::TYPE_ID;
}
impl <'a,> ::core::convert::From<crate::private::layout::StructReader<'a>> for Reader<'a,> {
fn from(reader: crate::private::layout::StructReader<'a>) -> Self {
Self { reader, }
}
}

impl <'a,> ::core::convert::From<Reader<'a,>> for crate::dynamic_value::Reader<'a> {
fn from(reader: Reader<'a,>) -> Self {
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::<>})))
}
}

impl <'a,> ::core::fmt::Debug for Reader<'a,> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::result::Result<(), ::core::fmt::Error> {
core::fmt::Debug::fmt(&::core::convert::Into::<crate::dynamic_value::Reader<'_>>::into(*self), f)
}
}

impl <'a,> crate::traits::FromPointerReader<'a> for Reader<'a,> {
fn get_from_pointer(reader: &crate::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [crate::Word]>) -> crate::Result<Self> {
::core::result::Result::Ok(reader.get_struct(default)?.into())
}
}

impl <'a,> crate::traits::IntoInternalStructReader<'a> for Reader<'a,> {
fn into_internal_struct_reader(self) -> crate::private::layout::StructReader<'a> {
self.reader
}
}

impl <'a,> crate::traits::Imbue<'a> for Reader<'a,> {
fn imbue(&mut self, cap_table: &'a crate::private::layout::CapTable) {
self.reader.imbue(crate::private::layout::CapTableReader::Plain(cap_table))
}
}

impl <'a,> Reader<'a,> {
pub fn reborrow(&self) -> Reader<'_,> {
Self { .. *self }
}

pub fn total_size(&self) -> crate::Result<crate::MessageSize> {
self.reader.total_size()
}
#[inline]
pub fn get_path(self) -> crate::Result<crate::text::Reader<'a>> {
crate::traits::FromPointerReader::get_from_pointer(&self.reader.get_pointer_field(0), ::core::option::Option::None)
}
#[inline]
pub fn has_path(&self) -> bool {
!self.reader.get_pointer_field(0).is_null()
}
#[inline]
pub fn get_crate(self) -> crate::Result<crate::text::Reader<'a>> {
crate::traits::FromPointerReader::get_from_pointer(&self.reader.get_pointer_field(1), ::core::option::Option::None)
}
#[inline]
pub fn has_crate(&self) -> bool {
!self.reader.get_pointer_field(1).is_null()
}
}

pub struct Builder<'a> { builder: crate::private::layout::StructBuilder<'a> }
impl <'a,> crate::traits::HasStructSize for Builder<'a,> {
const STRUCT_SIZE: crate::private::layout::StructSize = crate::private::layout::StructSize { data: 0, pointers: 2 };
}
impl <'a,> crate::traits::HasTypeId for Builder<'a,> {
const TYPE_ID: u64 = _private::TYPE_ID;
}
impl <'a,> ::core::convert::From<crate::private::layout::StructBuilder<'a>> for Builder<'a,> {
fn from(builder: crate::private::layout::StructBuilder<'a>) -> Self {
Self { builder, }
}
}

impl <'a,> ::core::convert::From<Builder<'a,>> for crate::dynamic_value::Builder<'a> {
fn from(builder: Builder<'a,>) -> Self {
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::<>})))
}
}

impl <'a,> crate::traits::ImbueMut<'a> for Builder<'a,> {
fn imbue_mut(&mut self, cap_table: &'a mut crate::private::layout::CapTable) {
self.builder.imbue(crate::private::layout::CapTableBuilder::Plain(cap_table))
}
}

impl <'a,> crate::traits::FromPointerBuilder<'a> for Builder<'a,> {
fn init_pointer(builder: crate::private::layout::PointerBuilder<'a>, _size: u32) -> Self {
builder.init_struct(<Self as crate::traits::HasStructSize>::STRUCT_SIZE).into()
}
fn get_from_pointer(builder: crate::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [crate::Word]>) -> crate::Result<Self> {
::core::result::Result::Ok(builder.get_struct(<Self as crate::traits::HasStructSize>::STRUCT_SIZE, default)?.into())
}
}

impl <'a,> crate::traits::SetPointerBuilder for Reader<'a,> {
fn set_pointer_builder(mut pointer: crate::private::layout::PointerBuilder<'_>, value: Self, canonicalize: bool) -> crate::Result<()> { pointer.set_struct(&value.reader, canonicalize) }
}

impl <'a,> Builder<'a,> {
pub fn into_reader(self) -> Reader<'a,> {
self.builder.into_reader().into()
}
pub fn reborrow(&mut self) -> Builder<'_,> {
Builder { builder: self.builder.reborrow() }
}
pub fn reborrow_as_reader(&self) -> Reader<'_,> {
self.builder.as_reader().into()
}

pub fn total_size(&self) -> crate::Result<crate::MessageSize> {
self.builder.as_reader().total_size()
}
#[inline]
pub fn get_path(self) -> crate::Result<crate::text::Builder<'a>> {
crate::traits::FromPointerBuilder::get_from_pointer(self.builder.get_pointer_field(0), ::core::option::Option::None)
}
#[inline]
pub fn set_path(&mut self, value: crate::text::Reader<'_>) {
self.builder.reborrow().get_pointer_field(0).set_text(value);
}
#[inline]
pub fn init_path(self, size: u32) -> crate::text::Builder<'a> {
self.builder.get_pointer_field(0).init_text(size)
}
#[inline]
pub fn has_path(&self) -> bool {
!self.builder.is_pointer_field_null(0)
}
#[inline]
pub fn get_crate(self) -> crate::Result<crate::text::Builder<'a>> {
crate::traits::FromPointerBuilder::get_from_pointer(self.builder.get_pointer_field(1), ::core::option::Option::None)
}
#[inline]
pub fn set_crate(&mut self, value: crate::text::Reader<'_>) {
self.builder.reborrow().get_pointer_field(1).set_text(value);
}
#[inline]
pub fn init_crate(self, size: u32) -> crate::text::Builder<'a> {
self.builder.get_pointer_field(1).init_text(size)
}
#[inline]
pub fn has_crate(&self) -> bool {
!self.builder.is_pointer_field_null(1)
}
}

pub struct Pipeline { _typeless: crate::any_pointer::Pipeline }
impl crate::capability::FromTypelessPipeline for Pipeline {
fn new(typeless: crate::any_pointer::Pipeline) -> Self {
Self { _typeless: typeless, }
}
}
impl Pipeline {
}
mod _private {
pub static ENCODED_NODE: [crate::Word; 47] = [
crate::word(0, 0, 0, 0, 5, 0, 6, 0),
crate::word(98, 59, 209, 191, 187, 136, 72, 196),
crate::word(11, 0, 0, 0, 1, 0, 0, 0),
crate::word(131, 208, 141, 60, 76, 193, 179, 131),
crate::word(2, 0, 7, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(21, 0, 0, 0, 146, 0, 0, 0),
crate::word(29, 0, 0, 0, 7, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(25, 0, 0, 0, 119, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(114, 117, 115, 116, 46, 99, 97, 112),
crate::word(110, 112, 58, 73, 109, 112, 111, 114),
crate::word(116, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 1, 0, 1, 0),
crate::word(8, 0, 0, 0, 3, 0, 4, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 1, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(41, 0, 0, 0, 42, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(36, 0, 0, 0, 3, 0, 1, 0),
crate::word(48, 0, 0, 0, 2, 0, 1, 0),
crate::word(1, 0, 0, 0, 1, 0, 0, 0),
crate::word(0, 0, 1, 0, 1, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(45, 0, 0, 0, 50, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(40, 0, 0, 0, 3, 0, 1, 0),
crate::word(52, 0, 0, 0, 2, 0, 1, 0),
crate::word(112, 97, 116, 104, 0, 0, 0, 0),
crate::word(12, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(12, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(99, 114, 97, 116, 101, 0, 0, 0),
crate::word(12, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(12, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
crate::word(0, 0, 0, 0, 0, 0, 0, 0),
];
pub fn get_field_types(index: u16) -> crate::introspect::Type {
match index {
0 => <crate::text::Owned as crate::introspect::Introspect>::introspect(),
1 => <crate::text::Owned as crate::introspect::Introspect>::introspect(),
_ => panic!("invalid field index {}", index),
}
}
pub fn get_annotation_types(child_index: Option<u16>, index: u32) -> crate::introspect::Type {
panic!("invalid annotation indices ({:?}, {}) ", child_index, index)
}
pub static RAW_SCHEMA: crate::introspect::RawStructSchema = crate::introspect::RawStructSchema {
encoded_node: &ENCODED_NODE,
nonunion_members: NONUNION_MEMBERS,
members_by_discriminant: MEMBERS_BY_DISCRIMINANT,
};
pub static NONUNION_MEMBERS : &[u16] = &[0,1];
pub static MEMBERS_BY_DISCRIMINANT : &[u16] = &[];
pub const TYPE_ID: u64 = 0xc448_88bb_bfd1_3b62;
}
}
pub mod crate_ {
pub const ID: u64 = 0xc1763f46d790815c;
pub fn get_type() -> crate::introspect::Type { <crate::text::Owned as crate::introspect::Introspect>::introspect() }
}
pub mod option {
pub const ID: u64 = 0xabfef22c4ee1964e;
pub fn get_type() -> crate::introspect::Type { <() as crate::introspect::Introspect>::introspect() }
}
26 changes: 26 additions & 0 deletions capnpc/rust.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ annotation parentModule @0xabee386cd1450364 (file) :Text;
# }
# }

annotation imports @0xc3b9fe42a83105cd (file) :List(Import);
# Allows specifying that the generated code for an import is located in another
# crate.
#
# You only need this if your code uses imported types whose generated code is
# in another crate. You can only use this annotation once in all the files
# compiled together.
#
# using Json = import "/capnp/compat/json.capnp";
#
# $Rust.imports([
# (path = "/capnp/compat/json.capnp", crate = "capnp_json")
# ]);

struct Import {
path @0 :Text;
crate @1 :Text;
}

annotation crate @0xc1763f46d790815c (file) :Text;
# The Rust crate that provides the generated code.
#
# You need this if you're providing a library to be used by other crates. If
# you're only using the generated code in your own crate then you don't need to
# change from the default.

annotation option @0xabfef22c4ee1964e (field) :Void;
# Make the generated getters return Option<T> instead of T. Supported on
# pointer types (e.g. structs, lists, and blobs).
Expand Down
Loading

0 comments on commit a0c37e8

Please sign in to comment.