decouple FactoryBase from MentionableTop

This commit is contained in:
AF 2023-07-29 09:23:19 +00:00
parent f7ad44920f
commit d75e7c05a8
7 changed files with 38 additions and 16 deletions

View File

@ -73,13 +73,13 @@ pub trait MentionableTop<'a, Ctx: Context<'a>>: 'a {
pub trait Mentionable<'a, Ctx: Context<'a>>:
MentionableBase<'a, Ctx, Fctr = Self::_Fctr> + MentionableTop<'a, Ctx>
{
type _Fctr: Factory<'a, Ctx, Mtbl = Self>;
type _Fctr: Factory<'a, Ctx, _Mtbl = Self>;
}
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx> + MentionableTop<'a, Ctx>>
Mentionable<'a, Ctx> for A
where
Self::Fctr: Factory<'a, Ctx>,
Self::Fctr: Factory<'a, Ctx, _Mtbl = Self>,
{
type _Fctr = Self::Fctr;
}
@ -96,14 +96,18 @@ pub type ParseResultA<'a, Ctx, A> = Result<A, ParseErrorA<'a, Ctx, A>>;
/// [Factory] base.
pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
/// Type of the associated objects.
type Mtbl: MentionableBase<'a, Ctx, Fctr = Self> + MentionableTop<'a, Ctx>;
type Mtbl: MentionableBase<'a, Ctx, Fctr = Self>;
/// Type of an error that [`Factory::deserialize`] can fail with.
type ParseError: 'a + Error;
}
/// Trait representing deserialisation rules for [Mentionable]s.
/// Crucial for [`crate::rstd::typeless`].
pub trait Factory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> + ParseMode {
pub trait Factory<'a, Ctx: Context<'a>>:
FactoryBase<'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.

View File

@ -30,7 +30,10 @@ pub trait FactoryProxy<'a, Ctx: Context<'a>> {
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;
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
<F::WithMode as FactoryProxy<'a, Ctx>>::pdeserialize(self, inctx)
}

View File

@ -38,7 +38,7 @@ impl<A: AtomicBase> Serializable for AtomicObject<A> {
pub trait AoProxy<'a, Ctx: Context<'a>>: AtomicProxy {
type Mtbl: MentionableBase<'a, Ctx, Fctr = Self::Fctr> + MentionableTop<'a, Ctx>;
type Fctr: Factory<'a, Ctx, Mtbl = Self::Mtbl>;
type Fctr: Factory<'a, Ctx, _Mtbl = Self::Mtbl>;
fn factory() -> Self::Fctr;
fn wrap(a: Self::A) -> Self::Mtbl;

View File

@ -58,6 +58,8 @@ where
impl<'a, Ctx: Context<'a>, F: RegularFactory<'a, Ctx>>
StackCompatibleProxy<'a, Ctx, Stack<'a, Ctx, Mtbl<'a, Ctx, F>>> for WithMode<F, RegularMode>
where
F::Mtbl: MentionableTop<'a, Ctx>,
{
fn points_typed_rest(
stack: &Stack<'a, Ctx, Mtbl<'a, Ctx, Self::F>>,
@ -69,6 +71,8 @@ impl<'a, Ctx: Context<'a>, F: RegularFactory<'a, Ctx>>
impl<'a, Ctx: Context<'a>, F: InlineableFactory<'a, Ctx>>
StackCompatibleProxy<'a, Ctx, Stack<'a, Ctx, Mtbl<'a, Ctx, F>>> for WithMode<F, InliningMode>
where
F::Mtbl: MentionableTop<'a, Ctx>,
{
fn points_typed_rest(
stack: &Stack<'a, Ctx, Mtbl<'a, Ctx, Self::F>>,
@ -151,6 +155,8 @@ impl<F: ParseMode> ParseMode for StackNodeFactory<F> {
impl<'a, Ctx: Context<'a>, F: RegularFactory<'a, Ctx>> RegularFactory<'a, Ctx>
for StackNodeFactory<F>
where
F::Mtbl: MentionableTop<'a, Ctx>,
{
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let (rest, inctx) = self.parse_point(inctx)?;
@ -180,7 +186,7 @@ pub type StackVecResult<'a, Ctx, A> = Result<Vec<A>, StackFaiure<'a, Ctx, A>>;
pub type StackVecWrapped<'a, Ctx, A> = Wrapped<'a, Ctx, StackVecResult<'a, Ctx, A>>;
/// Extention trait with helper methods for [Stack]s.
pub trait ExtStack<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>:
pub trait ExtStack<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>>:
MentionableBase<'a, Ctx>
{
/// Get an empty stack ([`Nullable::Null`]).
@ -201,9 +207,10 @@ pub trait ExtStackClone<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx> + Clon
fn vec(self) -> StackVecWrapped<'a, Ctx, A>;
}
impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx>> ExtStack<'a, Ctx, A> for Stack<'a, Ctx, A>
impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx> + Mentionable<'a, Ctx>> ExtStack<'a, Ctx, A>
for Stack<'a, Ctx, A>
where
Fctr<'a, Ctx, StackNode<'a, Ctx, A>>: Factory<'a, Ctx>,
StackNode<'a, Ctx, A>: Mentionable<'a, Ctx, _Fctr = StackNodeFactory<A::Fctr>>,
{
fn empty(factory: A::Fctr) -> Self {
Nullable::Null(StackNodeFactory::new(factory.clone()))
@ -222,10 +229,10 @@ where
}
}
impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx> + Clone> ExtStackClone<'a, Ctx, A>
for Stack<'a, Ctx, A>
impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx> + Mentionable<'a, Ctx> + Clone>
ExtStackClone<'a, Ctx, A> for Stack<'a, Ctx, A>
where
Fctr<'a, Ctx, StackNode<'a, Ctx, A>>: Factory<'a, Ctx>,
StackNode<'a, Ctx, A>: Mentionable<'a, Ctx, _Fctr = StackNodeFactory<A::Fctr>>,
{
fn vec(self) -> StackVecWrapped<'a, Ctx, A> {
Ctx::T::iterate_mut((vec![], self), |(mut vec, stack)| match stack {
@ -246,6 +253,8 @@ where
impl<'a, Ctx: Context<'a>, F: InlineableFactory<'a, Ctx>> InlineableFactory<'a, Ctx>
for StackNodeFactory<F>
where
F::Mtbl: MentionableTop<'a, Ctx>,
{
fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
StackParseError::Element(self.element_factory.extension_error(tail))
@ -263,6 +272,8 @@ impl<'a, Ctx: Context<'a>, F: InlineableFactory<'a, Ctx>> InlineableFactory<'a,
impl<'a, Ctx: Context<'a>, F: FixedSizeFactory<'a, Ctx>> FixedSizeFactory<'a, Ctx>
for StackNodeFactory<F>
where
F::Mtbl: MentionableTop<'a, Ctx>,
{
fn size(&self) -> usize {
Stack::<'a, Ctx, F::Mtbl>::SIZE + self.element_factory.size()
@ -271,6 +282,8 @@ impl<'a, Ctx: Context<'a>, F: FixedSizeFactory<'a, Ctx>> FixedSizeFactory<'a, Ct
impl<'a, Ctx: Context<'a>, F: ConstSizeFactory<'a, Ctx>> ConstSizeFactory<'a, Ctx>
for StackNodeFactory<F>
where
F::Mtbl: MentionableTop<'a, Ctx>,
{
const SIZE: usize = Stack::<'a, Ctx, F::Mtbl>::SIZE + F::SIZE;
}

View File

@ -28,7 +28,7 @@ impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> Serializable for Nullabl
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx>
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> MentionableBase<'a, Ctx>
for Nullable<'a, Ctx, A>
{
type Fctr = NullableFactory<A::Fctr>;
@ -56,7 +56,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableTop<'a, Ctx>
}
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for NullableFactory<F> {
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for NullableFactory<F> {
type Mtbl = Nullable<'a, Ctx, F::Mtbl>;
type ParseError = PointParseError;

View File

@ -11,7 +11,9 @@ impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> Serializable for Point<'
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx> for Point<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> MentionableBase<'a, Ctx>
for Point<'a, Ctx, A>
{
type Fctr = PointFactory<A::Fctr>;
fn factory(&self) -> Self::Fctr {
@ -71,7 +73,7 @@ impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> AsRef<[u8]> for Point<'a
}
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for PointFactory<F> {
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for PointFactory<F> {
type Mtbl = Point<'a, Ctx, F::Mtbl>;
type ParseError = PointParseError;

View File

@ -26,7 +26,7 @@ pub trait MappableOrigin<'a, Ctx: Context<'a>>: Origin<'a, Ctx> {
map_factory: impl 'a + FnOnce(OFctr<'a, Ctx, Self>) -> B::Fctr,
) -> Rc<dyn Origin<'a, Ctx, Mtbl = B>>
where
OFctr<'a, Ctx, Self>: Factory<'a, Ctx>,
OFctr<'a, Ctx, Self>: Factory<'a, Ctx, _Mtbl = Self::Mtbl>,
Self::Mtbl: MentionableTop<'a, Ctx>,
{
let origin_r = self.clone();