From 8ecc4678f7b1079e287096a3766184d7122498c6 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 16 Jun 2023 06:48:44 +0000 Subject: [PATCH] add `mentionable` to `unexpected_tail` --- src/rcore.rs | 2 +- src/rcore/addresses.rs | 2 +- src/rstd/atomic.rs | 8 ++--- src/rstd/atomic/atomic_object.rs | 4 +-- src/rstd/atomic/au64.rs | 2 +- src/rstd/atomic/boolean.rs | 2 +- src/rstd/atomic/plain.rs | 2 +- src/rstd/collections/avl.rs | 8 ++--- src/rstd/collections/pair.rs | 4 +++ src/rstd/collections/stack.rs | 7 +++-- src/rstd/inlining/static_pair.rs | 6 ++-- src/rstd/nullable.rs | 2 +- src/rstd/point.rs | 2 +- src/rstd/typeless.rs | 50 ++++++++++++++++++++++++-------- 14 files changed, 68 insertions(+), 33 deletions(-) diff --git a/src/rcore.rs b/src/rcore.rs index b6953b9..a8581f7 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -76,7 +76,7 @@ pub trait Factory<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone { addresses: &mut Addresses, ) -> ParseResult<'a, Ctx, Self>; /// Called by finite stream parsers if there's any data left. - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError; + fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError; } pub type Mtbl<'a, Ctx, F> = >::Mtbl; diff --git a/src/rcore/addresses.rs b/src/rcore/addresses.rs index 448d49a..33ca875 100644 --- a/src/rcore/addresses.rs +++ b/src/rcore/addresses.rs @@ -51,7 +51,7 @@ fn _parse_slice<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>( if tail.is_empty() { Ok(mentionable) } else { - Err(factory.unexpected_tail(tail)) + Err(factory.unexpected_tail(mentionable, tail)) } } diff --git a/src/rstd/atomic.rs b/src/rstd/atomic.rs index 7a506a5..15f15fb 100644 --- a/src/rstd/atomic.rs +++ b/src/rstd/atomic.rs @@ -21,17 +21,17 @@ pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable { /// Static equivalent of [`Factory::deserialize`]. fn a_deserialize(deserializer: &mut dyn Deserializer) -> Result; /// Static equivalent of [`Factory::unexpected_tail`]. - fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError; + fn a_unexpected_tail(self, tail: &[u8]) -> Self::AParseError; } fn _parse_slice(slice: &[u8]) -> Result { let mut deserializer = SliceDeserializer::from(slice); - let mentionable = A::a_deserialize(&mut deserializer)?; + let atomic = A::a_deserialize(&mut deserializer)?; let tail = deserializer.read_all(); if tail.is_empty() { - Ok(mentionable) + Ok(atomic) } else { - Err(A::a_unexpected_tail(tail)) + Err(A::a_unexpected_tail(atomic, tail)) } } diff --git a/src/rstd/atomic/atomic_object.rs b/src/rstd/atomic/atomic_object.rs index 861a0aa..bb94d52 100644 --- a/src/rstd/atomic/atomic_object.rs +++ b/src/rstd/atomic/atomic_object.rs @@ -81,8 +81,8 @@ impl<'a, Ctx: Context<'a>, A: Atomic> Factory<'a, Ctx> for AtomicFactory { Ok(A::a_deserialize(deserializer)?.into()) } - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError { - A::a_unexpected_tail(tail) + fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError { + A::a_unexpected_tail(mentionable.atomic, tail) } } diff --git a/src/rstd/atomic/au64.rs b/src/rstd/atomic/au64.rs index cfae36f..2c81293 100644 --- a/src/rstd/atomic/au64.rs +++ b/src/rstd/atomic/au64.rs @@ -40,7 +40,7 @@ impl Atomic for u64 { Ok(u64::from_le_bytes(deserializer.read_n_const::<8>()?)) } - fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError { + fn a_unexpected_tail(self, tail: &[u8]) -> Self::AParseError { IntParseError::ExtraData(tail.len()) } } diff --git a/src/rstd/atomic/boolean.rs b/src/rstd/atomic/boolean.rs index 1c39de7..740bd81 100644 --- a/src/rstd/atomic/boolean.rs +++ b/src/rstd/atomic/boolean.rs @@ -52,7 +52,7 @@ impl Atomic for bool { } } - fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError { + fn a_unexpected_tail(self, tail: &[u8]) -> Self::AParseError { BooleanParseError::ExtraData(tail.len()) } } diff --git a/src/rstd/atomic/plain.rs b/src/rstd/atomic/plain.rs index b44f913..5da29f5 100644 --- a/src/rstd/atomic/plain.rs +++ b/src/rstd/atomic/plain.rs @@ -35,7 +35,7 @@ impl Atomic for Plain { Ok(Plain::from_slice(deserializer.read_all())) } - fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError { + fn a_unexpected_tail(self, tail: &[u8]) -> Self::AParseError { panic!( "Plain must use read_all, therefore there must not be any extra tail (received {} bytes).", tail.len() diff --git a/src/rstd/collections/avl.rs b/src/rstd/collections/avl.rs index 0a26072..8ded131 100644 --- a/src/rstd/collections/avl.rs +++ b/src/rstd/collections/avl.rs @@ -149,8 +149,8 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for AvlNodeFact Ok(AvlNode { l, r, key }) } - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError { - AvlError::Key(self.0.unexpected_tail(tail)) + fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError { + AvlError::Key(self.0.unexpected_tail(mentionable.key, tail)) } } @@ -177,7 +177,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for AvlTreeFact Ok(AvlTree { node, height }) } - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError { - u64::a_unexpected_tail(tail).into() + fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError { + u64::a_unexpected_tail(mentionable.height, tail).into() } } diff --git a/src/rstd/collections/pair.rs b/src/rstd/collections/pair.rs index 506c966..d7572d8 100644 --- a/src/rstd/collections/pair.rs +++ b/src/rstd/collections/pair.rs @@ -22,6 +22,10 @@ impl StaticPairSerializable for Pair { fn elements(&self) -> (&Self::SA, &Self::SB) { (&self.a, &self.b) } + + fn into_elements(self) -> (Self::SA, Self::SB) { + (self.a, self.b) + } } #[derive(Debug)] diff --git a/src/rstd/collections/stack.rs b/src/rstd/collections/stack.rs index 9cd537f..45bb292 100644 --- a/src/rstd/collections/stack.rs +++ b/src/rstd/collections/stack.rs @@ -102,8 +102,11 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFa Ok(StackNode { rest, element }) } - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError { - StackParseError::Element(self.element_factory.unexpected_tail(tail)) + fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError { + StackParseError::Element( + self.element_factory + .unexpected_tail(mentionable.element, tail), + ) } } diff --git a/src/rstd/inlining/static_pair.rs b/src/rstd/inlining/static_pair.rs index 46e7d10..ecfd66d 100644 --- a/src/rstd/inlining/static_pair.rs +++ b/src/rstd/inlining/static_pair.rs @@ -18,6 +18,7 @@ pub trait StaticPairSerializable { /// Borrow both elements. fn elements(&self) -> (&Self::SA, &Self::SB); + fn into_elements(self) -> (Self::SA, Self::SB); } /// Trait to be implemented on object data. @@ -150,9 +151,10 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx> }) } - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError { + fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError { let (_, fb) = SP::factories(&self.factory_data); - SP::from_error_b(&self.factory_data, fb.unexpected_tail(tail)) + let (_, b) = mentionable.pair.into_elements(); + SP::from_error_b(&self.factory_data, fb.unexpected_tail(b, tail)) } } diff --git a/src/rstd/nullable.rs b/src/rstd/nullable.rs index 86cee50..6011e6b 100644 --- a/src/rstd/nullable.rs +++ b/src/rstd/nullable.rs @@ -70,7 +70,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NullableFac }) } - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError { + fn unexpected_tail(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError { PointParseError::WrongLength(HASH_SIZE + tail.len()) } } diff --git a/src/rstd/point.rs b/src/rstd/point.rs index 3245eec..cd1535a 100644 --- a/src/rstd/point.rs +++ b/src/rstd/point.rs @@ -77,7 +77,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for PointFactor Ok(addresses.next_point(deserializer, resolver, self.factory.clone())?) } - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError { + fn unexpected_tail(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError { PointParseError::WrongLength(HASH_SIZE + tail.len()) } } diff --git a/src/rstd/typeless.rs b/src/rstd/typeless.rs index ee503f3..c113a01 100644 --- a/src/rstd/typeless.rs +++ b/src/rstd/typeless.rs @@ -1,6 +1,6 @@ //! Previously part of [`crate::rcore`]. -use super::{wrapped_origin::*, *}; +use super::{cast::CastError, wrapped_origin::*, *}; type TypelessSerialize<'a> = dyn 'a + Fn(&mut dyn Serializer); @@ -30,7 +30,7 @@ type TdeBox<'a, Ctx> = Box>; trait Tut<'a, Ctx: Context<'a>>: 'a + Send + Sync { fn clone_box(&self) -> TutBox<'a, Ctx>; - fn ut(&self, tail: &[u8]) -> TypelessError<'a>; + fn ut(&self, mentionable: TypelessMentionable<'a, Ctx>, tail: &[u8]) -> TypelessError<'a>; } type TutBox<'a, Ctx> = Box>; @@ -103,12 +103,15 @@ impl<'a, Ctx: Context<'a>> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> { } } - fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError { - self.t_unexpected_tail.ut(tail) + fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError { + self.t_unexpected_tail.ut(mentionable, tail) } } -impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx> { +impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx> +where + Ctx::LookupError: From>, +{ pub fn from_typed>(mentionable: Rc) -> Self { let factory = TypelessFactory::from_typed(mentionable.factory()); let topology = mentionable.topology(); @@ -122,7 +125,10 @@ impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx> { } } -impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F { +impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F +where + Ctx::LookupError: From>, +{ fn clone_box(&self) -> TdeBox<'a, Ctx> { Box::new(self.clone()) } @@ -143,17 +149,29 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F { } } -impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F { +impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F +where + Ctx::LookupError: From>, +{ fn clone_box(&self) -> TutBox<'a, Ctx> { Box::new(self.clone()) } - fn ut(&self, tail: &[u8]) -> TypelessError<'a> { - TypelessError::from_typed(self.unexpected_tail(tail)) + fn ut(&self, mentionable: TypelessMentionable<'a, Ctx>, tail: &[u8]) -> TypelessError<'a> { + TypelessError::from_typed(self.unexpected_tail( + match mentionable.cast(self.clone()) { + Ok(m) => m, + Err(e) => return TypelessError::from_typed(e), + }, + tail, + )) } } -impl<'a, Ctx: Context<'a>> TypelessFactory<'a, Ctx> { +impl<'a, Ctx: Context<'a>> TypelessFactory<'a, Ctx> +where + Ctx::LookupError: From>, +{ pub fn from_typed>(factory: F) -> Self { TypelessFactory { t_deserialize: Box::new(factory.clone()), @@ -168,7 +186,10 @@ impl<'a> TypelessError<'a> { } } -impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> { +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> +where + Ctx::LookupError: From>, +{ /// Typeless version of the point. pub fn typeless(&self) -> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> { Point { @@ -189,7 +210,10 @@ pub trait MentionableExt<'a, Ctx: Context<'a>>: Mentionable<'a, Ctx> { fn points_vec(&self) -> Vec>>; } -impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A { +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A +where + Ctx::LookupError: From>, +{ fn points_typeless(&self, points: &mut Vec>>) { self.points_typed(points) } @@ -203,6 +227,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for impl<'a, Ctx: Context<'a>> TakesPoints<'a, Ctx> for Vec>> +where + Ctx::LookupError: From>, { fn take>(&mut self, point: &Point<'a, Ctx, A>) { self.push(point.typeless());