extract hashing and serialization

This commit is contained in:
AF 2023-04-23 00:36:14 +00:00
parent 5869c185ed
commit 9b7c716595
3 changed files with 39 additions and 37 deletions

View File

@ -2,9 +2,13 @@
//! Brings [`Mentionable`]/[`Factory`]/[`Origin`] concepts from the original implementation in Python.
//! Allows for more generic behaviour via [`Context`], as opposed to original async-only.
mod hashing;
mod serialization;
mod slice_deserializer;
mod typeless;
pub use hashing::*;
pub use serialization::*;
pub use slice_deserializer::*;
pub use typeless::*;
@ -46,33 +50,6 @@ pub type Wrapped<'a, Ctx, A> = <<Ctx as Context>::T as WeakFunctor>::F<'a, A>;
/// Wrapped result returned by [`Origin`].
pub type Resolution<'a, Ctx, A> = Wrapped<'a, Ctx, ResolutionResult<'a, Ctx, A>>;
/// Fixed [type@Hash] length.
pub const HASH_SIZE: usize = 32;
/// Zeroed out array of the same length as [`type@Hash`].
/// Used in [`crate::std::nullable`].
pub const HASH_ZEROS: [u8; HASH_SIZE] = [0; HASH_SIZE];
/// For use in [`Point`]/[`Address`].
pub type Hash = [u8; HASH_SIZE];
/// Serialisation mechanism that is chosen over bytestring concatenation
/// both for performance and simplicity.
///
/// See [`Serializable`].
pub trait Serializer {
/// Writes bytes from a slice. Should advance value of [`Serializer::tell()`] by `buf.len()`.
fn write(&mut self, buf: &[u8]);
/// Current position of the stream. It's expected to be used by [`crate::std::inlining`]
fn tell(&self) -> usize;
}
/// See [`Serializer`].
pub trait Serializable {
/// Expected to use [`Serializer::write`].
fn serialize(&self, serializer: &mut dyn Serializer);
}
/// Fundamental trait for ADN objects.
pub trait Mentionable<'a, Ctx: 'a + Context>: 'a + Serializable {
/// Type of the associated factory.
@ -329,13 +306,3 @@ impl<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> ExtFactory<'a, Ctx> for F {
_parse_slice::<Ctx, F::Mtbl>(self, slice, resolver)
}
}
impl Serializer for Vec<u8> {
fn write(&mut self, buf: &[u8]) {
self.extend(buf);
}
fn tell(&self) -> usize {
self.len()
}
}

9
src/core/hashing.rs Normal file
View File

@ -0,0 +1,9 @@
/// Fixed [type@Hash] length.
pub const HASH_SIZE: usize = 32;
/// Zeroed out array of the same length as [`type@Hash`].
/// Used in [`crate::std::nullable`].
pub const HASH_ZEROS: [u8; HASH_SIZE] = [0; HASH_SIZE];
/// For use in [`Point`]/[`Address`].
pub type Hash = [u8; HASH_SIZE];

26
src/core/serialization.rs Normal file
View File

@ -0,0 +1,26 @@
/// Serialisation mechanism that is chosen over bytestring concatenation
/// both for performance and simplicity.
///
/// See [`Serializable`].
pub trait Serializer {
/// Writes bytes from a slice. Should advance value of [`Serializer::tell()`] by `buf.len()`.
fn write(&mut self, buf: &[u8]);
/// Current position of the stream. It's expected to be used by [`crate::std::inlining`]
fn tell(&self) -> usize;
}
/// See [`Serializer`].
pub trait Serializable {
/// Expected to use [`Serializer::write`].
fn serialize(&self, serializer: &mut dyn Serializer);
}
impl Serializer for Vec<u8> {
fn write(&mut self, buf: &[u8]) {
self.extend(buf);
}
fn tell(&self) -> usize {
self.len()
}
}