rcore::stream
This commit is contained in:
parent
3ba406856e
commit
2d8f457b21
@ -34,9 +34,9 @@ pub type ModeResult<M, A, E, I> = Result<ParseSuccess<M, A, I>, E>;
|
||||
/// [`()`]: unit
|
||||
pub trait Mode {
|
||||
/// Successful parsing, may countain the parser itself
|
||||
/// (`I`, usually [`Inlining`]).
|
||||
/// (`I`, usually [`Stream`]).
|
||||
///
|
||||
/// [`Inlining`]: crate::rcore::Inlining
|
||||
/// [`Stream`]: crate::rcore::Stream
|
||||
type ParseSuccess<A, I>;
|
||||
|
||||
/// Result of extending the value, failing sometimes or always.
|
||||
|
@ -9,7 +9,6 @@ mod demoted;
|
||||
mod diagnostic;
|
||||
mod hashing;
|
||||
mod inctx;
|
||||
mod inlining;
|
||||
mod modes;
|
||||
mod origin;
|
||||
mod point;
|
||||
@ -17,6 +16,7 @@ mod points;
|
||||
mod resolution;
|
||||
mod resolver_origin;
|
||||
mod slice_deserializer;
|
||||
mod stream;
|
||||
|
||||
use std::{error::Error, rc::Rc};
|
||||
|
||||
@ -31,7 +31,6 @@ pub use self::demoted::Demoted;
|
||||
pub use self::diagnostic::Diagnostic;
|
||||
pub use self::hashing::{Hash, HASH_SIZE, HASH_ZEROS};
|
||||
pub use self::inctx::InCtx;
|
||||
pub use self::inlining::{Inlining, InliningExt, InliningResultExt};
|
||||
pub use self::modes::{
|
||||
CRegularFactory, ExtensionResultM, ExtensionSourceM, FactoryModeParse, FactoryModeProxy,
|
||||
ModeResultM, RegularFactory,
|
||||
@ -44,6 +43,7 @@ pub use self::resolution::{
|
||||
ResolutionFailure, ResolutionResult, Resolver, ResolverMap,
|
||||
};
|
||||
pub use self::slice_deserializer::SliceDeserializer;
|
||||
pub use self::stream::{Stream, StreamExt, StreamResultExt};
|
||||
|
||||
/// Helper alias for [`WeakFunctor::F`] of [`FunctorContext::T`].
|
||||
pub type Wrapped<'a, Ctx, A> = WrapC<'a, A, Ctx>;
|
||||
|
@ -26,7 +26,7 @@ impl Addresses {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) trait InliningAddresses<E>: Inlining {
|
||||
pub(super) trait InliningAddresses<E>: Stream {
|
||||
fn inext_address(
|
||||
self,
|
||||
addresses: &mut Addresses,
|
||||
@ -53,7 +53,7 @@ pub(super) trait InliningAddresses<E>: Inlining {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, D: ?Sized + Inlining> InliningAddresses<E> for D {}
|
||||
impl<E, D: ?Sized + Stream> InliningAddresses<E> for D {}
|
||||
|
||||
fn _parse_slice<'a, Ctx: Context<'a>, F: FactoryParse<'a, Ctx>>(
|
||||
factory: &F,
|
||||
|
@ -3,7 +3,7 @@ use super::*;
|
||||
/// Demoted [`InCtx`], returned by [`InCtx::demote`]. Use when a concrete type required.
|
||||
pub struct Demoted<'a: 'c, 'c, Ctx: Context<'a>>(pub(super) &'c mut dyn DeCtx<'a, Ctx>);
|
||||
|
||||
impl<'a: 'c, 'c, Ctx: Context<'a>> Inlining for Demoted<'a, 'c, Ctx> {
|
||||
impl<'a: 'c, 'c, Ctx: Context<'a>> Stream for Demoted<'a, 'c, Ctx> {
|
||||
fn iread_n<A, E>(
|
||||
self,
|
||||
n: usize,
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
/// [Inlining] context.
|
||||
pub trait InCtx<'a, Ctx: Context<'a>>: Inlining {
|
||||
/// Inlining ([Stream]ed) context.
|
||||
pub trait InCtx<'a, Ctx: Context<'a>>: Stream {
|
||||
/// Read the next [Address].
|
||||
fn icnext_address<E>(self, err: impl FnOnce(&[u8]) -> E) -> Result<(Address, Self), E>;
|
||||
|
||||
@ -28,7 +28,7 @@ pub trait InCtx<'a, Ctx: Context<'a>>: Inlining {
|
||||
Self: 'd;
|
||||
}
|
||||
|
||||
impl<'a: 'c, 'c, Ctx: Context<'a>> Inlining for &'c mut dyn DeCtx<'a, Ctx> {
|
||||
impl<'a: 'c, 'c, Ctx: Context<'a>> Stream for &'c mut dyn DeCtx<'a, Ctx> {
|
||||
fn iread_n<A, E>(
|
||||
self,
|
||||
n: usize,
|
||||
|
@ -2,7 +2,7 @@ use super::*;
|
||||
|
||||
/// [`Deserializer`] with a cleaner interface for parsing multiple objects
|
||||
/// from one stream ("inlining").
|
||||
pub trait Inlining: Sized {
|
||||
pub trait Stream: Sized {
|
||||
/// Try to read `n` bytes. Consumes the deserializer on failure.
|
||||
fn iread_n<A, E>(
|
||||
self,
|
||||
@ -18,9 +18,9 @@ pub trait Inlining: Sized {
|
||||
fn itell(&self) -> usize;
|
||||
}
|
||||
|
||||
/// [`InliningExt::iread_n_const`].
|
||||
pub trait InliningExt<E>: Inlining {
|
||||
/// `const`-length equivalent of [`Inlining::iread_n`].
|
||||
/// [`StreamExt::iread_n_const`].
|
||||
pub trait StreamExt<E>: Stream {
|
||||
/// `const`-length equivalent of [`Stream::iread_n`].
|
||||
fn iread_n_const<const N: usize>(
|
||||
self,
|
||||
err: impl FnOnce(&[u8]) -> E,
|
||||
@ -33,9 +33,9 @@ pub trait InliningExt<E>: Inlining {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Inlining, E> InliningExt<E> for D {}
|
||||
impl<I: Stream, E> StreamExt<E> for I {}
|
||||
|
||||
impl<D: ?Sized + Deserializer> Inlining for &mut D {
|
||||
impl<D: ?Sized + Deserializer> Stream for &mut D {
|
||||
fn iread_n<A, E>(
|
||||
self,
|
||||
n: usize,
|
||||
@ -59,13 +59,13 @@ impl<D: ?Sized + Deserializer> Inlining for &mut D {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`InliningResultExt::seal`].
|
||||
pub trait InliningResultExt<A, E> {
|
||||
/// [`StreamResultExt::seal`].
|
||||
pub trait StreamResultExt<A, E> {
|
||||
/// Drop the deserializer from parsing result.
|
||||
fn seal(self) -> Result<A, E>;
|
||||
}
|
||||
|
||||
impl<A, E, I: Inlining> InliningResultExt<A, E> for Result<(A, I), E> {
|
||||
impl<A, E, I: Stream> StreamResultExt<A, E> for Result<(A, I), E> {
|
||||
fn seal(self) -> Result<A, E> {
|
||||
self.map(|(a, _)| a)
|
||||
}
|
@ -35,7 +35,7 @@ pub trait AtomicBase: 'static + Send + Sync + Send + Clone + Serializable {
|
||||
/// to an empty sequence.
|
||||
pub trait Atomic: AtomicBase + ParseMode {
|
||||
/// Static equivalent of [`FactoryParse::deserialize`].
|
||||
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
|
||||
fn a_deserialize(stream: impl Stream) -> AParseResult<Self>;
|
||||
/// Static equivalent of [`FactoryParse::extend`].
|
||||
fn a_extend(self, tail: &[u8]) -> AParseResult<Self>;
|
||||
}
|
||||
|
@ -47,8 +47,8 @@ impl<const N: usize> CInliningAtomic for [u8; N] {
|
||||
ArrayParseError::ExtraData(tail.len())
|
||||
}
|
||||
|
||||
fn ca_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D> {
|
||||
inlining.iread_n_const::<N>(|slice| ArrayParseError::from(slice))
|
||||
fn ca_ideserialize<I: Stream>(stream: I) -> AIParseResult<Self, I> {
|
||||
stream.iread_n_const::<N>(|slice| ArrayParseError::from(slice))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,9 +47,9 @@ impl CInliningAtomic for u64 {
|
||||
IntParseError::ExtraData(tail.len())
|
||||
}
|
||||
|
||||
fn ca_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D> {
|
||||
let (x, inlining) = inlining.iread_n_const::<8>(|slice| IntParseError::from(slice))?;
|
||||
Ok((u64::from_le_bytes(x), inlining))
|
||||
fn ca_ideserialize<I: Stream>(stream: I) -> AIParseResult<Self, I> {
|
||||
let (x, stream) = stream.iread_n_const::<8>(|slice| IntParseError::from(slice))?;
|
||||
Ok((u64::from_le_bytes(x), stream))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,12 +54,11 @@ impl CInliningAtomic for bool {
|
||||
BooleanParseError::ExtraData(tail.len())
|
||||
}
|
||||
|
||||
fn ca_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D> {
|
||||
let (byte, inlining) =
|
||||
inlining.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?;
|
||||
fn ca_ideserialize<I: Stream>(stream: I) -> AIParseResult<Self, I> {
|
||||
let (byte, stream) = stream.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?;
|
||||
match byte[0] {
|
||||
0 => Ok((false, inlining)),
|
||||
1 => Ok((true, inlining)),
|
||||
0 => Ok((false, stream)),
|
||||
1 => Ok((true, stream)),
|
||||
value => Err(BooleanParseError::OutOfBounds(value)),
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
use super::*;
|
||||
|
||||
impl<A: AtomicModeParse> Atomic for A {
|
||||
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
|
||||
Self::ma_deserialize(inlining).map(Self::seal)
|
||||
fn a_deserialize(stream: impl Stream) -> AParseResult<Self> {
|
||||
Self::ma_deserialize(stream).map(Self::seal)
|
||||
}
|
||||
|
||||
fn a_extend(self, tail: &[u8]) -> AParseResult<Self> {
|
||||
@ -22,7 +22,7 @@ pub type AExtensionSourceM<A> = ExtensionSourceP<A, A>;
|
||||
/// A more generic version of [`Atomic`].
|
||||
pub trait AtomicModeParse: AtomicBase + ParseMode {
|
||||
/// A more generic version of [`Atomic::a_deserialize`].
|
||||
fn ma_deserialize<I: Inlining>(inlining: I) -> AModeResultM<Self, I>;
|
||||
fn ma_deserialize<I: Stream>(stream: I) -> AModeResultM<Self, I>;
|
||||
|
||||
/// A more generic version of [`Atomic::a_extend`].
|
||||
fn ma_extend(atomic: AExtensionSourceM<Self>, tail: &[u8]) -> AExtensionResultM<Self>;
|
||||
@ -34,7 +34,7 @@ pub trait AtomicModeProxy {
|
||||
type A: AtomicBase + ParseMode;
|
||||
|
||||
/// External implementation of [`AtomicModeParse::ma_deserialize`].
|
||||
fn pma_deserialize<I: Inlining>(inlining: I) -> AModeResultM<Self::A, I>;
|
||||
fn pma_deserialize<I: Stream>(stream: I) -> AModeResultM<Self::A, I>;
|
||||
|
||||
/// External implementation of [`AtomicModeParse::ma_extend`].
|
||||
fn pma_extend(atomic: AExtensionSourceM<Self::A>, tail: &[u8]) -> AExtensionResultM<Self::A>;
|
||||
@ -44,8 +44,8 @@ impl<A: AtomicBase + WithParseMode> AtomicModeParse for A
|
||||
where
|
||||
<A as WithParseMode>::WithMode: AtomicModeProxy<A = A>,
|
||||
{
|
||||
fn ma_deserialize<I: Inlining>(inlining: I) -> AModeResultM<Self, I> {
|
||||
<<A as WithParseMode>::WithMode as AtomicModeProxy>::pma_deserialize(inlining)
|
||||
fn ma_deserialize<I: Stream>(stream: I) -> AModeResultM<Self, I> {
|
||||
<<A as WithParseMode>::WithMode as AtomicModeProxy>::pma_deserialize(stream)
|
||||
}
|
||||
|
||||
fn ma_extend(atomic: AExtensionSourceM<Self>, tail: &[u8]) -> AExtensionResultM<Self> {
|
||||
@ -56,7 +56,7 @@ where
|
||||
/// For auto-deriving [`RegularAtomic`] from concrete implementations.
|
||||
pub trait CRegularAtomic: AtomicBase + ImplMode<Mode = RegularMode> {
|
||||
/// Concrete implementation of [`RegularAtomic::ra_deserialize`].
|
||||
fn cra_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
|
||||
fn cra_deserialize(stream: impl Stream) -> AParseResult<Self>;
|
||||
/// Concrete implementation of [`RegularAtomic::ra_extend`].
|
||||
fn cra_extend(self, tail: &[u8]) -> AParseResult<Self>;
|
||||
}
|
||||
@ -64,14 +64,14 @@ pub trait CRegularAtomic: AtomicBase + ImplMode<Mode = RegularMode> {
|
||||
/// Mostly same as [`AtomicModeParse`] but requires [`Mode`] to be [`RegularMode`].
|
||||
pub trait RegularAtomic: AtomicBase + ParseMode<Mode = RegularMode> {
|
||||
/// Same as [`AtomicModeParse::ma_deserialize`].
|
||||
fn ra_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
|
||||
fn ra_deserialize(stream: impl Stream) -> AParseResult<Self>;
|
||||
/// Same as [`AtomicModeParse::ma_extend`].
|
||||
fn ra_extend(self, tail: &[u8]) -> AParseResult<Self>;
|
||||
}
|
||||
|
||||
impl<A: AtomicModeParse + ParseMode<Mode = RegularMode>> RegularAtomic for A {
|
||||
fn ra_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
|
||||
A::ma_deserialize(inlining)
|
||||
fn ra_deserialize(stream: impl Stream) -> AParseResult<Self> {
|
||||
A::ma_deserialize(stream)
|
||||
}
|
||||
|
||||
fn ra_extend(self, tail: &[u8]) -> AParseResult<Self> {
|
||||
@ -82,8 +82,8 @@ impl<A: AtomicModeParse + ParseMode<Mode = RegularMode>> RegularAtomic for A {
|
||||
impl<A: CRegularAtomic> AtomicModeProxy for WithMode<A, RegularMode> {
|
||||
type A = A;
|
||||
|
||||
fn pma_deserialize<I: Inlining>(inlining: I) -> AModeResultM<Self::A, I> {
|
||||
A::cra_deserialize(inlining)
|
||||
fn pma_deserialize<I: Stream>(stream: I) -> AModeResultM<Self::A, I> {
|
||||
A::cra_deserialize(stream)
|
||||
}
|
||||
|
||||
fn pma_extend(atomic: AExtensionSourceM<Self::A>, tail: &[u8]) -> AExtensionResultM<Self::A> {
|
||||
|
@ -27,8 +27,8 @@ impl ImplMode for Plain {
|
||||
}
|
||||
|
||||
impl CRegularAtomic for Plain {
|
||||
fn cra_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
|
||||
Ok(inlining.iread_all(Plain::from_slice))
|
||||
fn cra_deserialize(stream: impl Stream) -> AParseResult<Self> {
|
||||
Ok(stream.iread_all(Plain::from_slice))
|
||||
}
|
||||
|
||||
fn cra_extend(mut self, tail: &[u8]) -> AParseResult<Self> {
|
||||
|
@ -71,19 +71,19 @@ pub trait ConstSizeObject<'a, Ctx: Context<'a>>: FixedSizeObject<'a, Ctx> {
|
||||
const SIZE: usize;
|
||||
}
|
||||
|
||||
pub type ADParseResult<A, D> = Result<(A, D), AParseError<A>>;
|
||||
pub type AIParseResult<A, I> = Result<(A, I), AParseError<A>>;
|
||||
|
||||
pub trait CInliningAtomic: AtomicBase + ImplMode<Mode = InliningMode> {
|
||||
fn ca_extension_error(tail: &[u8]) -> Self::AParseError;
|
||||
|
||||
fn ca_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D>;
|
||||
fn ca_ideserialize<I: Stream>(stream: I) -> AIParseResult<Self, I>;
|
||||
}
|
||||
|
||||
/// Atomic analogue of [`InliningFactory`]/[`InliningObject`].
|
||||
pub trait InliningAtomic: AtomicBase + ParseMode<Mode = InliningMode> {
|
||||
fn a_extension_error(tail: &[u8]) -> Self::AParseError;
|
||||
|
||||
fn a_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D>;
|
||||
fn a_ideserialize<I: Stream>(strean: I) -> AIParseResult<Self, I>;
|
||||
}
|
||||
|
||||
impl<A: AtomicModeParse + ParseMode<Mode = InliningMode>> InliningAtomic for A {
|
||||
@ -91,8 +91,8 @@ impl<A: AtomicModeParse + ParseMode<Mode = InliningMode>> InliningAtomic for A {
|
||||
A::ma_extend((), tail)
|
||||
}
|
||||
|
||||
fn a_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D> {
|
||||
A::ma_deserialize(inlining)
|
||||
fn a_ideserialize<I: Stream>(stream: I) -> AIParseResult<Self, I> {
|
||||
A::ma_deserialize(stream)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,8 +93,8 @@ impl<'a, Ctx: Context<'a>, F: CInliningFactory<'a, Ctx>> FactoryModeProxy<'a, Ct
|
||||
impl<A: CInliningAtomic> AtomicModeProxy for WithMode<A, InliningMode> {
|
||||
type A = A;
|
||||
|
||||
fn pma_deserialize<I: Inlining>(inlining: I) -> AModeResultM<Self::A, I> {
|
||||
A::ca_ideserialize(inlining)
|
||||
fn pma_deserialize<I: Stream>(stream: I) -> AModeResultM<Self::A, I> {
|
||||
A::ca_ideserialize(stream)
|
||||
}
|
||||
|
||||
fn pma_extend(_atomic: AExtensionSourceM<Self::A>, tail: &[u8]) -> AExtensionResultM<Self::A> {
|
||||
|
Loading…
Reference in New Issue
Block a user