InCtx
in Factory::deserialize
This commit is contained in:
parent
a72d2ed441
commit
d6adc543a7
@ -78,7 +78,7 @@ pub trait Factory<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
|
||||
type ParseError: 'a + Error;
|
||||
|
||||
/// See [`Deserializer`], [`Resolver`].
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>;
|
||||
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>;
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ fn _parse_slice<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>(
|
||||
deserializer: &mut deserializer,
|
||||
resolver,
|
||||
addresses: &mut Addresses::start(),
|
||||
})?;
|
||||
} as &mut dyn DeCtx<'a, Ctx>)?;
|
||||
let tail = deserializer.read_all();
|
||||
if tail.is_empty() {
|
||||
Ok(mentionable)
|
||||
|
@ -21,7 +21,7 @@ pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable {
|
||||
/// Equivalent of [`Factory::ParseError`].
|
||||
type AParseError: Error;
|
||||
/// Static equivalent of [`Factory::deserialize`].
|
||||
fn a_deserialize(deserializer: impl Inlining) -> AParseResult<Self>;
|
||||
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
|
||||
/// Static equivalent of [`Factory::extend`].
|
||||
fn a_extend(self, tail: &[u8]) -> AParseResult<Self>;
|
||||
}
|
||||
|
@ -72,8 +72,8 @@ impl<'a, Ctx: Context<'a>, A: Atomic> Factory<'a, Ctx> for AtomicFactory<A> {
|
||||
|
||||
type ParseError = A::AParseError;
|
||||
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
Ok(A::a_deserialize(dectx)?.into())
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
Ok(A::a_deserialize(inctx)?.into())
|
||||
}
|
||||
|
||||
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
|
||||
|
@ -37,8 +37,8 @@ impl From<&[u8]> for IntParseError {
|
||||
impl Atomic for u64 {
|
||||
type AParseError = IntParseError;
|
||||
|
||||
fn a_deserialize(deserializer: impl Inlining) -> AParseResult<Self> {
|
||||
let (n, _) = deserializer.iread_n_const::<8>(|slice| IntParseError::from(slice))?;
|
||||
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
|
||||
let (n, _) = inlining.iread_n_const::<8>(|slice| IntParseError::from(slice))?;
|
||||
Ok(u64::from_le_bytes(n))
|
||||
}
|
||||
|
||||
@ -52,10 +52,9 @@ impl InlineableAtomic for u64 {
|
||||
IntParseError::ExtraData(tail.len())
|
||||
}
|
||||
|
||||
fn a_ideserialize<D: Inlining>(deserializer: D) -> ADParseResult<Self, D> {
|
||||
let (x, deserializer) =
|
||||
deserializer.iread_n_const::<8>(|slice| IntParseError::from(slice))?;
|
||||
Ok((u64::from_le_bytes(x), deserializer))
|
||||
fn a_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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,8 +44,8 @@ impl From<&[u8]> for BooleanParseError {
|
||||
impl Atomic for bool {
|
||||
type AParseError = BooleanParseError;
|
||||
|
||||
fn a_deserialize(deserializer: impl Inlining) -> AParseResult<Self> {
|
||||
let (byte, _) = deserializer.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?;
|
||||
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
|
||||
let (byte, _) = inlining.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?;
|
||||
match byte[0] {
|
||||
0 => Ok(false),
|
||||
1 => Ok(true),
|
||||
@ -63,12 +63,12 @@ impl InlineableAtomic for bool {
|
||||
BooleanParseError::ExtraData(tail.len())
|
||||
}
|
||||
|
||||
fn a_ideserialize<D: Inlining>(deserializer: D) -> ADParseResult<Self, D> {
|
||||
let (byte, deserializer) =
|
||||
deserializer.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?;
|
||||
fn a_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D> {
|
||||
let (byte, inlining) =
|
||||
inlining.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?;
|
||||
match byte[0] {
|
||||
0 => Ok((false, deserializer)),
|
||||
1 => Ok((true, deserializer)),
|
||||
0 => Ok((false, inlining)),
|
||||
1 => Ok((true, inlining)),
|
||||
value => Err(BooleanParseError::OutOfBounds(value)),
|
||||
}
|
||||
}
|
||||
|
@ -31,8 +31,8 @@ impl Serializable for Plain {
|
||||
impl Atomic for Plain {
|
||||
type AParseError = PlainParseError;
|
||||
|
||||
fn a_deserialize(deserializer: impl Inlining) -> AParseResult<Self> {
|
||||
Ok(deserializer.iread_all(Plain::from_slice))
|
||||
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
|
||||
Ok(inlining.iread_all(Plain::from_slice))
|
||||
}
|
||||
|
||||
fn a_extend(mut self, tail: &[u8]) -> AParseResult<Self> {
|
||||
|
@ -74,13 +74,13 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFa
|
||||
|
||||
type ParseError = StackParseError<ParseError<'a, Ctx, F>>;
|
||||
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let rest = NullableFactory::new(self.clone())
|
||||
.deserialize(dectx)
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let (rest, inctx) = NullableFactory::new(self.clone())
|
||||
.ideserialize(inctx)
|
||||
.map_err(StackParseError::Point)?;
|
||||
let element = self
|
||||
.element_factory
|
||||
.deserialize(dectx)
|
||||
.deserialize(inctx)
|
||||
.map_err(StackParseError::Element)?;
|
||||
Ok(StackNode { rest, element })
|
||||
}
|
||||
|
@ -136,11 +136,11 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NodeFactory
|
||||
|
||||
type ParseError = TreeParseError<F::ParseError>;
|
||||
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let tree_factory = TreeFactory(NullableFactory::new(self.clone()));
|
||||
let l = tree_factory.deserialize(dectx)?;
|
||||
let r = tree_factory.deserialize(dectx)?;
|
||||
let key = self.0.deserialize(dectx).map_err(TreeParseError::Key)?;
|
||||
let (l, inctx) = tree_factory.ideserialize(inctx)?;
|
||||
let (r, inctx) = tree_factory.ideserialize(inctx)?;
|
||||
let key = self.0.deserialize(inctx).map_err(TreeParseError::Key)?;
|
||||
Ok(Node { l, r, key })
|
||||
}
|
||||
|
||||
@ -158,9 +158,9 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for TreeFactory
|
||||
|
||||
type ParseError = TreeParseError<F::ParseError>;
|
||||
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let node = self.0.deserialize(dectx)?;
|
||||
let height = u64::a_deserialize(dectx)?;
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let (node, inctx) = self.0.ideserialize(inctx)?;
|
||||
let height = u64::a_deserialize(inctx)?;
|
||||
let tree = Tree { node, height };
|
||||
tree.validate_height()?;
|
||||
Ok(tree)
|
||||
|
@ -121,7 +121,7 @@ pub type ADParseResult<A, D> = Result<(A, D), <A as Atomic>::AParseError>;
|
||||
pub trait InlineableAtomic: Atomic {
|
||||
fn a_extension_error(tail: &[u8]) -> Self::AParseError;
|
||||
|
||||
fn a_ideserialize<D: Inlining>(deserializer: D) -> ADParseResult<Self, D>;
|
||||
fn a_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D>;
|
||||
}
|
||||
|
||||
/// Atomic analogue of [`ConstSizeFactory`]/[`ConstSizeObject`].
|
||||
@ -225,14 +225,11 @@ pub type CheckedParseResult<'a, Ctx, F> =
|
||||
/// Extension trait for factories that ensures fixed size read.
|
||||
pub trait CheckedParse<'a, Ctx: Context<'a>>: FixedSizeFactory<'a, Ctx> {
|
||||
/// Verify proper read length using [`Deserializer::tell`].
|
||||
fn deserialize_checked(
|
||||
&self,
|
||||
dectx: &mut dyn DeCtx<'a, Ctx>,
|
||||
) -> CheckedParseResult<'a, Ctx, Self> {
|
||||
fn deserialize_checked(&self, inctx: impl InCtx<'a, Ctx>) -> CheckedParseResult<'a, Ctx, Self> {
|
||||
let expected_size = self.size();
|
||||
let start = dectx.deserializer().tell();
|
||||
let result = self.deserialize(dectx)?;
|
||||
let end = dectx.deserializer().tell();
|
||||
let start = inctx.itell();
|
||||
let (result, inctx) = self.ideserialize(inctx)?;
|
||||
let end = inctx.itell();
|
||||
let received_size = end - start;
|
||||
if received_size == expected_size {
|
||||
Ok(result)
|
||||
|
@ -131,13 +131,13 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx>
|
||||
|
||||
type ParseError = SP::ParseError;
|
||||
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let (fa, fb) = SP::factories(&self.factory_data);
|
||||
let a = fa
|
||||
.deserialize(dectx)
|
||||
let (a, inctx) = fa
|
||||
.ideserialize(inctx)
|
||||
.map_err(|e| SP::from_error_a(&self.factory_data, e))?;
|
||||
let b = fb
|
||||
.deserialize(dectx)
|
||||
.deserialize(inctx)
|
||||
.map_err(|e| SP::from_error_b(&self.factory_data, e))?;
|
||||
Ok(StaticPairObject {
|
||||
pair: SP::from_parsed(&self.factory_data, a, b),
|
||||
|
@ -55,13 +55,13 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NullableFac
|
||||
|
||||
type ParseError = PointParseError;
|
||||
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let factory = self.factory.clone();
|
||||
let address = dectx.next_address()?;
|
||||
let (address, inctx) = inctx.icnext_address(|slice| PointParseError::from(slice))?;
|
||||
Ok(if address.point == HASH_ZEROS {
|
||||
Nullable::Null(factory)
|
||||
} else {
|
||||
Nullable::NotNull(Point::from_address(address, factory, dectx.resolver()))
|
||||
Nullable::NotNull(Point::from_address(address, factory, inctx.iresolver()))
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -73,9 +73,9 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for PointFactor
|
||||
|
||||
type ParseError = PointParseError;
|
||||
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let (addresses, deserializer, resolver) = dectx.adr();
|
||||
Ok(addresses.next_point(deserializer, resolver.clone(), self.factory.clone())?)
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
let (point, _) = inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice))?;
|
||||
Ok(point)
|
||||
}
|
||||
|
||||
fn extend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! Previously part of [`crate::rcore`].
|
||||
|
||||
use super::{cast::CastError, wrapped_origin::*, *};
|
||||
use super::{cast::CastError, inlining::*, wrapped_origin::*, *};
|
||||
|
||||
type TypelessSerialize<'a> = dyn 'a + Fn(&mut dyn Serializer);
|
||||
|
||||
@ -17,7 +17,9 @@ type TypelessParsed<'a, Ctx> = Result<TypelessMentionable<'a, Ctx>, Box<dyn 'a +
|
||||
trait Tde<'a, Ctx: Context<'a>>: 'a + Send + Sync {
|
||||
fn clone_box(&self) -> TdeBox<'a, Ctx>;
|
||||
|
||||
fn de(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> TypelessParsed<'a, Ctx>;
|
||||
fn de<'c>(&self, indyn: Box<dyn InCtxDyn<'a, 'c, Ctx>>) -> TypelessParsed<'a, Ctx>
|
||||
where
|
||||
'a: 'c;
|
||||
}
|
||||
|
||||
type TdeBox<'a, Ctx> = Box<dyn Tde<'a, Ctx>>;
|
||||
@ -90,8 +92,10 @@ impl<'a, Ctx: Context<'a>> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> {
|
||||
|
||||
type ParseError = TypelessError<'a>;
|
||||
|
||||
fn deserialize(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
self.t_deserialize.de(dectx).map_err(TypelessError)
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||
self.t_deserialize
|
||||
.de(Box::new(inctx))
|
||||
.map_err(TypelessError)
|
||||
}
|
||||
|
||||
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
|
||||
@ -124,8 +128,11 @@ where
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn de(&self, dectx: &mut dyn DeCtx<'a, Ctx>) -> TypelessParsed<'a, Ctx> {
|
||||
self.deserialize(dectx)
|
||||
fn de<'c>(&self, indyn: Box<dyn InCtxDyn<'a, 'c, Ctx>>) -> TypelessParsed<'a, Ctx>
|
||||
where
|
||||
'a: 'c,
|
||||
{
|
||||
self.deserialize(indyn)
|
||||
.map_err(|e| Box::new(e) as Box<dyn 'a + Error>)
|
||||
.map(Rc::new)
|
||||
.map(TypelessMentionable::from_typed)
|
||||
|
Loading…
Reference in New Issue
Block a user