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

add dynamic_value::Reader::downcast_struct() #521

Merged
merged 3 commits into from
Sep 23, 2024
Merged
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
24 changes: 24 additions & 0 deletions capnp/src/data_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ 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()
);
Reader { reader: dl.reader }
}
}

impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
fn from(t: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
crate::dynamic_value::Builder::List(crate::dynamic_list::Builder {
Expand All @@ -224,6 +235,19 @@ 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()
);
Builder {
builder: dl.builder,
}
}
}

impl<'a> core::fmt::Debug for Reader<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(
Expand Down
30 changes: 27 additions & 3 deletions capnp/src/dynamic_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) fn struct_size_from_schema(schema: StructSchema) -> Result<layout::St
#[derive(Clone, Copy)]
pub struct Reader<'a> {
pub(crate) reader: layout::StructReader<'a>,
schema: StructSchema,
pub(crate) schema: StructSchema,
}

impl<'a> From<Reader<'a>> for dynamic_value::Reader<'a> {
Expand Down Expand Up @@ -232,12 +232,24 @@ impl<'a> Reader<'a> {
let field = self.schema.get_field_by_name(field_name)?;
self.has(field)
}

/// Downcasts the `Reader` into a specific struct type. Panics if the
/// expected type does not match the value.
pub fn downcast<T: crate::traits::OwnedStruct>(self) -> T::Reader<'a> {
assert!(
Into::<crate::introspect::Type>::into(crate::introspect::TypeVariant::Struct(
self.schema.raw
))
.may_downcast_to(T::introspect())
);
self.reader.into()
}
}

/// A mutable dynamically-typed struct.
pub struct Builder<'a> {
builder: layout::StructBuilder<'a>,
schema: StructSchema,
pub(crate) builder: layout::StructBuilder<'a>,
pub(crate) schema: StructSchema,
}

impl<'a> From<Builder<'a>> for dynamic_value::Builder<'a> {
Expand Down Expand Up @@ -773,6 +785,18 @@ impl<'a> Builder<'a> {
}
Ok(())
}

/// Downcasts the `Builder` into a specific struct type. Panics if the
/// expected type does not match the value.
pub fn downcast<T: crate::traits::OwnedStruct>(self) -> T::Builder<'a> {
assert!(
Into::<crate::introspect::Type>::into(crate::introspect::TypeVariant::Struct(
self.schema.raw
))
.may_downcast_to(T::introspect())
);
self.builder.into()
}
}

impl<'a> crate::traits::SetterInput<crate::any_pointer::Owned> for Reader<'a> {
Expand Down
24 changes: 24 additions & 0 deletions capnp/src/dynamic_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ impl<'a> Reader<'a> {
pub fn downcast<T: DowncastReader<'a>>(self) -> T {
T::downcast_reader(self)
}

/// Downcasts the `Reader` into a specific struct type. Panics if the
/// expected type does not match the value.
///
/// Design note: instead of this method, it would be better to add a blanket impl
/// of the `DowncastBuilder` trait that covered every struct type. Unfortunately,
/// the current way the `Introspect` and `OwnedStruct` traits are set up does not
/// seem to allow this.
pub fn downcast_struct<T: crate::traits::OwnedStruct>(self) -> T::Reader<'a> {
let sr: dynamic_struct::Reader = self.downcast();
sr.downcast::<T>()
}
}

impl<'a> From<()> for Reader<'a> {
Expand Down Expand Up @@ -215,6 +227,18 @@ impl<'a> Builder<'a> {
pub fn downcast<T: DowncastBuilder<'a>>(self) -> T {
T::downcast_builder(self)
}

/// Downcasts the `Builder` into a specific struct type. Panics if the
/// expected type does not match the value.
///
/// Design note: instead of this method, it would be better to add a blanket impl
/// of the `DowncastBuilder` trait that covered every struct type. Unfortunately,
/// the current way the `Introspect` and `OwnedStruct` traits are set up does not
/// seem to allow this.
pub fn downcast_struct<T: crate::traits::OwnedStruct>(self) -> T::Builder<'a> {
let sb: dynamic_struct::Builder = self.downcast();
sb.downcast::<T>()
}
}

/// Helper trait for the `dynamic_value::Builder::downcast()` method.
Expand Down
26 changes: 26 additions & 0 deletions capnp/src/enum_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> F
}
}

impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect>
crate::dynamic_value::DowncastReader<'a> for Reader<'a, T>
{
fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
let dl: crate::dynamic_list::Reader = v.downcast();
assert!(dl.element_type().may_downcast_to(T::introspect()));
Reader {
reader: dl.reader,
marker: PhantomData,
}
}
}

impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> From<Builder<'a, T>>
for crate::dynamic_value::Builder<'a>
{
Expand All @@ -266,6 +279,19 @@ impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> F
}
}

impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect>
crate::dynamic_value::DowncastBuilder<'a> for Builder<'a, T>
{
fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
let dl: crate::dynamic_list::Builder = v.downcast();
assert!(dl.element_type().may_downcast_to(T::introspect()));
Builder {
builder: dl.builder,
marker: PhantomData,
}
}
}

impl<'a, T: Copy + TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect>
core::fmt::Debug for Reader<'a, T>
{
Expand Down
38 changes: 37 additions & 1 deletion 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, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Type {
/// The type, minus any outer `List( )`.
base: BaseType,
Expand Down Expand Up @@ -104,6 +104,42 @@ impl Type {
)
}
}

/// Returns true is a dynamic value of type `self` is
/// allowed to be downcast to type `other`.
pub(crate) fn may_downcast_to(&self, other: Self) -> bool {
match (self.which(), other.which()) {
(TypeVariant::Void, TypeVariant::Void) => true,
(TypeVariant::UInt8, TypeVariant::UInt8) => true,
(TypeVariant::UInt16, TypeVariant::UInt16) => true,
(TypeVariant::UInt32, TypeVariant::UInt32) => true,
(TypeVariant::UInt64, TypeVariant::UInt64) => true,
(TypeVariant::Int8, TypeVariant::Int8) => true,
(TypeVariant::Int16, TypeVariant::Int16) => true,
(TypeVariant::Int32, TypeVariant::Int32) => true,
(TypeVariant::Int64, TypeVariant::Int64) => true,
(TypeVariant::Float32, TypeVariant::Float32) => true,
(TypeVariant::Float64, TypeVariant::Float64) => true,
(TypeVariant::Text, TypeVariant::Text) => true,
(TypeVariant::Data, TypeVariant::Data) => true,
(TypeVariant::Enum(es1), TypeVariant::Enum(es2)) => es1 == es2,
(TypeVariant::Struct(rbs1), TypeVariant::Struct(rbs2)) => {
// Ignore any type parameters. The original intent was that
// we would additionally check that the `field_types` fields
// were equal function pointers here. However, according to
// Miri's behavior at least, that check returns `false`
// more than we would like it to. So we settle for being
// a bit more accepting.
core::ptr::eq(rbs1.generic, rbs2.generic)
}
(TypeVariant::List(element1), TypeVariant::List(element2)) => {
element1.may_downcast_to(element2)
}
(TypeVariant::AnyPointer, TypeVariant::AnyPointer) => true,
(TypeVariant::Capability, TypeVariant::Capability) => true,
_ => false,
}
}
}

#[derive(Copy, Clone, PartialEq, Eq)]
Expand Down
22 changes: 22 additions & 0 deletions capnp/src/list_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,17 @@ impl<'a, T: crate::traits::Owned> From<Reader<'a, T>> for crate::dynamic_value::
}
}

impl<'a, T: crate::traits::Owned> crate::dynamic_value::DowncastReader<'a> for Reader<'a, T> {
fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
let dl: crate::dynamic_list::Reader = v.downcast();
assert!(dl.element_type().may_downcast_to(T::introspect()));
Reader {
reader: dl.reader,
marker: core::marker::PhantomData,
}
}
}

impl<'a, T: crate::traits::Owned> From<Builder<'a, T>> for crate::dynamic_value::Builder<'a> {
fn from(t: Builder<'a, T>) -> crate::dynamic_value::Builder<'a> {
crate::dynamic_value::Builder::List(crate::dynamic_list::Builder::new(
Expand All @@ -293,6 +304,17 @@ impl<'a, T: crate::traits::Owned> From<Builder<'a, T>> for crate::dynamic_value:
}
}

impl<'a, T: crate::traits::Owned> crate::dynamic_value::DowncastBuilder<'a> for Builder<'a, T> {
fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
let dl: crate::dynamic_list::Builder = v.downcast();
assert!(dl.element_type().may_downcast_to(T::introspect()));
Builder {
builder: dl.builder,
marker: core::marker::PhantomData,
}
}
}

impl<'a, T: crate::traits::Owned> core::fmt::Debug for Reader<'a, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(
Expand Down
26 changes: 26 additions & 0 deletions capnp/src/primitive_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ impl<'a, T: PrimitiveElement + crate::introspect::Introspect> From<Reader<'a, T>
}
}

impl<'a, T: PrimitiveElement + crate::introspect::Introspect>
crate::dynamic_value::DowncastReader<'a> for Reader<'a, T>
{
fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
let dl: crate::dynamic_list::Reader = v.downcast();
assert!(dl.element_type().may_downcast_to(T::introspect()));
Reader {
reader: dl.reader,
marker: marker::PhantomData,
}
}
}

impl<'a, T: PrimitiveElement + crate::introspect::Introspect> From<Builder<'a, T>>
for crate::dynamic_value::Builder<'a>
{
Expand All @@ -355,6 +368,19 @@ impl<'a, T: PrimitiveElement + crate::introspect::Introspect> From<Builder<'a, T
}
}

impl<'a, T: PrimitiveElement + crate::introspect::Introspect>
crate::dynamic_value::DowncastBuilder<'a> for Builder<'a, T>
{
fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
let dl: crate::dynamic_list::Builder = v.downcast();
assert!(dl.element_type().may_downcast_to(T::introspect()));
Builder {
builder: dl.builder,
marker: marker::PhantomData,
}
}
}

impl<'a, T: Copy + PrimitiveElement + crate::introspect::Introspect> core::fmt::Debug
for Reader<'a, T>
{
Expand Down
24 changes: 24 additions & 0 deletions capnp/src/struct_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,17 @@ impl<'a, T: crate::traits::OwnedStruct> From<Reader<'a, T>> for crate::dynamic_v
}
}

impl<'a, T: crate::traits::OwnedStruct> crate::dynamic_value::DowncastReader<'a> for Reader<'a, T> {
fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
let dl: crate::dynamic_list::Reader = v.downcast();
assert!(dl.element_type().may_downcast_to(T::introspect()));
Reader {
reader: dl.reader,
marker: PhantomData,
}
}
}

impl<'a, T: crate::traits::OwnedStruct> From<Builder<'a, T>> for crate::dynamic_value::Builder<'a> {
fn from(t: Builder<'a, T>) -> crate::dynamic_value::Builder<'a> {
crate::dynamic_value::Builder::List(crate::dynamic_list::Builder::new(
Expand All @@ -300,6 +311,19 @@ impl<'a, T: crate::traits::OwnedStruct> From<Builder<'a, T>> for crate::dynamic_
}
}

impl<'a, T: crate::traits::OwnedStruct> crate::dynamic_value::DowncastBuilder<'a>
for Builder<'a, T>
{
fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
let dl: crate::dynamic_list::Builder = v.downcast();
assert!(dl.element_type().may_downcast_to(T::introspect()));
Builder {
builder: dl.builder,
marker: PhantomData,
}
}
}

impl<'a, T: crate::traits::OwnedStruct> core::fmt::Debug for Reader<'a, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(
Expand Down
24 changes: 24 additions & 0 deletions capnp/src/text_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ 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()
);
Reader { reader: dl.reader }
}
}

impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
fn from(t: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
crate::dynamic_value::Builder::List(crate::dynamic_list::Builder {
Expand All @@ -259,6 +270,19 @@ 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()
);
Builder {
builder: dl.builder,
}
}
}

impl<'a> core::fmt::Debug for Reader<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(
Expand Down
Loading
Loading