Skip to content

Commit

Permalink
Use Debug constraint on Owned
Browse files Browse the repository at this point in the history
  • Loading branch information
as-com committed Apr 18, 2023
1 parent 5af87c0 commit 5c3ce2a
Show file tree
Hide file tree
Showing 10 changed files with 1,075 additions and 201 deletions.
394 changes: 333 additions & 61 deletions capnp-rpc/src/rpc_capnp.rs

Large diffs are not rendered by default.

102 changes: 83 additions & 19 deletions capnp-rpc/src/rpc_twoparty_capnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum Side {
}
impl ::core::convert::TryFrom<u16> for Side {
type Error = ::capnp::NotInSchema;
fn try_from(value: u16) -> ::core::result::Result<Self, Self::Error> {
fn try_from(value: u16) -> ::core::result::Result<Self, <Side as ::core::convert::TryFrom<u16>>::Error> {
match value {
0 => ::core::result::Result::Ok(Self::Server),
1 => ::core::result::Result::Ok(Self::Client),
Expand All @@ -34,8 +34,11 @@ pub mod vat_id {
impl ::capnp::traits::OwnedStruct for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
impl ::capnp::traits::Pipelined for Owned { type Pipeline = Pipeline; }

#[derive(Clone, Copy)]
pub struct Reader<'a> { reader: ::capnp::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,> ::capnp::traits::HasTypeId for Reader<'a,> {
const TYPE_ID: u64 = _private::TYPE_ID;
Expand All @@ -47,7 +50,7 @@ pub mod vat_id {
}

impl <'a,> ::capnp::traits::FromPointerReader<'a> for Reader<'a,> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(reader.get_struct(default)?.into())
}
}
Expand Down Expand Up @@ -77,6 +80,13 @@ pub mod vat_id {
::core::convert::TryInto::try_into(self.reader.get_data_field::<u16>(0))
}
}
impl <'a,> ::core::fmt::Debug for Reader<'a,> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut s = f.debug_struct("vat_id");
s.field("side", &self.get_side());
s.finish()
}
}

pub struct Builder<'a> { builder: ::capnp::private::layout::StructBuilder<'a> }
impl <'a,> ::capnp::traits::HasStructSize for Builder<'a,> {
Expand All @@ -101,7 +111,7 @@ pub mod vat_id {
fn init_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, _size: u32) -> Self {
builder.init_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE).into()
}
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(builder.get_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE, default)?.into())
}
}
Expand Down Expand Up @@ -154,8 +164,11 @@ pub mod provision_id {
impl ::capnp::traits::OwnedStruct for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
impl ::capnp::traits::Pipelined for Owned { type Pipeline = Pipeline; }

#[derive(Clone, Copy)]
pub struct Reader<'a> { reader: ::capnp::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,> ::capnp::traits::HasTypeId for Reader<'a,> {
const TYPE_ID: u64 = _private::TYPE_ID;
Expand All @@ -167,7 +180,7 @@ pub mod provision_id {
}

impl <'a,> ::capnp::traits::FromPointerReader<'a> for Reader<'a,> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(reader.get_struct(default)?.into())
}
}
Expand Down Expand Up @@ -197,6 +210,13 @@ pub mod provision_id {
self.reader.get_data_field::<u32>(0)
}
}
impl <'a,> ::core::fmt::Debug for Reader<'a,> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut s = f.debug_struct("provision_id");
s.field("join_id", &self.get_join_id());
s.finish()
}
}

pub struct Builder<'a> { builder: ::capnp::private::layout::StructBuilder<'a> }
impl <'a,> ::capnp::traits::HasStructSize for Builder<'a,> {
Expand All @@ -221,7 +241,7 @@ pub mod provision_id {
fn init_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, _size: u32) -> Self {
builder.init_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE).into()
}
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(builder.get_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE, default)?.into())
}
}
Expand Down Expand Up @@ -274,8 +294,11 @@ pub mod recipient_id {
impl ::capnp::traits::OwnedStruct for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
impl ::capnp::traits::Pipelined for Owned { type Pipeline = Pipeline; }

#[derive(Clone, Copy)]
pub struct Reader<'a> { reader: ::capnp::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,> ::capnp::traits::HasTypeId for Reader<'a,> {
const TYPE_ID: u64 = _private::TYPE_ID;
Expand All @@ -287,7 +310,7 @@ pub mod recipient_id {
}

impl <'a,> ::capnp::traits::FromPointerReader<'a> for Reader<'a,> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(reader.get_struct(default)?.into())
}
}
Expand All @@ -313,6 +336,12 @@ pub mod recipient_id {
self.reader.total_size()
}
}
impl <'a,> ::core::fmt::Debug for Reader<'a,> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut s = f.debug_struct("recipient_id");
s.finish()
}
}

pub struct Builder<'a> { builder: ::capnp::private::layout::StructBuilder<'a> }
impl <'a,> ::capnp::traits::HasStructSize for Builder<'a,> {
Expand All @@ -337,7 +366,7 @@ pub mod recipient_id {
fn init_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, _size: u32) -> Self {
builder.init_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE).into()
}
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(builder.get_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE, default)?.into())
}
}
Expand Down Expand Up @@ -382,8 +411,11 @@ pub mod third_party_cap_id {
impl ::capnp::traits::OwnedStruct for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
impl ::capnp::traits::Pipelined for Owned { type Pipeline = Pipeline; }

#[derive(Clone, Copy)]
pub struct Reader<'a> { reader: ::capnp::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,> ::capnp::traits::HasTypeId for Reader<'a,> {
const TYPE_ID: u64 = _private::TYPE_ID;
Expand All @@ -395,7 +427,7 @@ pub mod third_party_cap_id {
}

impl <'a,> ::capnp::traits::FromPointerReader<'a> for Reader<'a,> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(reader.get_struct(default)?.into())
}
}
Expand All @@ -421,6 +453,12 @@ pub mod third_party_cap_id {
self.reader.total_size()
}
}
impl <'a,> ::core::fmt::Debug for Reader<'a,> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut s = f.debug_struct("third_party_cap_id");
s.finish()
}
}

pub struct Builder<'a> { builder: ::capnp::private::layout::StructBuilder<'a> }
impl <'a,> ::capnp::traits::HasStructSize for Builder<'a,> {
Expand All @@ -445,7 +483,7 @@ pub mod third_party_cap_id {
fn init_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, _size: u32) -> Self {
builder.init_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE).into()
}
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(builder.get_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE, default)?.into())
}
}
Expand Down Expand Up @@ -490,8 +528,11 @@ pub mod join_key_part {
impl ::capnp::traits::OwnedStruct for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
impl ::capnp::traits::Pipelined for Owned { type Pipeline = Pipeline; }

#[derive(Clone, Copy)]
pub struct Reader<'a> { reader: ::capnp::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,> ::capnp::traits::HasTypeId for Reader<'a,> {
const TYPE_ID: u64 = _private::TYPE_ID;
Expand All @@ -503,7 +544,7 @@ pub mod join_key_part {
}

impl <'a,> ::capnp::traits::FromPointerReader<'a> for Reader<'a,> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(reader.get_struct(default)?.into())
}
}
Expand Down Expand Up @@ -541,6 +582,15 @@ pub mod join_key_part {
self.reader.get_data_field::<u16>(3)
}
}
impl <'a,> ::core::fmt::Debug for Reader<'a,> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut s = f.debug_struct("join_key_part");
s.field("join_id", &self.get_join_id());
s.field("part_count", &self.get_part_count());
s.field("part_num", &self.get_part_num());
s.finish()
}
}

pub struct Builder<'a> { builder: ::capnp::private::layout::StructBuilder<'a> }
impl <'a,> ::capnp::traits::HasStructSize for Builder<'a,> {
Expand All @@ -565,7 +615,7 @@ pub mod join_key_part {
fn init_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, _size: u32) -> Self {
builder.init_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE).into()
}
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(builder.get_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE, default)?.into())
}
}
Expand Down Expand Up @@ -634,8 +684,11 @@ pub mod join_result {
impl ::capnp::traits::OwnedStruct for Owned { type Reader<'a> = Reader<'a>; type Builder<'a> = Builder<'a>; }
impl ::capnp::traits::Pipelined for Owned { type Pipeline = Pipeline; }

#[derive(Clone, Copy)]
pub struct Reader<'a> { reader: ::capnp::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,> ::capnp::traits::HasTypeId for Reader<'a,> {
const TYPE_ID: u64 = _private::TYPE_ID;
Expand All @@ -647,7 +700,7 @@ pub mod join_result {
}

impl <'a,> ::capnp::traits::FromPointerReader<'a> for Reader<'a,> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(reader: &::capnp::private::layout::PointerReader<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(reader.get_struct(default)?.into())
}
}
Expand Down Expand Up @@ -689,6 +742,17 @@ pub mod join_result {
!self.reader.get_pointer_field(0).is_null()
}
}
impl <'a,> ::core::fmt::Debug for Reader<'a,> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut s = f.debug_struct("join_result");
s.field("join_id", &self.get_join_id());
s.field("succeeded", &self.get_succeeded());
if self.has_cap() {
s.field("cap", &self.get_cap())
} else { s.field("cap", &::core::option::Option::None::<()>) };
s.finish()
}
}

pub struct Builder<'a> { builder: ::capnp::private::layout::StructBuilder<'a> }
impl <'a,> ::capnp::traits::HasStructSize for Builder<'a,> {
Expand All @@ -713,7 +777,7 @@ pub mod join_result {
fn init_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, _size: u32) -> Self {
builder.init_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE).into()
}
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [capnp::Word]>) -> ::capnp::Result<Self> {
fn get_from_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, default: ::core::option::Option<&'a [::capnp::Word]>) -> ::capnp::Result<Self> {
::core::result::Result::Ok(builder.get_struct(<Self as ::capnp::traits::HasStructSize>::STRUCT_SIZE, default)?.into())
}
}
Expand Down
2 changes: 1 addition & 1 deletion capnp/src/capability_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where

impl<T> crate::traits::Owned for Owned<T>
where
T: FromClientHook,
T: FromClientHook + Debug,
{
type Reader<'a> = Reader<'a, T>;
type Builder<'a> = Builder<'a, T>;
Expand Down
2 changes: 1 addition & 1 deletion capnp/src/enum_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Owned<T> {

impl<T> crate::traits::Owned for Owned<T>
where
T: TryFrom<u16, Error = NotInSchema>,
T: TryFrom<u16, Error = NotInSchema> + Debug,
{
type Reader<'a> = Reader<'a, T>;
type Builder<'a> = Builder<'a, T>;
Expand Down
2 changes: 1 addition & 1 deletion capnp/src/primitive_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Owned<T> {

impl<T> crate::traits::Owned for Owned<T>
where
T: PrimitiveElement,
T: PrimitiveElement + Debug,
{
type Reader<'a> = Reader<'a, T>;
type Builder<'a> = Builder<'a, T>;
Expand Down
1 change: 1 addition & 0 deletions capnp/src/struct_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ where
impl<T> crate::traits::Owned for Owned<T>
where
T: crate::traits::OwnedStruct,
for<'a> <T as crate::traits::OwnedStruct>::Reader<'a>: Debug,
{
type Reader<'a> = Reader<'a, T>;
type Builder<'a> = Builder<'a, T>;
Expand Down
3 changes: 2 additions & 1 deletion capnp/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::private::layout::{
};
use crate::Result;

use core::fmt::Debug;
use core::marker::PhantomData;

pub trait HasStructSize {
Expand Down Expand Up @@ -58,7 +59,7 @@ pub trait FromPointerReader<'a>: Sized {
/// nonetheless as a type parameter, e.g. for a generic container that owns a Cap'n Proto
/// message of type `T: capnp::traits::Owned`.
pub trait Owned {
type Reader<'a>: FromPointerReader<'a> + SetPointerBuilder;
type Reader<'a>: FromPointerReader<'a> + SetPointerBuilder + Debug;
type Builder<'a>: FromPointerBuilder<'a>;
}

Expand Down
2 changes: 1 addition & 1 deletion capnpc/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,7 @@ fn generate_node(
Line("}".to_string())]))),
Indent(Box::new(Branch(reader_members))),
Line("}".to_string()),
Line(format!("impl <'a,{0}> ::core::fmt::Debug for Reader<'a,{0}> {1} {{", params.params, params.where_clause_with_debug)),
Line(format!("impl <'a,{0}> ::core::fmt::Debug for Reader<'a,{0}> {1} {{", params.params, params.where_clause)),
Indent(
Box::new(Branch(vec![
Line("fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {".to_string()),
Expand Down
10 changes: 0 additions & 10 deletions capnpc/src/codegen_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pub struct TypeParameterTexts {
pub params: String,
pub where_clause: String,
pub where_clause_with_static: String,
pub where_clause_with_debug: String,
pub pipeline_where_clause: String,
pub phantom_data_value: String,
pub phantom_data_type: String,
Expand Down Expand Up @@ -131,13 +130,6 @@ impl<'a> RustNodeInfo for node::Reader<'a> {
.collect::<Vec<String>>()
.join(", ")
+ " ");
let where_clause_with_debug = "where ".to_string()
+ &*(params
.iter()
.flat_map(|param| [format!("{param}: ::capnp::traits::Owned"), format!("<{param} as ::capnp::traits::Owned>::Reader<'a>: ::core::fmt::Debug")])
.collect::<Vec<String>>()
.join(", ")
+ " ");
let pipeline_where_clause = "where ".to_string() + &*(params.iter().map(|param| {
format!("{param}: ::capnp::traits::Pipelined, <{param} as ::capnp::traits::Pipelined>::Pipeline: ::capnp::capability::FromTypelessPipeline")
}).collect::<Vec<String>>().join(", ") + " ");
Expand All @@ -154,7 +146,6 @@ impl<'a> RustNodeInfo for node::Reader<'a> {
params: type_parameters,
where_clause,
where_clause_with_static,
where_clause_with_debug,
pipeline_where_clause,
phantom_data_type,
phantom_data_value,
Expand All @@ -165,7 +156,6 @@ impl<'a> RustNodeInfo for node::Reader<'a> {
params: "".to_string(),
where_clause: "".to_string(),
where_clause_with_static: "".to_string(),
where_clause_with_debug: "".to_string(),
pipeline_where_clause: "".to_string(),
phantom_data_type: "".to_string(),
phantom_data_value: "".to_string(),
Expand Down

0 comments on commit 5c3ce2a

Please sign in to comment.