diff --git a/src/core.rs b/src/core.rs index 41ccee6..cf13ba7 100644 --- a/src/core.rs +++ b/src/core.rs @@ -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> = <::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::(self, slice, resolver) } } - -impl Serializer for Vec { - fn write(&mut self, buf: &[u8]) { - self.extend(buf); - } - - fn tell(&self) -> usize { - self.len() - } -} diff --git a/src/core/hashing.rs b/src/core/hashing.rs new file mode 100644 index 0000000..8566c24 --- /dev/null +++ b/src/core/hashing.rs @@ -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]; diff --git a/src/core/serialization.rs b/src/core/serialization.rs new file mode 100644 index 0000000..8bdf8d4 --- /dev/null +++ b/src/core/serialization.rs @@ -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 { + fn write(&mut self, buf: &[u8]) { + self.extend(buf); + } + + fn tell(&self) -> usize { + self.len() + } +}