move deserializer to serialization

This commit is contained in:
AF 2023-04-23 00:40:31 +00:00
parent 9b7c716595
commit 1b00d2c326
2 changed files with 28 additions and 28 deletions

View File

@ -99,18 +99,6 @@ pub trait Resolver<'a, Ctx: 'a + Context>: 'a {
fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx>; fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx>;
} }
/// Trait representing a readable stream used for parsing.
///
/// See [`Serializer`], [`Factory::deserialize`].
pub trait Deserializer {
/// Read at most `n` bytes.
fn read_n(&mut self, n: usize) -> &[u8];
/// Read til the end of the stream.
fn read_all(&mut self) -> &[u8];
/// See [`Serializer::tell`].
fn tell(&self) -> usize;
}
/// Short-hand for the type of vaalues returned by [`Factory::deserialize`]. /// Short-hand for the type of vaalues returned by [`Factory::deserialize`].
pub type ParseResult<'a, Ctx, F> = pub type ParseResult<'a, Ctx, F> =
Result<<F as Factory<'a, Ctx>>::Mtbl, <F as Factory<'a, Ctx>>::ParseError>; Result<<F as Factory<'a, Ctx>>::Mtbl, <F as Factory<'a, Ctx>>::ParseError>;
@ -222,22 +210,6 @@ impl Addresses {
} }
} }
/// Extension trait for [Deserializer]s.
pub trait ExtDeserializer {
/// Try to read exactly `N` bytes.
fn read_n_const<const N: usize>(&mut self) -> Result<[u8; N], &[u8]>;
}
impl<D: ?Sized + Deserializer> ExtDeserializer for D {
fn read_n_const<const N: usize>(&mut self) -> Result<[u8; N], &[u8]> {
let slice = self.read_n(N);
match slice.try_into() {
Ok(array) => Ok(array),
Err(_) => Err(slice),
}
}
}
struct ResolverOrigin<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> { struct ResolverOrigin<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> {
r_factory: F, r_factory: F,
r_address: Address, r_address: Address,

View File

@ -24,3 +24,31 @@ impl Serializer for Vec<u8> {
self.len() self.len()
} }
} }
/// Trait representing a readable stream used for parsing.
///
/// See [`Serializer`], [`Factory::deserialize`].
pub trait Deserializer {
/// Read at most `n` bytes.
fn read_n(&mut self, n: usize) -> &[u8];
/// Read til the end of the stream.
fn read_all(&mut self) -> &[u8];
/// See [`Serializer::tell`].
fn tell(&self) -> usize;
}
/// Extension trait for [Deserializer]s.
pub trait ExtDeserializer {
/// Try to read exactly `N` bytes.
fn read_n_const<const N: usize>(&mut self) -> Result<[u8; N], &[u8]>;
}
impl<D: ?Sized + Deserializer> ExtDeserializer for D {
fn read_n_const<const N: usize>(&mut self) -> Result<[u8; N], &[u8]> {
let slice = self.read_n(N);
match slice.try_into() {
Ok(array) => Ok(array),
Err(_) => Err(slice),
}
}
}