move deserializer to serialization
This commit is contained in:
parent
9b7c716595
commit
1b00d2c326
28
src/core.rs
28
src/core.rs
@ -99,18 +99,6 @@ pub trait Resolver<'a, Ctx: 'a + Context>: 'a {
|
||||
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`].
|
||||
pub type ParseResult<'a, Ctx, F> =
|
||||
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>> {
|
||||
r_factory: F,
|
||||
r_address: Address,
|
||||
|
@ -24,3 +24,31 @@ impl Serializer for Vec<u8> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user