InliningMode

This commit is contained in:
AF 2023-07-29 00:17:27 +00:00
parent 2e596e4f34
commit 54a6912baf
22 changed files with 225 additions and 176 deletions

View File

@ -49,9 +49,9 @@ pub use self::slice_deserializer::SliceDeserializer;
pub type Wrapped<'a, Ctx, A> = WrapC<'a, A, Ctx>;
/// Fundamental trait for ADN objects.
pub trait Mentionable<'a, Ctx: Context<'a>>: 'a + Serializable {
pub trait MentionableBase<'a, Ctx: Context<'a>>: 'a + Serializable + Sized {
/// Type of the associated factory.
type Fctr: Factory<'a, Ctx, Mtbl = Self>;
type Fctr: FactoryBase<'a, Ctx, Mtbl = Self>;
/// Value of the associated factory.
fn factory(&self) -> Self::Fctr;
@ -67,8 +67,19 @@ pub trait Mentionable<'a, Ctx: Context<'a>>: 'a + Serializable {
fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>);
}
pub trait Mentionable<'a, Ctx: Context<'a>>: MentionableBase<'a, Ctx, Fctr = Self::_Fctr> {
type _Fctr: Factory<'a, Ctx, Mtbl = Self>;
}
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> Mentionable<'a, Ctx> for A
where
Self::Fctr: Factory<'a, Ctx>,
{
type _Fctr = Self::Fctr;
}
/// [`Factory`] associated with the [`Mentionable`]. Mostly useful for `type` definitions.
pub type Fctr<'a, Ctx, A> = <A as Mentionable<'a, Ctx>>::Fctr;
pub type Fctr<'a, Ctx, A> = <A as MentionableBase<'a, Ctx>>::Fctr;
/// Shorthand for the type of vaalues returned by [`Factory::deserialize`].
pub type ParseResult<'a, Ctx, F> = Result<Mtbl<'a, Ctx, F>, ParseError<'a, Ctx, F>>;
@ -79,7 +90,7 @@ 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: Mentionable<'a, Ctx, Fctr = Self>;
type Mtbl: MentionableBase<'a, Ctx, Fctr = Self>;
/// Type of an error that [`Factory::deserialize`] can fail with.
type ParseError: 'a + Error;
}

View File

@ -6,25 +6,16 @@ pub trait ParseMode {
type Mode: ?Sized;
}
pub struct RegularMode;
pub trait RegularFactory<'a, Ctx: Context<'a>>:
FactoryBase<'a, Ctx> + ParseMode<Mode = RegularMode>
{
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>;
fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>;
}
pub trait WithParseMode: ParseMode {
type WithMode: ?Sized;
}
pub struct WithMode<T: ?Sized, M: ?Sized>(PhantomData<M>, T);
impl<T: ?Sized + ParseMode> WithParseMode for T {
type WithMode = WithMode<Self, <Self as ParseMode>::Mode>;
}
pub struct WithMode<T: ?Sized, M: ?Sized>(PhantomData<M>, T);
pub trait FactoryProxy<'a, Ctx: Context<'a>> {
type F: FactoryBase<'a, Ctx> + ParseMode;
@ -36,6 +27,28 @@ 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
where
F::WithMode: FactoryProxy<'a, Ctx, F = Self>,
{
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
<F::WithMode as FactoryProxy<'a, Ctx>>::pdeserialize(self, inctx)
}
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
<F::WithMode as FactoryProxy<'a, Ctx>>::pextend(self, mentionable, tail)
}
}
pub struct RegularMode;
pub trait RegularFactory<'a, Ctx: Context<'a>>:
FactoryBase<'a, Ctx> + ParseMode<Mode = RegularMode>
{
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>;
fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>;
}
impl<'a, Ctx: Context<'a>, F: RegularFactory<'a, Ctx>> FactoryProxy<'a, Ctx>
for WithMode<F, RegularMode>
{
@ -53,16 +66,3 @@ impl<'a, Ctx: Context<'a>, F: RegularFactory<'a, Ctx>> FactoryProxy<'a, Ctx>
f.rextend(mentionable, tail)
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx> + WithParseMode> Factory<'a, Ctx> for F
where
F::WithMode: FactoryProxy<'a, Ctx, F = Self>,
{
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
<F::WithMode as FactoryProxy<'a, Ctx>>::pdeserialize(self, inctx)
}
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
<F::WithMode as FactoryProxy<'a, Ctx>>::pextend(self, mentionable, tail)
}
}

View File

@ -7,7 +7,9 @@ pub trait Origin<'a, Ctx: Context<'a>>: 'a {
/// Clone the associated factory.
fn factory(&self) -> OFctr<'a, Ctx, Self>;
/// Try resolving the value.
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl>;
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl>
where
OFctr<'a, Ctx, Self>: Factory<'a, Ctx>;
/// Try resolving the bytes. Should avoid parsing the value.
fn resolve_bytes(self: Rc<Self>) -> HashResolution<'a, Ctx>;
}

View File

@ -1,23 +1,23 @@
use super::*;
/// The main way to represent a reference in ADN.
pub struct Point<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> {
pub struct Point<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> {
/// Hash of the referred content.
/// Derived both from the serialised value ([`Serializable::serialize`])
/// and its topology ([`Mentionable::topology`]).
/// and its topology ([`MentionableBase::topology`]).
pub point: Hash,
/// [Origin] used in [`Point::resolve`].
pub origin: Rc<dyn Origin<'a, Ctx, Mtbl = A>>,
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> PartialEq for Point<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> PartialEq for Point<'a, Ctx, A> {
/// Note: this doesn't check for [Factory] equality.
fn eq(&self, other: &Self) -> bool {
self.point == other.point
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Clone for Point<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> Clone for Point<'a, Ctx, A> {
fn clone(&self) -> Self {
Self {
point: self.point,

View File

@ -1,13 +1,13 @@
use super::*;
/// Visitor used in [`Mentionable::points_typed`].
/// Visitor used in [`MentionableBase::points_typed`].
pub trait PointsVisitor<'a, Ctx: Context<'a>> {
/// Visit a [Point].
fn visit<A: Mentionable<'a, Ctx>>(&mut self, point: &Point<'a, Ctx, A>);
}
impl<'a, Ctx: Context<'a>> PointsVisitor<'a, Ctx> for Vec<u8> {
/// The only natural implementation, as used in [`Mentionable::topology`].
/// The only natural implementation, as used in [`MentionableBase::topology`].
fn visit<A: Mentionable<'a, Ctx>>(&mut self, point: &Point<'a, Ctx, A>) {
self.extend(point.point)
}

View File

@ -44,14 +44,14 @@ pub type HashResolution<'a, Ctx> = Wrapped<'a, Ctx, HashResolutionResult<'a, Ctx
#[derive(Clone, Copy, Debug)]
pub struct Address {
pub point: Hash,
/// Index of the point in the [`Mentionable::points_typed()`].
/// Index of the point in the [`MentionableBase::points_typed()`].
pub index: usize,
}
/// Trait representing the "rainbow table" behaviour.
pub trait Resolver<'a, Ctx: Context<'a>>: 'a {
/// Successfully returned value should be the inverse of the point passed
/// with topology header ([`Mentionable::topology()`]) omitted.
/// with topology header ([`MentionableBase::topology()`]) omitted.
fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx>;
}

View File

@ -18,7 +18,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> {
}
}
struct ResolverOrigin<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> {
struct ResolverOrigin<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> {
r_factory: F,
r_address: Address,
r_resolver: Rc<dyn Resolver<'a, Ctx>>,
@ -47,7 +47,10 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Origin<'a, Ctx> for ResolverOrig
self.r_factory.clone()
}
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl> {
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl>
where
OFctr<'a, Ctx, Self>: Factory<'a, Ctx>,
{
_resolve_origin(self)
}

View File

@ -12,16 +12,16 @@ use std::marker::PhantomData;
use crate::rcore::*;
pub use self::modes::{AtomicProxy, RegularAtomic};
use super::*;
pub use self::modes::{AtomicProxy, RegularAtomic};
pub type AParseError<A> = <A as AtomicBase>::AParseError;
pub type AParseResult<A> = Result<A, AParseError<A>>;
/// This trait combines functionality of [`Mentionable`] and [`Factory`],
/// while limiting [`Mentionable::points_typed`] (and corresponding [`Mentionable::topology`])
/// while limiting [`MentionableBase::points_typed`] (and corresponding [`MentionableBase::topology`])
/// to an empty sequence.
pub trait AtomicBase: 'static + Send + Sync + Send + Clone + Serializable {
/// Equivalent of [`FactoryBase::ParseError`].

View File

@ -39,17 +39,7 @@ impl<const N: usize> AtomicBase for [u8; N] {
}
impl<const N: usize> ParseMode for [u8; N] {
type Mode = RegularMode;
}
impl<const N: usize> RegularAtomic for [u8; N] {
fn ra_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
Self::a_ideserialize(inlining).seal()
}
fn ra_extend(self, tail: &[u8]) -> AParseResult<Self> {
Err(Self::a_extension_error(tail))
}
type Mode = InliningMode;
}
impl<const N: usize> InlineableAtomic for [u8; N] {

View File

@ -37,14 +37,15 @@ impl<A: AtomicBase> Serializable for AtomicObject<A> {
}
pub trait AoProxy<'a, Ctx: Context<'a>>: AtomicProxy {
type Mtbl: Mentionable<'a, Ctx, Fctr = Self::Fctr>;
type Mtbl: MentionableBase<'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<A>
impl<'a, Ctx: Context<'a>, A: AtomicBase + WithParseMode> MentionableBase<'a, Ctx>
for AtomicObject<A>
where
A::WithMode: AoProxy<'a, Ctx, Mtbl = Self, A = A>,
{
@ -87,8 +88,8 @@ where
type ParseError = A::AParseError;
}
impl<A> ParseMode for AtomicFactory<A> {
type Mode = RegularMode;
impl<A: ParseMode> ParseMode for AtomicFactory<A> {
type Mode = A::Mode;
}
impl<'a, Ctx: Context<'a>, A: RegularAtomic> AoProxy<'a, Ctx> for WithMode<A, RegularMode> {

View File

@ -39,17 +39,7 @@ impl AtomicBase for u64 {
}
impl ParseMode for u64 {
type Mode = RegularMode;
}
impl RegularAtomic for u64 {
fn ra_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
Self::a_ideserialize(inlining).seal()
}
fn ra_extend(self, tail: &[u8]) -> AParseResult<Self> {
Err(Self::a_extension_error(tail))
}
type Mode = InliningMode;
}
impl InlineableAtomic for u64 {

View File

@ -46,17 +46,7 @@ impl AtomicBase for bool {
}
impl ParseMode for bool {
type Mode = RegularMode;
}
impl RegularAtomic for bool {
fn ra_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
Self::a_ideserialize(inlining).seal()
}
fn ra_extend(self, tail: &[u8]) -> AParseResult<Self> {
Err(Self::a_extension_error(tail))
}
type Mode = InliningMode;
}
impl InlineableAtomic for bool {

View File

@ -1,10 +1,5 @@
use super::*;
pub trait RegularAtomic: AtomicBase + ParseMode<Mode = RegularMode> {
fn ra_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
fn ra_extend(self, tail: &[u8]) -> AParseResult<Self>;
}
pub trait AtomicProxy {
type A: AtomicBase + ParseMode;
@ -12,18 +7,6 @@ pub trait AtomicProxy {
fn pa_extend(a: Self::A, tail: &[u8]) -> AParseResult<Self::A>;
}
impl<A: RegularAtomic> AtomicProxy for WithMode<A, RegularMode> {
type A = A;
fn pa_deserialize(inlining: impl Inlining) -> AParseResult<Self::A> {
A::ra_deserialize(inlining)
}
fn pa_extend(a: Self::A, tail: &[u8]) -> AParseResult<Self::A> {
a.ra_extend(tail)
}
}
impl<A: AtomicBase + WithParseMode> Atomic for A
where
A::WithMode: AtomicProxy<A = Self>,
@ -36,3 +19,20 @@ where
<A::WithMode as AtomicProxy>::pa_extend(self, tail)
}
}
pub trait RegularAtomic: AtomicBase + ParseMode<Mode = RegularMode> {
fn ra_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
fn ra_extend(self, tail: &[u8]) -> AParseResult<Self>;
}
impl<A: RegularAtomic> AtomicProxy for WithMode<A, RegularMode> {
type A = A;
fn pa_deserialize(inlining: impl Inlining) -> AParseResult<Self::A> {
A::ra_deserialize(inlining)
}
fn pa_extend(a: Self::A, tail: &[u8]) -> AParseResult<Self::A> {
a.ra_extend(tail)
}
}

View File

@ -24,7 +24,7 @@ pub enum CastError<'a> {
/// If you don't know what that means, it's a good idea to [`panic!`].
/// Happens due to internal resolver using indices rather than `point`s.
/// This error usually indicates inconsistent behaviour
/// of [`Mentionable::points_typed`] and/or [`Mentionable::topology`].
/// of [`MentionableBase::points_typed`] and/or [`MentionableBase::topology`].
AddressIndexOutOfBounds {
index: usize,
length: usize,

View File

@ -5,7 +5,7 @@ use crate::rcore::*;
use crate::rstd::{inlining::*, nullable::*, point::*, *};
/// Node containing a (nullable) reference to the next node and an element.
pub struct StackNode<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> {
pub struct StackNode<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx>> {
/// Reference comes first due to being inlineable.
pub rest: Stack<'a, Ctx, A>,
/// Unlike the original implementation in Python, doesn't default to using Point.
@ -28,14 +28,59 @@ impl<F> StackNodeFactory<F> {
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Serializable for StackNode<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx>> Serializable for StackNode<'a, Ctx, A> {
fn serialize(&self, serializer: &mut dyn Serializer) {
self.rest.serialize(serializer);
self.element.serialize(serializer);
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for StackNode<'a, Ctx, A> {
pub trait StackCompatible<'a, Ctx: Context<'a>>: Mentionable<'a, Ctx> {
fn points_typed_rest(stack: &Stack<'a, Ctx, Self>, points: &mut impl PointsVisitor<'a, Ctx>);
}
pub trait StackCompatibleProxy<'a, Ctx: Context<'a>, T>: FactoryProxy<'a, Ctx>
where
Self::F: Factory<'a, Ctx>,
{
fn points_typed_rest(stack: &T, points: &mut impl PointsVisitor<'a, Ctx>);
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> StackCompatible<'a, Ctx> for A
where
<Fctr<'a, Ctx, A> as WithParseMode>::WithMode:
StackCompatibleProxy<'a, Ctx, Stack<'a, Ctx, A>, F = Fctr<'a, Ctx, A>>,
{
fn points_typed_rest(stack: &Stack<'a, Ctx, Self>, points: &mut impl PointsVisitor<'a, Ctx>) {
<<Fctr<'a, Ctx, A> as WithParseMode>::WithMode as StackCompatibleProxy<'a, Ctx, _>>::points_typed_rest(stack, points)
}
}
impl<'a, Ctx: Context<'a>, F: RegularFactory<'a, Ctx>>
StackCompatibleProxy<'a, Ctx, Stack<'a, Ctx, Mtbl<'a, Ctx, F>>> for WithMode<F, RegularMode>
{
fn points_typed_rest(
stack: &Stack<'a, Ctx, Mtbl<'a, Ctx, Self::F>>,
points: &mut impl PointsVisitor<'a, Ctx>,
) {
stack.points_typed(points)
}
}
impl<'a, Ctx: Context<'a>, F: InlineableFactory<'a, Ctx>>
StackCompatibleProxy<'a, Ctx, Stack<'a, Ctx, Mtbl<'a, Ctx, F>>> for WithMode<F, InliningMode>
{
fn points_typed_rest(
stack: &Stack<'a, Ctx, Mtbl<'a, Ctx, Self::F>>,
points: &mut impl PointsVisitor<'a, Ctx>,
) {
stack.points_typed(points)
}
}
impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx>> MentionableBase<'a, Ctx>
for StackNode<'a, Ctx, A>
{
type Fctr = StackNodeFactory<A::Fctr>;
fn factory(&self) -> Self::Fctr {
@ -43,7 +88,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Sta
}
fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>) {
self.rest.points_typed(points);
<A as StackCompatible<'a, Ctx>>::points_typed_rest(&self.rest, points);
self.element.points_typed(points);
}
}
@ -81,23 +126,28 @@ impl<F> StackNodeFactory<F> {
inctx: I,
) -> IParseResult<'a, Ctx, NullableFactory<Self>, I>
where
F: FactoryBase<'a, Ctx>,
StackNodeFactory<F>: Factory<'a, Ctx>,
{
NullableFactory::new(self.clone()).ideserialize(inctx)
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for StackNodeFactory<F> {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for StackNodeFactory<F>
where
F::Mtbl: StackCompatible<'a, Ctx, _Fctr = F>,
{
type Mtbl = StackNode<'a, Ctx, F::Mtbl>;
type ParseError = StackParseError<ParseError<'a, Ctx, F>>;
}
impl<F> ParseMode for StackNodeFactory<F> {
type Mode = RegularMode;
impl<F: ParseMode> ParseMode for StackNodeFactory<F> {
type Mode = F::Mode;
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> RegularFactory<'a, Ctx> for StackNodeFactory<F> {
impl<'a, Ctx: Context<'a>, F: RegularFactory<'a, Ctx>> RegularFactory<'a, Ctx>
for StackNodeFactory<F>
{
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let (rest, inctx) = self.parse_point(inctx)?;
let element = self
@ -126,7 +176,9 @@ 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>>: Mentionable<'a, Ctx> {
pub trait ExtStack<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>:
MentionableBase<'a, Ctx>
{
/// Get an empty stack ([`Nullable::Null`]).
fn empty(factory: A::Fctr) -> Self;
/// Get the corresponding factory.
@ -138,14 +190,17 @@ pub trait ExtStack<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>: Mentionable<'
}
/// Extention trait with helper methods for [Stack]s.
pub trait ExtStackClone<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone>:
Mentionable<'a, Ctx>
pub trait ExtStackClone<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx> + Clone>:
MentionableBase<'a, Ctx>
{
/// Collect all the elements into a [`Vec`].
fn vec(self) -> StackVecWrapped<'a, Ctx, A>;
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> ExtStack<'a, Ctx, A> for Stack<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx>> ExtStack<'a, Ctx, A> for Stack<'a, Ctx, A>
where
Fctr<'a, Ctx, StackNode<'a, Ctx, A>>: Factory<'a, Ctx>,
{
fn empty(factory: A::Fctr) -> Self {
Nullable::Null(StackNodeFactory::new(factory.clone()))
}
@ -163,8 +218,10 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> ExtStack<'a, Ctx, A> for Sta
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone> ExtStackClone<'a, Ctx, A>
impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx> + Clone> ExtStackClone<'a, Ctx, A>
for Stack<'a, Ctx, A>
where
Fctr<'a, Ctx, StackNode<'a, Ctx, A>>: Factory<'a, Ctx>,
{
fn vec(self) -> StackVecWrapped<'a, Ctx, A> {
Ctx::T::iterate_mut((vec![], self), |(mut vec, stack)| match stack {

View File

@ -5,12 +5,7 @@ use std::{error::Error, fmt::Display};
use crate::{
flow::binary::*,
rcore::*,
rstd::{
atomic::{au64::*, *},
inlining::*,
nullable::*,
point::*,
},
rstd::{atomic::au64::*, inlining::*, nullable::*, point::*},
};
#[derive(Debug)]
@ -105,7 +100,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Serializable for Tree<'a, Ct
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Node<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx> for Node<'a, Ctx, A> {
type Fctr = NodeFactory<A::Fctr>;
fn factory(&self) -> Self::Fctr {
@ -119,7 +114,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Nod
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Tree<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx> for Tree<'a, Ctx, A> {
type Fctr = TreeFactory<A::Fctr>;
fn factory(&self) -> Self::Fctr {
@ -131,7 +126,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Tre
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for NodeFactory<F> {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for NodeFactory<F> {
type Mtbl = Node<'a, Ctx, F::Mtbl>;
type ParseError = TreeParseError<F::ParseError>;
@ -159,29 +154,17 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> RegularFactory<'a, Ctx> for Node
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for TreeFactory<F> {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for TreeFactory<F> {
type Mtbl = Tree<'a, Ctx, F::Mtbl>;
type ParseError = TreeParseError<F::ParseError>;
}
impl<F> ParseMode for TreeFactory<F> {
type Mode = RegularMode;
type Mode = InliningMode;
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for TreeFactory<F> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.ideserialize(inctx).seal()
}
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)
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> InlineableFactory<'a, Ctx> for TreeFactory<F> {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> InlineableFactory<'a, Ctx> for TreeFactory<F> {
fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
u64::a_extension_error(tail).into()
}

View File

@ -1,5 +1,6 @@
//! Traits to better express parsing semantics.
mod modes;
pub mod static_pair;
use crate::rcore::*;
@ -9,10 +10,14 @@ use super::{
*,
};
pub use self::modes::InliningMode;
pub type IParseResult<'a, Ctx, F, I> = Result<(Mtbl<'a, Ctx, F>, I), ParseError<'a, Ctx, F>>;
/// This factory should return an error on EOF.
pub trait InlineableFactory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> {
pub trait InlineableFactory<'a, Ctx: Context<'a>>:
FactoryBase<'a, Ctx> + ParseMode<Mode = InliningMode>
{
fn extension_error(&self, tail: &[u8]) -> Self::ParseError;
fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I>;
@ -48,7 +53,7 @@ pub trait ConstSizeObject<'a, Ctx: Context<'a>>: FixedSizeObject<'a, Ctx> {
pub type ADParseResult<A, D> = Result<(A, D), AParseError<A>>;
/// Atomic analogue of [`InlineableFactory`]/[`InlineableObject`].
pub trait InlineableAtomic: AtomicBase + ParseMode {
pub trait InlineableAtomic: AtomicBase + ParseMode<Mode = InliningMode> {
fn a_extension_error(tail: &[u8]) -> Self::AParseError;
fn a_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D>;

View File

@ -0,0 +1,33 @@
use super::*;
pub struct InliningMode;
impl<'a, Ctx: Context<'a>, F: InlineableFactory<'a, Ctx>> FactoryProxy<'a, Ctx>
for WithMode<F, InliningMode>
{
type F = F;
fn pdeserialize(f: &Self::F, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self::F> {
f.ideserialize(inctx).seal()
}
fn pextend(
f: &Self::F,
_mentionable: Mtbl<'a, Ctx, Self::F>,
tail: &[u8],
) -> ParseResult<'a, Ctx, Self::F> {
Err(f.extension_error(tail))
}
}
impl<A: InlineableAtomic> AtomicProxy for WithMode<A, InliningMode> {
type A = A;
fn pa_deserialize(inlining: impl Inlining) -> AParseResult<Self::A> {
A::a_ideserialize(inlining).seal()
}
fn pa_extend(_a: Self::A, tail: &[u8]) -> AParseResult<Self::A> {
Err(A::a_extension_error(tail))
}
}

View File

@ -34,9 +34,9 @@ pub trait StaticPair<'a, Ctx: Context<'a>>:
/// [Factory] data. May, depending on the usecase, include factory (factories) on the element(s).
type FactoryData: 'a + Send + Sync + Clone;
/// First element's type. Must equal [`StaticPairSerializable::SA`].
type A: Mentionable<'a, Ctx, Fctr = Self::FA>;
type A: MentionableBase<'a, Ctx, Fctr = Self::FA>;
/// Second element's type. Must equal [`StaticPairSerializable::SB`].
type B: Mentionable<'a, Ctx, Fctr = Self::FB>;
type B: MentionableBase<'a, Ctx, Fctr = Self::FB>;
/// First element's factory.
type FA: Factory<'a, Ctx, Mtbl = Self::A> + InlineableFactory<'a, Ctx>;
/// Second element's factory.
@ -100,7 +100,9 @@ impl<SP: StaticPairSerializable> Serializable for StaticPairObject<SP> {
}
}
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Mentionable<'a, Ctx> for StaticPairObject<SP> {
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> MentionableBase<'a, Ctx>
for StaticPairObject<SP>
{
type Fctr = StaticPairFactory<'a, Ctx, SP>;
fn factory(&self) -> Self::Fctr {
@ -133,11 +135,13 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FactoryBase<'a, Ctx>
}
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> ParseMode for StaticPairFactory<'a, Ctx, SP> {
type Mode = RegularMode;
type Mode = <SP::FB as ParseMode>::Mode;
}
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> RegularFactory<'a, Ctx>
for StaticPairFactory<'a, Ctx, SP>
where
SP::FB: RegularFactory<'a, Ctx>,
{
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let (fa, fb) = SP::factories(&self.factory_data);

View File

@ -6,7 +6,7 @@ use crate::rcore::*;
use super::{inlining::*, point::*, *};
/// Nullable reference type. Made for use as a linking element in data structures.
pub enum Nullable<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> {
pub enum Nullable<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> {
/// Unlike original Python implementation, stores the factory only in the null case.
Null(A::Fctr),
NotNull(Point<'a, Ctx, A>),
@ -19,7 +19,7 @@ pub struct NullableFactory<F> {
factory: F,
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Serializable for Nullable<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> Serializable for Nullable<'a, Ctx, A> {
fn serialize(&self, serializer: &mut dyn Serializer) {
serializer.write(match self {
Self::Null(_) => &HASH_ZEROS,
@ -28,7 +28,9 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Serializable for Nullable<'a
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Nullable<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx>
for Nullable<'a, Ctx, A>
{
type Fctr = NullableFactory<A::Fctr>;
fn factory(&self) -> Self::Fctr {
@ -50,24 +52,14 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Nul
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for NullableFactory<F> {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for NullableFactory<F> {
type Mtbl = Nullable<'a, Ctx, F::Mtbl>;
type ParseError = PointParseError;
}
impl<F> ParseMode for NullableFactory<F> {
type Mode = RegularMode;
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for NullableFactory<F> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.ideserialize(inctx).seal()
}
fn rextend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
Err(self.extension_error(tail))
}
type Mode = InliningMode;
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Nullable<'a, Ctx, Nullable<'a, Ctx, A>> {
@ -102,7 +94,7 @@ impl<F> NullableFactory<F> {
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Clone for Nullable<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> Clone for Nullable<'a, Ctx, A> {
fn clone(&self) -> Self {
match self {
Self::Null(factory) => Self::Null(factory.clone()),
@ -111,9 +103,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Clone for Nullable<'a, Ctx,
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> InlineableFactory<'a, Ctx>
for NullableFactory<F>
{
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> InlineableFactory<'a, Ctx> for NullableFactory<F> {
fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
PointParseError::WrongLength(HASH_SIZE + tail.len())
}

View File

@ -11,7 +11,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Serializable for Point<'a, C
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Point<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx> for Point<'a, Ctx, A> {
type Fctr = PointFactory<A::Fctr>;
fn factory(&self) -> Self::Fctr {
@ -69,27 +69,17 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AsRef<[u8]> for Point<'a, Ct
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for PointFactory<F> {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for PointFactory<F> {
type Mtbl = Point<'a, Ctx, F::Mtbl>;
type ParseError = PointParseError;
}
impl<F> ParseMode for PointFactory<F> {
type Mode = RegularMode;
type Mode = InliningMode;
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for PointFactory<F> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.ideserialize(inctx).seal()
}
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<F> {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> InlineableFactory<'a, Ctx> for PointFactory<F> {
fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
PointParseError::WrongLength(HASH_SIZE + tail.len())
}

View File

@ -48,7 +48,7 @@ impl<'a, Ctx: Context<'a>> Serializable for TypelessMentionable<'a, Ctx> {
}
}
impl<'a, Ctx: Context<'a>> Mentionable<'a, Ctx> for TypelessMentionable<'a, Ctx> {
impl<'a, Ctx: Context<'a>> MentionableBase<'a, Ctx> for TypelessMentionable<'a, Ctx> {
type Fctr = TypelessFactory<'a, Ctx>;
fn factory(&self) -> Self::Fctr {
@ -207,7 +207,7 @@ where
pub trait MentionableExt<'a, Ctx: Context<'a>>: Mentionable<'a, Ctx> {
/// References ([Point]s) to other objects. Typeless.
fn points_typeless(&self, points: &mut Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>);
/// [Vec] of [Point]s as used by [`Mentionable::topology`].
/// [Vec] of [Point]s as used by [`MentionableBase::topology`].
fn points_vec(&self) -> Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>;
}