Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove function pointer comparisons #537

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions capnp/src/data_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ impl<'a> From<Reader<'a>> 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 }
}
}
Expand All @@ -238,10 +237,9 @@ impl<'a> From<Builder<'a>> 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,
}
Expand Down
5 changes: 3 additions & 2 deletions capnp/src/dynamic_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 14 additions & 7 deletions capnp/src/dynamic_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ impl<'a> Reader<'a> {
}

pub fn get(self, field: Field) -> Result<dynamic_value::Reader<'a>> {
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) => {
Expand Down Expand Up @@ -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<bool> {
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 {
Expand Down Expand Up @@ -289,7 +291,8 @@ impl<'a> Builder<'a> {
}

pub fn get(self, field: Field) -> Result<dynamic_value::Builder<'a>> {
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) => {
Expand Down Expand Up @@ -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()? {
Expand Down Expand Up @@ -586,7 +590,8 @@ impl<'a> Builder<'a> {
}

pub fn init(mut self, field: Field) -> Result<dynamic_value::Builder<'a>> {
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()? {
Expand Down Expand Up @@ -627,7 +632,8 @@ impl<'a> Builder<'a> {
}

pub fn initn(mut self, field: Field, size: u32) -> Result<dynamic_value::Builder<'a>> {
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()? {
Expand Down Expand Up @@ -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()? {
Expand Down
11 changes: 6 additions & 5 deletions capnp/src/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -194,7 +194,7 @@ impl From<TypeVariant> for Type {
}

/// A Cap'n Proto type, excluding `List`.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Clone, Debug)]
enum BaseType {
Void,
Bool,
Expand Down Expand Up @@ -270,15 +270,16 @@ pub struct RawBrandedStructSchema {
pub annotation_types: fn(Option<u16>, 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> {
Expand Down
14 changes: 6 additions & 8 deletions capnp/src/text_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,9 @@ impl<'a> From<Reader<'a>> 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 }
}
}
Expand All @@ -273,10 +272,9 @@ impl<'a> From<Builder<'a>> 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,
}
Expand Down
6 changes: 4 additions & 2 deletions example/fill_random_values/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ impl<R: Rng> Filler<R> {
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(),
Expand Down
Loading