From d05be062c04f860dd36148da081979a60c72063d Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Mon, 6 Jan 2025 08:06:06 -0500 Subject: [PATCH] remove function pointer comparisons --- capnp/src/data_list.rs | 14 ++++++-------- capnp/src/dynamic_list.rs | 5 +++-- capnp/src/dynamic_struct.rs | 21 ++++++++++++++------- capnp/src/introspect.rs | 11 ++++++----- capnp/src/text_list.rs | 14 ++++++-------- example/fill_random_values/src/lib.rs | 6 ++++-- 6 files changed, 39 insertions(+), 32 deletions(-) diff --git a/capnp/src/data_list.rs b/capnp/src/data_list.rs index 2bfc88808..e836427a8 100644 --- a/capnp/src/data_list.rs +++ b/capnp/src/data_list.rs @@ -218,10 +218,9 @@ impl<'a> From> for crate::dynamic_value::Reader<'a> { impl<'a> crate::dynamic_value::DowncastReader<'a> for Reader<'a> { fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self { let dl: crate::dynamic_list::Reader = v.downcast(); - assert_eq!( - dl.element_type(), - crate::introspect::TypeVariant::Data.into() - ); + assert!(dl + .element_type() + .may_downcast_to(crate::introspect::TypeVariant::Data.into())); Reader { reader: dl.reader } } } @@ -238,10 +237,9 @@ impl<'a> From> for crate::dynamic_value::Builder<'a> { impl<'a> crate::dynamic_value::DowncastBuilder<'a> for Builder<'a> { fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self { let dl: crate::dynamic_list::Builder = v.downcast(); - assert_eq!( - dl.element_type(), - crate::introspect::TypeVariant::Data.into() - ); + assert!(dl + .element_type() + .may_downcast_to(crate::introspect::TypeVariant::Data.into())); Builder { builder: dl.builder, } diff --git a/capnp/src/dynamic_list.rs b/capnp/src/dynamic_list.rs index b93097381..3092b1b34 100644 --- a/capnp/src/dynamic_list.rs +++ b/capnp/src/dynamic_list.rs @@ -325,8 +325,9 @@ impl<'a> Builder<'a> { .set_data(d); Ok(()) } - (TypeVariant::Struct(ss), dynamic_value::Reader::Struct(s)) => { - assert_eq!(ss, s.get_schema().raw); + (TypeVariant::Struct(_ss), dynamic_value::Reader::Struct(s)) => { + // TODO: assertion here? + // assert_eq!(ss, s.get_schema().raw); self.builder .reborrow() .get_struct_element(index) diff --git a/capnp/src/dynamic_struct.rs b/capnp/src/dynamic_struct.rs index f464bc213..ac448000f 100644 --- a/capnp/src/dynamic_struct.rs +++ b/capnp/src/dynamic_struct.rs @@ -49,7 +49,8 @@ impl<'a> Reader<'a> { } pub fn get(self, field: Field) -> Result> { - assert_eq!(self.schema.raw, field.parent.raw); + // TODO: assertion here? + // assert_eq!(self.schema.raw, field.parent.raw); let ty = field.get_type(); match field.get_proto().which()? { field::Slot(slot) => { @@ -198,7 +199,8 @@ impl<'a> Reader<'a> { /// is active in the union and is not a null pointer. On non-union fields, /// returns `true` if the field is not a null pointer. pub fn has(&self, field: Field) -> Result { - assert_eq!(self.schema.raw, field.parent.raw); + // TODO: assertion here? + // assert_eq!(self.schema.raw, field.parent.raw); let proto = field.get_proto(); if has_discriminant_value(proto) { let node::Struct(st) = self.schema.get_proto().which()? else { @@ -289,7 +291,8 @@ impl<'a> Builder<'a> { } pub fn get(self, field: Field) -> Result> { - assert_eq!(self.schema.raw, field.parent.raw); + // TODO: assertion here? + // assert_eq!(self.schema.raw, field.parent.raw); let ty = field.get_type(); match field.get_proto().which()? { field::Slot(slot) => { @@ -446,7 +449,8 @@ impl<'a> Builder<'a> { } pub fn set(&mut self, field: Field, value: dynamic_value::Reader<'_>) -> Result<()> { - assert_eq!(self.schema.raw, field.parent.raw); + // TODO: assertion here? + // assert_eq!(self.schema.raw, field.parent.raw); self.set_in_union(field)?; let ty = field.get_type(); match field.get_proto().which()? { @@ -586,7 +590,8 @@ impl<'a> Builder<'a> { } pub fn init(mut self, field: Field) -> Result> { - assert_eq!(self.schema.raw, field.parent.raw); + // TODO: assertion here? + // assert_eq!(self.schema.raw, field.parent.raw); self.set_in_union(field)?; let ty = field.get_type(); match field.get_proto().which()? { @@ -627,7 +632,8 @@ impl<'a> Builder<'a> { } pub fn initn(mut self, field: Field, size: u32) -> Result> { - assert_eq!(self.schema.raw, field.parent.raw); + // TODO: assertion here? + //assert_eq!(self.schema.raw, field.parent.raw); self.set_in_union(field)?; let ty = field.get_type(); match field.get_proto().which()? { @@ -680,7 +686,8 @@ impl<'a> Builder<'a> { /// Clears a field, setting it to its default value. For pointer fields, /// this makes the field null. pub fn clear(&mut self, field: Field) -> Result<()> { - assert_eq!(self.schema.raw, field.parent.raw); + // TODO: assertion here? + //assert_eq!(self.schema.raw, field.parent.raw); self.set_in_union(field)?; let ty = field.get_type(); match field.get_proto().which()? { diff --git a/capnp/src/introspect.rs b/capnp/src/introspect.rs index 6208c81b5..0161f9c5e 100644 --- a/capnp/src/introspect.rs +++ b/capnp/src/introspect.rs @@ -13,7 +13,7 @@ pub trait Introspect { /// optimized to avoid heap allocation. /// /// To examine a `Type`, you should call the `which()` method. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(Copy, Clone, Debug)] pub struct Type { /// The type, minus any outer `List( )`. base: BaseType, @@ -142,7 +142,7 @@ impl Type { } } -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone)] /// A `Type` unfolded one level. Suitable for pattern matching. Can be trivially /// converted to `Type` via the `From`/`Into` traits. pub enum TypeVariant { @@ -194,7 +194,7 @@ impl From for Type { } /// A Cap'n Proto type, excluding `List`. -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, Debug)] enum BaseType { Void, Bool, @@ -270,15 +270,16 @@ pub struct RawBrandedStructSchema { pub annotation_types: fn(Option, u32) -> Type, } +/* impl core::cmp::PartialEq for RawBrandedStructSchema { fn eq(&self, other: &Self) -> bool { core::ptr::eq(self.generic, other.generic) && self.field_types == other.field_types // don't need to compare annotation_types. // that field is equal iff field_types is. } -} +}*/ -impl core::cmp::Eq for RawBrandedStructSchema {} +//impl core::cmp::Eq for RawBrandedStructSchema {} impl core::fmt::Debug for RawBrandedStructSchema { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> { diff --git a/capnp/src/text_list.rs b/capnp/src/text_list.rs index 7ea681265..d09f8a292 100644 --- a/capnp/src/text_list.rs +++ b/capnp/src/text_list.rs @@ -253,10 +253,9 @@ impl<'a> From> for crate::dynamic_value::Reader<'a> { impl<'a> crate::dynamic_value::DowncastReader<'a> for Reader<'a> { fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self { let dl: crate::dynamic_list::Reader = v.downcast(); - assert_eq!( - dl.element_type(), - crate::introspect::TypeVariant::Text.into() - ); + assert!(dl + .element_type() + .may_downcast_to(crate::introspect::TypeVariant::Text.into())); Reader { reader: dl.reader } } } @@ -273,10 +272,9 @@ impl<'a> From> for crate::dynamic_value::Builder<'a> { impl<'a> crate::dynamic_value::DowncastBuilder<'a> for Builder<'a> { fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self { let dl: crate::dynamic_list::Builder = v.downcast(); - assert_eq!( - dl.element_type(), - crate::introspect::TypeVariant::Text.into() - ); + assert!(dl + .element_type() + .may_downcast_to(crate::introspect::TypeVariant::Text.into())); Builder { builder: dl.builder, } diff --git a/example/fill_random_values/src/lib.rs b/example/fill_random_values/src/lib.rs index 8a2b247af..3c8e33467 100644 --- a/example/fill_random_values/src/lib.rs +++ b/example/fill_random_values/src/lib.rs @@ -75,12 +75,14 @@ impl Filler { let annotations = field.get_annotations()?; for annotation in annotations { if annotation.get_id() == fill_capnp::select_from::choices::ID { - if let TypeVariant::List(element_type) = annotation.get_type().which() { + if let TypeVariant::List(_element_type) = annotation.get_type().which() { + // TODO: assert that element_type matches field.get_type + /* if element_type != field.get_type() { return Err(::capnp::Error::failed( "choices annotation element type mismatch".into(), )); - } + }*/ } else { return Err(::capnp::Error::failed( "choices annotation was not of List type".into(),