From af813bf51fe801f35f1697638ce9c53cefdd8591 Mon Sep 17 00:00:00 2001 From: timofey Date: Sat, 29 Jul 2023 14:17:01 +0000 Subject: [PATCH] `ParseFactory` --- src/rcore.rs | 18 ++++++++++-------- src/rcore/modes.rs | 12 +++++++++--- src/rcore/resolution.rs | 2 +- src/rcore/serialization.rs | 2 +- src/rstd/atomic.rs | 4 ++-- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/rcore.rs b/src/rcore.rs index 90f6a3d..41ea785 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -87,7 +87,7 @@ where /// [`Factory`] associated with the [`Mentionable`]. Mostly useful for `type` definitions. pub type Fctr<'a, Ctx, A> = >::Fctr; -/// Shorthand for the type of vaalues returned by [`Factory::deserialize`]. +/// Shorthand for the type of values returned by [`ParseFactory::deserialize`]. pub type ParseResult<'a, Ctx, F> = Result, ParseError<'a, Ctx, F>>; /// [`ParseResult`] associated with a [`Mentionable`] (instead of a [`Factory`]). @@ -97,21 +97,23 @@ pub type ParseResultA<'a, Ctx, A> = Result>; pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone { /// Type of the associated objects. type Mtbl: MentionableBase<'a, Ctx, Fctr = Self>; - /// Type of an error that [`Factory::deserialize`] can fail with. + /// Type of an error that [`ParseFactory::deserialize`] can fail with. type ParseError: 'a + Error; } +pub trait ParseFactory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> { + /// 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. + fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>; +} + /// Trait representing deserialisation rules for [Mentionable]s. /// Crucial for [`crate::rstd::typeless`]. pub trait Factory<'a, Ctx: Context<'a>>: - FactoryBase<'a, Ctx, Mtbl = Self::_Mtbl> + ParseMode + ParseFactory<'a, Ctx, Mtbl = Self::_Mtbl> + ParseMode { type _Mtbl: MentionableBase<'a, Ctx, Fctr = Self> + MentionableTop<'a, Ctx>; - - /// 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. - fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>; } /// [`Mentionable`] associated with the [`Factory`]. Mostly useful for `type` definitions. diff --git a/src/rcore/modes.rs b/src/rcore/modes.rs index eeb5679..d855e92 100644 --- a/src/rcore/modes.rs +++ b/src/rcore/modes.rs @@ -27,13 +27,11 @@ pub trait FactoryProxy<'a, Ctx: Context<'a>> { ) -> ParseResult<'a, Ctx, Self::F>; } -impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx> + WithParseMode> Factory<'a, Ctx> for F +impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx> + WithParseMode> ParseFactory<'a, Ctx> for F where F::WithMode: FactoryProxy<'a, Ctx, F = Self>, F::Mtbl: MentionableTop<'a, Ctx>, { - type _Mtbl = Self::Mtbl; - fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { >::pdeserialize(self, inctx) } @@ -43,6 +41,14 @@ where } } +impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx> + WithParseMode> Factory<'a, Ctx> for F +where + F::WithMode: FactoryProxy<'a, Ctx, F = Self>, + F::Mtbl: MentionableTop<'a, Ctx>, +{ + type _Mtbl = Self::Mtbl; +} + pub struct RegularMode; pub trait RegularFactory<'a, Ctx: Context<'a>>: diff --git a/src/rcore/resolution.rs b/src/rcore/resolution.rs index cf520ae..5a2378d 100644 --- a/src/rcore/resolution.rs +++ b/src/rcore/resolution.rs @@ -5,7 +5,7 @@ use super::*; pub enum ResolutionError { /// Usually comes from [`Resolver::resolve`]. Lookup(L), - /// Usually comes from [`Factory::deserialize`]. + /// Usually comes from [`ParseFactory::deserialize`]. Parse(P), } diff --git a/src/rcore/serialization.rs b/src/rcore/serialization.rs index 92431e4..2b127df 100644 --- a/src/rcore/serialization.rs +++ b/src/rcore/serialization.rs @@ -30,7 +30,7 @@ impl Serializer for Vec { /// Trait representing a readable stream used for parsing. /// -/// See [`Serializer`], [`Factory::deserialize`]. +/// See [`Serializer`], [`ParseFactory::deserialize`]. pub trait Deserializer { /// Read at most `n` bytes. fn read_n(&mut self, n: usize) -> &[u8]; diff --git a/src/rstd/atomic.rs b/src/rstd/atomic.rs index e2bd80a..fbdffab 100644 --- a/src/rstd/atomic.rs +++ b/src/rstd/atomic.rs @@ -29,9 +29,9 @@ pub trait AtomicBase: 'static + Send + Sync + Send + Clone + Serializable { } pub trait Atomic: AtomicBase + ParseMode { - /// Static equivalent of [`Factory::deserialize`]. + /// Static equivalent of [`ParseFactory::deserialize`]. fn a_deserialize(inlining: impl Inlining) -> AParseResult; - /// Static equivalent of [`Factory::extend`]. + /// Static equivalent of [`ParseFactory::extend`]. fn a_extend(self, tail: &[u8]) -> AParseResult; }