From d6adc543a7e769141fb3f025e2b66d9ba1989aa1 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 30 Jun 2023 22:46:38 +0000 Subject: [PATCH] `InCtx` in `Factory::deserialize` --- src/rcore.rs | 2 +- src/rcore/addresses.rs | 2 +- src/rstd/atomic.rs | 2 +- src/rstd/atomic/atomic_object.rs | 4 ++-- src/rstd/atomic/au64.rs | 11 +++++------ src/rstd/atomic/boolean.rs | 14 +++++++------- src/rstd/atomic/plain.rs | 4 ++-- src/rstd/collections/stack.rs | 8 ++++---- src/rstd/collections/tree.rs | 14 +++++++------- src/rstd/inlining.rs | 13 +++++-------- src/rstd/inlining/static_pair.rs | 8 ++++---- src/rstd/nullable.rs | 6 +++--- src/rstd/point.rs | 6 +++--- src/rstd/typeless.rs | 19 +++++++++++++------ 14 files changed, 58 insertions(+), 55 deletions(-) diff --git a/src/rcore.rs b/src/rcore.rs index 31ba66e..61b4a58 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -78,7 +78,7 @@ pub trait Factory<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone { type ParseError: 'a + Error; /// See [`Deserializer`], [`Resolver`]. - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>; + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>; /// Called by finite stream parsers if there's any data left. fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>; } diff --git a/src/rcore/addresses.rs b/src/rcore/addresses.rs index f69de36..19e9061 100644 --- a/src/rcore/addresses.rs +++ b/src/rcore/addresses.rs @@ -79,7 +79,7 @@ fn _parse_slice<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>( deserializer: &mut deserializer, resolver, addresses: &mut Addresses::start(), - })?; + } as &mut dyn DeCtx<'a, Ctx>)?; let tail = deserializer.read_all(); if tail.is_empty() { Ok(mentionable) diff --git a/src/rstd/atomic.rs b/src/rstd/atomic.rs index ea63384..b5a645d 100644 --- a/src/rstd/atomic.rs +++ b/src/rstd/atomic.rs @@ -21,7 +21,7 @@ pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable { /// Equivalent of [`Factory::ParseError`]. type AParseError: Error; /// Static equivalent of [`Factory::deserialize`]. - fn a_deserialize(deserializer: impl Inlining) -> AParseResult; + fn a_deserialize(inlining: impl Inlining) -> AParseResult; /// Static equivalent of [`Factory::extend`]. fn a_extend(self, tail: &[u8]) -> AParseResult; } diff --git a/src/rstd/atomic/atomic_object.rs b/src/rstd/atomic/atomic_object.rs index fc3ace4..74af041 100644 --- a/src/rstd/atomic/atomic_object.rs +++ b/src/rstd/atomic/atomic_object.rs @@ -72,8 +72,8 @@ impl<'a, Ctx: Context<'a>, A: Atomic> Factory<'a, Ctx> for AtomicFactory { type ParseError = A::AParseError; - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { - Ok(A::a_deserialize(dectx)?.into()) + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + Ok(A::a_deserialize(inctx)?.into()) } fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { diff --git a/src/rstd/atomic/au64.rs b/src/rstd/atomic/au64.rs index ee386da..0f75e5d 100644 --- a/src/rstd/atomic/au64.rs +++ b/src/rstd/atomic/au64.rs @@ -37,8 +37,8 @@ impl From<&[u8]> for IntParseError { impl Atomic for u64 { type AParseError = IntParseError; - fn a_deserialize(deserializer: impl Inlining) -> AParseResult { - let (n, _) = deserializer.iread_n_const::<8>(|slice| IntParseError::from(slice))?; + fn a_deserialize(inlining: impl Inlining) -> AParseResult { + let (n, _) = inlining.iread_n_const::<8>(|slice| IntParseError::from(slice))?; Ok(u64::from_le_bytes(n)) } @@ -52,10 +52,9 @@ impl InlineableAtomic for u64 { IntParseError::ExtraData(tail.len()) } - fn a_ideserialize(deserializer: D) -> ADParseResult { - let (x, deserializer) = - deserializer.iread_n_const::<8>(|slice| IntParseError::from(slice))?; - Ok((u64::from_le_bytes(x), deserializer)) + fn a_ideserialize(inlining: D) -> ADParseResult { + let (x, inlining) = inlining.iread_n_const::<8>(|slice| IntParseError::from(slice))?; + Ok((u64::from_le_bytes(x), inlining)) } } diff --git a/src/rstd/atomic/boolean.rs b/src/rstd/atomic/boolean.rs index c561ed1..597513a 100644 --- a/src/rstd/atomic/boolean.rs +++ b/src/rstd/atomic/boolean.rs @@ -44,8 +44,8 @@ impl From<&[u8]> for BooleanParseError { impl Atomic for bool { type AParseError = BooleanParseError; - fn a_deserialize(deserializer: impl Inlining) -> AParseResult { - let (byte, _) = deserializer.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?; + fn a_deserialize(inlining: impl Inlining) -> AParseResult { + let (byte, _) = inlining.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?; match byte[0] { 0 => Ok(false), 1 => Ok(true), @@ -63,12 +63,12 @@ impl InlineableAtomic for bool { BooleanParseError::ExtraData(tail.len()) } - fn a_ideserialize(deserializer: D) -> ADParseResult { - let (byte, deserializer) = - deserializer.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?; + fn a_ideserialize(inlining: D) -> ADParseResult { + let (byte, inlining) = + inlining.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?; match byte[0] { - 0 => Ok((false, deserializer)), - 1 => Ok((true, deserializer)), + 0 => Ok((false, inlining)), + 1 => Ok((true, inlining)), value => Err(BooleanParseError::OutOfBounds(value)), } } diff --git a/src/rstd/atomic/plain.rs b/src/rstd/atomic/plain.rs index 2059054..f990d56 100644 --- a/src/rstd/atomic/plain.rs +++ b/src/rstd/atomic/plain.rs @@ -31,8 +31,8 @@ impl Serializable for Plain { impl Atomic for Plain { type AParseError = PlainParseError; - fn a_deserialize(deserializer: impl Inlining) -> AParseResult { - Ok(deserializer.iread_all(Plain::from_slice)) + fn a_deserialize(inlining: impl Inlining) -> AParseResult { + Ok(inlining.iread_all(Plain::from_slice)) } fn a_extend(mut self, tail: &[u8]) -> AParseResult { diff --git a/src/rstd/collections/stack.rs b/src/rstd/collections/stack.rs index 9ccaea4..4836e44 100644 --- a/src/rstd/collections/stack.rs +++ b/src/rstd/collections/stack.rs @@ -74,13 +74,13 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFa type ParseError = StackParseError>; - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { - let rest = NullableFactory::new(self.clone()) - .deserialize(dectx) + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + let (rest, inctx) = NullableFactory::new(self.clone()) + .ideserialize(inctx) .map_err(StackParseError::Point)?; let element = self .element_factory - .deserialize(dectx) + .deserialize(inctx) .map_err(StackParseError::Element)?; Ok(StackNode { rest, element }) } diff --git a/src/rstd/collections/tree.rs b/src/rstd/collections/tree.rs index c3fa7d7..2954276 100644 --- a/src/rstd/collections/tree.rs +++ b/src/rstd/collections/tree.rs @@ -136,11 +136,11 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NodeFactory type ParseError = TreeParseError; - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { let tree_factory = TreeFactory(NullableFactory::new(self.clone())); - let l = tree_factory.deserialize(dectx)?; - let r = tree_factory.deserialize(dectx)?; - let key = self.0.deserialize(dectx).map_err(TreeParseError::Key)?; + let (l, inctx) = tree_factory.ideserialize(inctx)?; + let (r, inctx) = tree_factory.ideserialize(inctx)?; + let key = self.0.deserialize(inctx).map_err(TreeParseError::Key)?; Ok(Node { l, r, key }) } @@ -158,9 +158,9 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for TreeFactory type ParseError = TreeParseError; - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { - let node = self.0.deserialize(dectx)?; - let height = u64::a_deserialize(dectx)?; + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + let (node, inctx) = self.0.ideserialize(inctx)?; + let height = u64::a_deserialize(inctx)?; let tree = Tree { node, height }; tree.validate_height()?; Ok(tree) diff --git a/src/rstd/inlining.rs b/src/rstd/inlining.rs index be9679f..69c269c 100644 --- a/src/rstd/inlining.rs +++ b/src/rstd/inlining.rs @@ -121,7 +121,7 @@ pub type ADParseResult = Result<(A, D), ::AParseError>; pub trait InlineableAtomic: Atomic { fn a_extension_error(tail: &[u8]) -> Self::AParseError; - fn a_ideserialize(deserializer: D) -> ADParseResult; + fn a_ideserialize(inlining: D) -> ADParseResult; } /// Atomic analogue of [`ConstSizeFactory`]/[`ConstSizeObject`]. @@ -225,14 +225,11 @@ pub type CheckedParseResult<'a, Ctx, F> = /// Extension trait for factories that ensures fixed size read. pub trait CheckedParse<'a, Ctx: Context<'a>>: FixedSizeFactory<'a, Ctx> { /// Verify proper read length using [`Deserializer::tell`]. - fn deserialize_checked( - &self, - dectx: &mut dyn DeCtx<'a, Ctx>, - ) -> CheckedParseResult<'a, Ctx, Self> { + fn deserialize_checked(&self, inctx: impl InCtx<'a, Ctx>) -> CheckedParseResult<'a, Ctx, Self> { let expected_size = self.size(); - let start = dectx.deserializer().tell(); - let result = self.deserialize(dectx)?; - let end = dectx.deserializer().tell(); + let start = inctx.itell(); + let (result, inctx) = self.ideserialize(inctx)?; + let end = inctx.itell(); let received_size = end - start; if received_size == expected_size { Ok(result) diff --git a/src/rstd/inlining/static_pair.rs b/src/rstd/inlining/static_pair.rs index 81cc144..ab75a8c 100644 --- a/src/rstd/inlining/static_pair.rs +++ b/src/rstd/inlining/static_pair.rs @@ -131,13 +131,13 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx> type ParseError = SP::ParseError; - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { let (fa, fb) = SP::factories(&self.factory_data); - let a = fa - .deserialize(dectx) + let (a, inctx) = fa + .ideserialize(inctx) .map_err(|e| SP::from_error_a(&self.factory_data, e))?; let b = fb - .deserialize(dectx) + .deserialize(inctx) .map_err(|e| SP::from_error_b(&self.factory_data, e))?; Ok(StaticPairObject { pair: SP::from_parsed(&self.factory_data, a, b), diff --git a/src/rstd/nullable.rs b/src/rstd/nullable.rs index 323e149..bd9597e 100644 --- a/src/rstd/nullable.rs +++ b/src/rstd/nullable.rs @@ -55,13 +55,13 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NullableFac type ParseError = PointParseError; - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { let factory = self.factory.clone(); - let address = dectx.next_address()?; + let (address, inctx) = inctx.icnext_address(|slice| PointParseError::from(slice))?; Ok(if address.point == HASH_ZEROS { Nullable::Null(factory) } else { - Nullable::NotNull(Point::from_address(address, factory, dectx.resolver())) + Nullable::NotNull(Point::from_address(address, factory, inctx.iresolver())) }) } diff --git a/src/rstd/point.rs b/src/rstd/point.rs index 89e92d1..effd113 100644 --- a/src/rstd/point.rs +++ b/src/rstd/point.rs @@ -73,9 +73,9 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for PointFactor type ParseError = PointParseError; - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { - let (addresses, deserializer, resolver) = dectx.adr(); - Ok(addresses.next_point(deserializer, resolver.clone(), self.factory.clone())?) + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + let (point, _) = inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice))?; + Ok(point) } fn extend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { diff --git a/src/rstd/typeless.rs b/src/rstd/typeless.rs index c9b9a51..4774ad7 100644 --- a/src/rstd/typeless.rs +++ b/src/rstd/typeless.rs @@ -1,6 +1,6 @@ //! Previously part of [`crate::rcore`]. -use super::{cast::CastError, wrapped_origin::*, *}; +use super::{cast::CastError, inlining::*, wrapped_origin::*, *}; type TypelessSerialize<'a> = dyn 'a + Fn(&mut dyn Serializer); @@ -17,7 +17,9 @@ type TypelessParsed<'a, Ctx> = Result, Box>: 'a + Send + Sync { fn clone_box(&self) -> TdeBox<'a, Ctx>; - fn de(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> TypelessParsed<'a, Ctx>; + fn de<'c>(&self, indyn: Box>) -> TypelessParsed<'a, Ctx> + where + 'a: 'c; } type TdeBox<'a, Ctx> = Box>; @@ -90,8 +92,10 @@ impl<'a, Ctx: Context<'a>> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> { type ParseError = TypelessError<'a>; - fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { - self.t_deserialize.de(dectx).map_err(TypelessError) + fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + self.t_deserialize + .de(Box::new(inctx)) + .map_err(TypelessError) } fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { @@ -124,8 +128,11 @@ where Box::new(self.clone()) } - fn de(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> TypelessParsed<'a, Ctx> { - self.deserialize(dectx) + fn de<'c>(&self, indyn: Box>) -> TypelessParsed<'a, Ctx> + where + 'a: 'c, + { + self.deserialize(indyn) .map_err(|e| Box::new(e) as Box) .map(Rc::new) .map(TypelessMentionable::from_typed)