From f3eaccfa779a647b35d3b3b886ce496dbd9bdaec Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 28 Jul 2023 21:06:59 +0000 Subject: [PATCH] `Factory: ModeFactory` --- src/rcore.rs | 2 +- src/rstd/atomic/atomic_object.rs | 10 +++++++--- src/rstd/collections/stack.rs | 10 +++++++--- src/rstd/collections/tree.rs | 20 ++++++++++++++------ src/rstd/inlining.rs | 15 --------------- src/rstd/inlining/static_pair.rs | 10 +++++++--- src/rstd/nullable.rs | 10 +++++++--- src/rstd/point.rs | 30 ++++++++++++++++++++++++------ src/rstd/typeless.rs | 10 +++++++--- 9 files changed, 74 insertions(+), 43 deletions(-) diff --git a/src/rcore.rs b/src/rcore.rs index 470af6a..58ee19f 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -86,7 +86,7 @@ pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone { /// Trait representing deserialisation rules for [Mentionable]s. /// Crucial for [`crate::rstd::typeless`]. -pub trait Factory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> { +pub trait Factory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> + ModeFactory { /// See [`Deserializer`], [`Resolver`]. fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>; /// Called by finite stream parsers if there's any data left. diff --git a/src/rstd/atomic/atomic_object.rs b/src/rstd/atomic/atomic_object.rs index 1c30cc2..f9eef31 100644 --- a/src/rstd/atomic/atomic_object.rs +++ b/src/rstd/atomic/atomic_object.rs @@ -73,12 +73,16 @@ impl<'a, Ctx: Context<'a>, A: Atomic> FactoryBase<'a, Ctx> for AtomicFactory type ParseError = A::AParseError; } -impl<'a, Ctx: Context<'a>, A: Atomic> Factory<'a, Ctx> for AtomicFactory { - fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { +impl ModeFactory for AtomicFactory { + type Mode = RegularFactoryMode; +} + +impl<'a, Ctx: Context<'a>, A: Atomic> RegularFactory<'a, Ctx> for AtomicFactory { + fn rdeserialize(&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> { + fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { Ok(A::a_extend(mentionable.atomic, tail)?.into()) } } diff --git a/src/rstd/collections/stack.rs b/src/rstd/collections/stack.rs index 2d2796f..a68d2c5 100644 --- a/src/rstd/collections/stack.rs +++ b/src/rstd/collections/stack.rs @@ -93,8 +93,12 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Sta type ParseError = StackParseError>; } -impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFactory { - fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { +impl ModeFactory for StackNodeFactory { + type Mode = RegularFactoryMode; +} + +impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> RegularFactory<'a, Ctx> for StackNodeFactory { + fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { let (rest, inctx) = self.parse_point(inctx)?; let element = self .element_factory @@ -103,7 +107,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFa Ok(StackNode { rest, element }) } - fn extend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { + fn rextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { mentionable.element = self .element_factory .extend(mentionable.element, tail) diff --git a/src/rstd/collections/tree.rs b/src/rstd/collections/tree.rs index 3bfdb22..7579238 100644 --- a/src/rstd/collections/tree.rs +++ b/src/rstd/collections/tree.rs @@ -137,8 +137,12 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Nod type ParseError = TreeParseError; } -impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NodeFactory { - fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { +impl ModeFactory for NodeFactory { + type Mode = RegularFactoryMode; +} + +impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> RegularFactory<'a, Ctx> for NodeFactory { + fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { let tree_factory = TreeFactory(NullableFactory::new(self.clone())); let (l, inctx) = tree_factory.ideserialize(inctx)?; let (r, inctx) = tree_factory.ideserialize(inctx)?; @@ -146,7 +150,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NodeFactory Ok(Node { l, r, key }) } - fn extend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { + fn rextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { mentionable.key = self .0 .extend(mentionable.key, tail) @@ -161,12 +165,16 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Tre type ParseError = TreeParseError; } -impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> Factory<'a, Ctx> for TreeFactory { - fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { +impl ModeFactory for TreeFactory { + type Mode = RegularFactoryMode; +} + +impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for TreeFactory { + fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { self.ideserialize(inctx).seal() } - fn extend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { + fn rextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { mentionable.height = u64::a_extend(mentionable.height, tail)?; mentionable.validate_height()?; Ok(mentionable) diff --git a/src/rstd/inlining.rs b/src/rstd/inlining.rs index 2a77e11..7b26785 100644 --- a/src/rstd/inlining.rs +++ b/src/rstd/inlining.rs @@ -6,7 +6,6 @@ use crate::rcore::*; use super::{ atomic::{atomic_object::*, *}, - point::*, *, }; @@ -232,17 +231,3 @@ impl<'a, Ctx: Context<'a>, F: AlwaysConstSize + InlineableFactory<'a, Ctx>> { const SIZE: usize = Self::_SIZE; } - -impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> InlineableFactory<'a, Ctx> for PointFactory { - fn extension_error(&self, tail: &[u8]) -> Self::ParseError { - PointParseError::WrongLength(HASH_SIZE + tail.len()) - } - - fn ideserialize>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> { - inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice)) - } -} - -impl AlwaysConstSize for PointFactory { - const _SIZE: usize = HASH_SIZE; -} diff --git a/src/rstd/inlining/static_pair.rs b/src/rstd/inlining/static_pair.rs index 36dfb4d..65a15c6 100644 --- a/src/rstd/inlining/static_pair.rs +++ b/src/rstd/inlining/static_pair.rs @@ -132,10 +132,14 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FactoryBase<'a, Ctx> type ParseError = SP::ParseError; } -impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx> +impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> ModeFactory for StaticPairFactory<'a, Ctx, SP> { + type Mode = RegularFactoryMode; +} + +impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> RegularFactory<'a, Ctx> for StaticPairFactory<'a, Ctx, SP> { - fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { let (fa, fb) = SP::factories(&self.factory_data); let (a, inctx) = fa .ideserialize(inctx) @@ -148,7 +152,7 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx> }) } - fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { + fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { let (_, fb) = SP::factories(&self.factory_data); let (a, b) = mentionable.pair.into_elements(); match fb.extend(b, tail) { diff --git a/src/rstd/nullable.rs b/src/rstd/nullable.rs index 67bd9fb..4c8b01a 100644 --- a/src/rstd/nullable.rs +++ b/src/rstd/nullable.rs @@ -56,12 +56,16 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Nul type ParseError = PointParseError; } -impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> Factory<'a, Ctx> for NullableFactory { - fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { +impl ModeFactory for NullableFactory { + type Mode = RegularFactoryMode; +} + +impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for NullableFactory { + fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { self.ideserialize(inctx).seal() } - fn extend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { + fn rextend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { Err(self.extension_error(tail)) } } diff --git a/src/rstd/point.rs b/src/rstd/point.rs index 3226a5d..e80a683 100644 --- a/src/rstd/point.rs +++ b/src/rstd/point.rs @@ -3,6 +3,7 @@ use std::{error::Error, fmt::Display}; use crate::rcore::*; +use crate::rstd::inlining::*; impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Serializable for Point<'a, Ctx, A> { fn serialize(&self, serializer: &mut dyn Serializer) { @@ -74,13 +75,30 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Poi type ParseError = PointParseError; } -impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> Factory<'a, Ctx> for PointFactory { - 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) +impl ModeFactory for PointFactory { + type Mode = RegularFactoryMode; +} + +impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for PointFactory { + fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { + self.ideserialize(inctx).seal() } - fn extend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { - Err(PointParseError::WrongLength(HASH_SIZE + tail.len())) + fn rextend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { + Err(self.extension_error(tail)) } } + +impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> InlineableFactory<'a, Ctx> for PointFactory { + fn extension_error(&self, tail: &[u8]) -> Self::ParseError { + PointParseError::WrongLength(HASH_SIZE + tail.len()) + } + + fn ideserialize>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> { + inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice)) + } +} + +impl AlwaysConstSize for PointFactory { + const _SIZE: usize = HASH_SIZE; +} diff --git a/src/rstd/typeless.rs b/src/rstd/typeless.rs index 2e42981..d46845f 100644 --- a/src/rstd/typeless.rs +++ b/src/rstd/typeless.rs @@ -93,12 +93,16 @@ impl<'a, Ctx: Context<'a>> FactoryBase<'a, Ctx> for TypelessFactory<'a, Ctx> { type ParseError = TypelessError<'a>; } -impl<'a, Ctx: Context<'a>> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> { - fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { +impl<'a, Ctx: Context<'a>> ModeFactory for TypelessFactory<'a, Ctx> { + type Mode = RegularFactoryMode; +} + +impl<'a, Ctx: Context<'a>> RegularFactory<'a, Ctx> for TypelessFactory<'a, Ctx> { + fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { self.t_deserialize.de(inctx.demote()).map_err(TypelessError) } - fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { + fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { self.t_extend.xt(mentionable, tail) } }