diff --git a/src/rstd/inlining.rs b/src/rstd/inlining.rs index 0b1a02c..64529e7 100644 --- a/src/rstd/inlining.rs +++ b/src/rstd/inlining.rs @@ -10,6 +10,62 @@ use super::{ *, }; +pub trait InlineableDeserializer: Sized { + fn iread_n( + self, + n: usize, + ok: impl FnOnce(&[u8]) -> A, + err: impl FnOnce(&[u8]) -> E, + ) -> Result<(A, Self), E>; +} + +impl InlineableDeserializer for &mut D { + fn iread_n( + self, + n: usize, + ok: impl FnOnce(&[u8]) -> A, + err: impl FnOnce(&[u8]) -> E, + ) -> Result<(A, Self), E> { + let slice = self.read_n(n); + if slice.len() == n { + Ok((ok(slice), self)) + } else { + Err(err(slice)) + } + } +} + +pub trait InlineableDeCtx<'a, Ctx: Context<'a>>: Sized { + type D: InlineableDeserializer; + + fn iread_n( + self, + n: usize, + ok: impl FnOnce(&[u8]) -> A, + err: impl FnOnce(&[u8]) -> E, + ) -> Result<(A, Self), E>; +} + +struct InlineableDeCtxT<'a: 'c, 'c, Ctx: Context<'a>> { + dectx: &'c mut dyn DeCtx<'a, Ctx>, +} + +impl<'a: 'c, 'c, Ctx: Context<'a>> InlineableDeCtx<'a, Ctx> for InlineableDeCtxT<'a, 'c, Ctx> { + type D = &'c mut dyn Deserializer; + + fn iread_n( + self, + n: usize, + ok: impl FnOnce(&[u8]) -> A, + err: impl FnOnce(&[u8]) -> E, + ) -> Result<(A, Self), E> { + match self.dectx.deserializer().iread_n(n, ok, err) { + Ok((a, _)) => Ok((a, self)), + Err(e) => Err(e), + } + } +} + /// This factory should return an error on EOF. pub trait InlineableFactory<'a, Ctx: Context<'a>>: Factory<'a, Ctx> { fn extension_error(&self, tail: &[u8]) -> Self::ParseError;