From 2e596e4f34be24d87154c63426f2e9754ecd464c Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 28 Jul 2023 22:24:03 +0000 Subject: [PATCH] `AoProxy` --- src/rcore/modes.rs | 4 ++-- src/rstd/atomic/atomic_object.rs | 40 +++++++++++++++++++++++++++----- src/rstd/inlining.rs | 22 +++++++++++------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/rcore/modes.rs b/src/rcore/modes.rs index 9be318d..f3a97e7 100644 --- a/src/rcore/modes.rs +++ b/src/rcore/modes.rs @@ -19,9 +19,9 @@ pub trait WithParseMode: ParseMode { type WithMode: ?Sized; } -pub struct WithMode(PhantomData, F); +pub struct WithMode(PhantomData, T); -impl WithParseMode for F { +impl WithParseMode for T { type WithMode = WithMode::Mode>; } diff --git a/src/rstd/atomic/atomic_object.rs b/src/rstd/atomic/atomic_object.rs index 1a9ec47..3f9b3ed 100644 --- a/src/rstd/atomic/atomic_object.rs +++ b/src/rstd/atomic/atomic_object.rs @@ -36,11 +36,22 @@ impl Serializable for AtomicObject { } } -impl<'a, Ctx: Context<'a>, A: Atomic> Mentionable<'a, Ctx> for AtomicObject { - type Fctr = AtomicFactory; +pub trait AoProxy<'a, Ctx: Context<'a>>: AtomicProxy { + type Mtbl: Mentionable<'a, Ctx, Fctr = Self::Fctr>; + type Fctr: Factory<'a, Ctx, Mtbl = Self::Mtbl>; + + fn factory() -> Self::Fctr; + fn wrap(a: Self::A) -> Self::Mtbl; +} + +impl<'a, Ctx: Context<'a>, A: AtomicBase + WithParseMode> Mentionable<'a, Ctx> for AtomicObject +where + A::WithMode: AoProxy<'a, Ctx, Mtbl = Self, A = A>, +{ + type Fctr = >::Fctr; fn factory(&self) -> Self::Fctr { - AtomicFactory::new() + >::factory() } fn topology(&self) -> Hash { @@ -67,8 +78,11 @@ impl Clone for AtomicFactory { } } -impl<'a, Ctx: Context<'a>, A: Atomic> FactoryBase<'a, Ctx> for AtomicFactory { - type Mtbl = AtomicObject; +impl<'a, Ctx: Context<'a>, A: AtomicBase + WithParseMode> FactoryBase<'a, Ctx> for AtomicFactory +where + A::WithMode: AoProxy<'a, Ctx, Fctr = Self, A = A>, +{ + type Mtbl = >::Mtbl; type ParseError = A::AParseError; } @@ -77,7 +91,21 @@ impl ParseMode for AtomicFactory { type Mode = RegularMode; } -impl<'a, Ctx: Context<'a>, A: Atomic> RegularFactory<'a, Ctx> for AtomicFactory { +impl<'a, Ctx: Context<'a>, A: RegularAtomic> AoProxy<'a, Ctx> for WithMode { + type Mtbl = AtomicObject; + + type Fctr = AtomicFactory; + + fn factory() -> Self::Fctr { + AtomicFactory::new() + } + + fn wrap(a: Self::A) -> Self::Mtbl { + a.into() + } +} + +impl<'a, Ctx: Context<'a>, A: RegularAtomic> RegularFactory<'a, Ctx> for AtomicFactory { fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { Ok(A::a_deserialize(inctx)?.into()) } diff --git a/src/rstd/inlining.rs b/src/rstd/inlining.rs index bfb6e8c..c748dd5 100644 --- a/src/rstd/inlining.rs +++ b/src/rstd/inlining.rs @@ -48,7 +48,7 @@ pub trait ConstSizeObject<'a, Ctx: Context<'a>>: FixedSizeObject<'a, Ctx> { pub type ADParseResult = Result<(A, D), AParseError>; /// Atomic analogue of [`InlineableFactory`]/[`InlineableObject`]. -pub trait InlineableAtomic: AtomicBase { +pub trait InlineableAtomic: AtomicBase + ParseMode { fn a_extension_error(tail: &[u8]) -> Self::AParseError; fn a_ideserialize(inlining: D) -> ADParseResult; @@ -63,8 +63,9 @@ pub trait ConstSizeAtomic: InlineableAtomic { const SIZE: usize; } -impl<'a, Ctx: Context<'a>, A: InlineableAtomic + Atomic> InlineableFactory<'a, Ctx> - for AtomicFactory +impl<'a, Ctx: Context<'a>, A: InlineableAtomic> InlineableFactory<'a, Ctx> for AtomicFactory +where + ::WithMode: AoProxy<'a, Ctx, Fctr = Self, A = A>, { fn extension_error(&self, tail: &[u8]) -> Self::ParseError { A::a_extension_error(tail) @@ -72,20 +73,25 @@ impl<'a, Ctx: Context<'a>, A: InlineableAtomic + Atomic> InlineableFactory<'a, C fn ideserialize>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> { let (a, inctx) = A::a_ideserialize(inctx)?; - Ok((a.into(), inctx)) + Ok(( + <::WithMode as AoProxy<'a, Ctx>>::wrap(a), + inctx, + )) } } -impl<'a, Ctx: Context<'a>, A: ConstSizeAtomic + Atomic> FixedSizeFactory<'a, Ctx> - for AtomicFactory +impl<'a, Ctx: Context<'a>, A: ConstSizeAtomic> FixedSizeFactory<'a, Ctx> for AtomicFactory +where + ::WithMode: AoProxy<'a, Ctx, Fctr = Self, A = A>, { fn size(&self) -> usize { A::SIZE } } -impl<'a, Ctx: Context<'a>, A: ConstSizeAtomic + Atomic> ConstSizeFactory<'a, Ctx> - for AtomicFactory +impl<'a, Ctx: Context<'a>, A: ConstSizeAtomic> ConstSizeFactory<'a, Ctx> for AtomicFactory +where + ::WithMode: AoProxy<'a, Ctx, Fctr = Self, A = A>, { const SIZE: usize = A::SIZE; }