summaryrefslogtreecommitdiff
path: root/src/serialise
diff options
context:
space:
mode:
Diffstat (limited to 'src/serialise')
-rw-r--r--src/serialise/mod.rs121
-rw-r--r--src/serialise/s_stream/mod.rs73
-rw-r--r--src/serialise/serialise/mod.rs135
-rw-r--r--src/serialise/test.rs40
4 files changed, 129 insertions, 240 deletions
diff --git a/src/serialise/mod.rs b/src/serialise/mod.rs
index 9fb56e3..1a63080 100644
--- a/src/serialise/mod.rs
+++ b/src/serialise/mod.rs
@@ -19,11 +19,120 @@
// er General Public License along with bzipper. If
// not, see <https://www.gnu.org/licenses/>.
-//! Serialisation utilities.
-
-use crate::use_mod;
-use_mod!(pub s_stream);
-use_mod!(pub serialise);
-
#[cfg(test)]
mod test;
+
+use crate::SStream;
+
+use std::convert::Infallible;
+use std::mem::size_of;
+use std::num::NonZero;
+
+/// Denotes a type capable of being serialised.
+pub trait Serialise: Sized {
+ /// Serialises `self` into a byte stream.
+ ///
+ /// One may assume that the resulting stream has at most the same ammount of bytes as before serialisation.
+ /// Therefore, not observing this rule is a logic error.
+ fn serialise(&self, stream: &mut SStream);
+}
+
+macro_rules! impl_float {
+ ($type:ty) => {
+ impl Serialise for $type {
+ fn serialise(&self, stream: &mut SStream) {
+ stream.append(&self.to_be_bytes())
+ }
+ }
+ };
+}
+
+macro_rules! impl_int {
+ ($type:ty) => {
+ impl Serialise for $type {
+ fn serialise(&self, stream: &mut SStream) {
+ stream.append(&self.to_be_bytes())
+ }
+ }
+
+ impl Serialise for NonZero<$type> {
+ fn serialise(&self, stream: &mut SStream) {
+ self.get().serialise(stream)
+ }
+ }
+ };
+}
+
+impl<T: Serialise, const N: usize> Serialise for [T; N] {
+ fn serialise(&self, stream: &mut SStream) {
+ u64::try_from(self.len()).unwrap().serialise(stream);
+
+ for v in self { v.serialise(stream) }
+ }
+}
+
+impl Serialise for () {
+ fn serialise(&self, _stream: &mut SStream) { }
+}
+
+impl Serialise for bool {
+ fn serialise(&self, stream: &mut SStream) {
+ u8::from(*self).serialise(stream)
+ }
+}
+
+impl Serialise for char {
+ fn serialise(&self, stream: &mut SStream) {
+ u32::from(*self).serialise(stream)
+ }
+}
+
+impl Serialise for Infallible {
+ fn serialise(&self, _stream: &mut SStream) { unreachable!() }
+}
+
+impl<T: Serialise> Serialise for Option<T> {
+ fn serialise(&self, stream: &mut SStream) {
+ match *self {
+ None => {
+ stream.append(&[0x00]);
+ stream.append(&vec![0x00; size_of::<T>()]);
+ },
+
+ Some(ref v) => {
+ stream.append(&[0x01]);
+ v.serialise(stream);
+ },
+ };
+ }
+}
+
+impl<T: Serialise, E: Serialise> Serialise for Result<T, E> {
+ fn serialise(&self, stream: &mut SStream) {
+ match *self {
+ Ok(ref v) => {
+ stream.append(&[0x00]);
+ v.serialise(stream);
+ },
+
+ Err(ref e) => {
+ stream.append(&[0x01]);
+ e.serialise(stream);
+ },
+ };
+ }
+}
+
+impl_float!(f32);
+impl_float!(f64);
+
+impl_int!(i128);
+impl_int!(i16);
+impl_int!(i32);
+impl_int!(i64);
+impl_int!(i8);
+impl_int!(u128);
+impl_int!(u16);
+impl_int!(u32);
+impl_int!(u64);
+impl_int!(u8);
diff --git a/src/serialise/s_stream/mod.rs b/src/serialise/s_stream/mod.rs
deleted file mode 100644
index 20f3fb9..0000000
--- a/src/serialise/s_stream/mod.rs
+++ /dev/null
@@ -1,73 +0,0 @@
-// Copyright 2024 Gabriel Bjørnager Jensen.
-//
-// This file is part of bzipper.
-//
-// bzipper is free software: you can redistribute
-// it and/or modify it under the terms of the GNU
-// Lesser General Public License as published by
-// the Free Software Foundation, either version 3
-// of the License, or (at your option) any later
-// version.
-//
-// bzipper is distributed in the hope that it will
-// be useful, but WITHOUT ANY WARRANTY; without
-// even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Less-
-// er General Public License along with bzipper. If
-// not, see <https://www.gnu.org/licenses/>.
-
-use crate::serialise::Serialise;
-
-use std::fmt::{Debug, Formatter};
-use std::mem::size_of;
-
-#[derive(Clone, Eq, PartialEq)]
-pub struct SStream(Vec<u8>);
-
-impl SStream {
- #[must_use]
- pub const fn new() -> Self { Self(Vec::new()) }
-
- pub fn append(&mut self, extra: &[u8]) {
- self.0.extend(extra);
- }
-}
-
-impl AsRef<[u8]> for SStream {
- #[inline(always)]
- fn as_ref(&self) -> &[u8] { self.0.as_ref() }
-}
-
-impl Debug for SStream {
- fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
- write!(f, "[")?;
-
- for v in &self.0 { write!(f, "{v:#02X},")? };
-
- write!(f, "]")?;
-
- Ok(())
- }
-}
-
-impl Default for SStream {
- #[inline(always)]
- fn default() -> Self { Self::new() }
-}
-
-impl<T: Serialise> From<&T> for SStream {
- fn from(value: &T) -> Self {
- let mut stream = Self(Vec::with_capacity(size_of::<T>()));
- value.serialise(&mut stream);
-
- stream
- }
-}
-
-impl From<SStream> for Box<[u8]> {
- #[inline(always)]
- fn from(value: SStream) -> Self { value.0.into_boxed_slice() }
-}
diff --git a/src/serialise/serialise/mod.rs b/src/serialise/serialise/mod.rs
deleted file mode 100644
index 8ee610c..0000000
--- a/src/serialise/serialise/mod.rs
+++ /dev/null
@@ -1,135 +0,0 @@
-// Copyright 2024 Gabriel Bjørnager Jensen.
-//
-// This file is part of bzipper.
-//
-// bzipper is free software: you can redistribute
-// it and/or modify it under the terms of the GNU
-// Lesser General Public License as published by
-// the Free Software Foundation, either version 3
-// of the License, or (at your option) any later
-// version.
-//
-// bzipper is distributed in the hope that it will
-// be useful, but WITHOUT ANY WARRANTY; without
-// even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Less-
-// er General Public License along with bzipper. If
-// not, see <https://www.gnu.org/licenses/>.
-
-use crate::serialise::SStream;
-
-use std::convert::Infallible;
-use std::mem::size_of;
-use std::num::NonZero;
-
-/// Denotes a type capable of being serialised.
-pub trait Serialise: Sized {
- /// Serialises `self` into a byte stream.
- ///
- /// One may assume that the resulting stream has at most the same ammount of bytes as before serialisation.
- /// Therefore, not observing this rule is a logic error.
- fn serialise(&self, stream: &mut SStream);
-}
-
-macro_rules! impl_float {
- ($type:ty) => {
- impl Serialise for $type {
- fn serialise(&self, stream: &mut SStream) {
- stream.append(&self.to_be_bytes())
- }
- }
- };
-}
-
-macro_rules! impl_int {
- ($type:ty) => {
- impl Serialise for $type {
- fn serialise(&self, stream: &mut SStream) {
- stream.append(&self.to_be_bytes())
- }
- }
-
- impl Serialise for NonZero<$type> {
- fn serialise(&self, stream: &mut SStream) {
- self.get().serialise(stream)
- }
- }
- };
-}
-
-impl<T: Serialise, const N: usize> Serialise for [T; N] {
- fn serialise(&self, stream: &mut SStream) {
- u64::try_from(self.len()).unwrap().serialise(stream);
-
- for v in self { v.serialise(stream) }
- }
-}
-
-impl Serialise for () {
- fn serialise(&self, _stream: &mut SStream) { }
-}
-
-impl Serialise for bool {
- fn serialise(&self, stream: &mut SStream) {
- u8::from(*self).serialise(stream)
- }
-}
-
-impl Serialise for char {
- fn serialise(&self, stream: &mut SStream) {
- u32::from(*self).serialise(stream)
- }
-}
-
-impl Serialise for Infallible {
- fn serialise(&self, _stream: &mut SStream) { unreachable!() }
-}
-
-impl<T: Serialise> Serialise for Option<T> {
- fn serialise(&self, stream: &mut SStream) {
- match *self {
- None => {
- stream.append(&[0x00]);
- stream.append(&vec![0x00; size_of::<T>()]);
- },
-
- Some(ref v) => {
- stream.append(&[0x01]);
- v.serialise(stream);
- },
- };
- }
-}
-
-impl<T: Serialise, E: Serialise> Serialise for Result<T, E> {
- fn serialise(&self, stream: &mut SStream) {
- match *self {
- Ok(ref v) => {
- stream.append(&[0x00]);
- v.serialise(stream);
- },
-
- Err(ref e) => {
- stream.append(&[0x01]);
- e.serialise(stream);
- },
- };
- }
-}
-
-impl_float!(f32);
-impl_float!(f64);
-
-impl_int!(i128);
-impl_int!(i16);
-impl_int!(i32);
-impl_int!(i64);
-impl_int!(i8);
-impl_int!(u128);
-impl_int!(u16);
-impl_int!(u32);
-impl_int!(u64);
-impl_int!(u8);
diff --git a/src/serialise/test.rs b/src/serialise/test.rs
index 8b06ed2..e0a0004 100644
--- a/src/serialise/test.rs
+++ b/src/serialise/test.rs
@@ -1,25 +1,6 @@
-// Copyright 2024 Gabriel Bjørnager Jensen.
-//
-// This file is part of bzipper.
-//
-// bzipper is free software: you can redistribute
-// it and/or modify it under the terms of the GNU
-// Lesser General Public License as published by
-// the Free Software Foundation, either version 3
-// of the License, or (at your option) any later
-// version.
-//
-// bzipper is distributed in the hope that it will
-// be useful, but WITHOUT ANY WARRANTY; without
-// even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Less-
-// er General Public License along with bzipper. If
-// not, see <https://www.gnu.org/licenses/>.
+// Copyright 2022-2024 Gabriel Bjørnager Jensen.
-use crate::serialise::{SStream, Serialise};
+use crate::{FixedString, SStream, Serialise};
#[test]
fn test_serialise() {
@@ -37,6 +18,9 @@ fn test_serialise() {
0x45_A0_15_6A_36_77_17_8A_83_2E_3C_2C_84_10_58_1A_u128.serialise(&mut stream);
+ FixedString::<0x1>::new("A").unwrap().serialise(&mut stream);
+ FixedString::<0x8>::new("l\u{00F8}gma\u{00F0}ur").unwrap().serialise(&mut stream);
+
['\u{03B4}', '\u{0190}', '\u{03BB}', '\u{03A4}', '\u{03B1}'].serialise(&mut stream);
Result::<u16, char>::Ok(0x45_45).serialise(&mut stream);
@@ -55,10 +39,14 @@ fn test_serialise() {
0x39, 0x45, 0xA0, 0x15, 0x6A, 0x36, 0x77, 0x17,
0x8A, 0x83, 0x2E, 0x3C, 0x2C, 0x84, 0x10, 0x58,
0x1A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x05, 0x00, 0x00, 0x03, 0xB4, 0x00, 0x00, 0x01,
- 0x90, 0x00, 0x00, 0x03, 0xBB, 0x00, 0x00, 0x03,
- 0xA4, 0x00, 0x00, 0x03, 0xB1, 0x00, 0x45, 0x45,
- 0x01, 0x00, 0x00, 0xFF, 0xFD, 0x00, 0x01,
+ 0x01, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x0A, 0x6C, 0xC3, 0xB8, 0x67, 0x6D, 0x61,
+ 0xC3, 0xB0, 0x75, 0x72, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x03, 0xB4,
+ 0x00, 0x00, 0x01, 0x90, 0x00, 0x00, 0x03, 0xBB,
+ 0x00, 0x00, 0x03, 0xA4, 0x00, 0x00, 0x03, 0xB1,
+ 0x00, 0x45, 0x45, 0x01, 0x00, 0x00, 0xFF, 0xFD,
+ 0x00, 0x01,
]
);
-}
+} \ No newline at end of file