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