summaryrefslogtreecommitdiff
path: root/src/serialise
diff options
context:
space:
mode:
Diffstat (limited to 'src/serialise')
-rw-r--r--src/serialise/mod.rs559
-rw-r--r--src/serialise/test.rs53
2 files changed, 387 insertions, 225 deletions
diff --git a/src/serialise/mod.rs b/src/serialise/mod.rs
index 98c1417..0685323 100644
--- a/src/serialise/mod.rs
+++ b/src/serialise/mod.rs
@@ -22,31 +22,43 @@
#[cfg(test)]
mod test;
-use crate::Sstream;
+use crate::{Error, Sstream};
-use std::convert::Infallible;
-use std::mem::size_of;
-use std::num::NonZero;
+use alloc::boxed::Box;
+use core::convert::Infallible;
+use core::error::Error as StdError;
+use core::mem::size_of;
+use core::num::NonZero;
-/// Denotes a type capable of being serialised.
+/// Types capable of being serialised.
pub trait Serialise: Sized {
- /// The maximum ammount of bytes that can result from serialisation.
+ /// The error of serialisation.
+ ///
+ /// Use [`Infallible`] if **all** deserialisations are infallible, as is the case of zero-length types.
+ type Error;
+
+ /// The maximum amount of bytes that can result from serialisation.
const SERIALISE_LIMIT: usize;
/// Serialises `self` into a byte stream.
///
- /// This function should not append *more* bytes than specified in [`SERIALISE_LIMIT`](Serialise::SERIALISE_LIMIT).
- /// Doing so is considered a logic error.
- fn serialise(&self, stream: &mut Sstream);
+ /// The number of bytes written is returned.
+ /// This should **not** exceed [`SERIALISE_LIMIT`](Serialise::SERIALISE_LIMIT), and doing so is considered a logic error.
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error>;
}
macro_rules! impl_float {
($type:ty) => {
impl Serialise for $type {
+ type Error = Error;
+
const SERIALISE_LIMIT: usize = size_of::<$type>();
- fn serialise(&self, stream: &mut Sstream) {
- stream.append(&self.to_be_bytes())
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let data = self.to_be_bytes();
+ stream.add(&data)?;
+
+ Ok(data.len())
}
}
};
@@ -55,17 +67,28 @@ macro_rules! impl_float {
macro_rules! impl_int {
($type:ty) => {
impl Serialise for $type {
+ type Error = Error;
+
const SERIALISE_LIMIT: usize = size_of::<$type>();
- fn serialise(&self, stream: &mut Sstream) {
- stream.append(&self.to_be_bytes())
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let data = self.to_be_bytes();
+ stream.add(&data)?;
+
+ Ok(data.len())
}
}
+ };
+}
+macro_rules! impl_non_zero {
+ ($type:ty) => {
impl Serialise for NonZero<$type> {
+ type Error = <$type as Serialise>::Error;
+
const SERIALISE_LIMIT: usize = size_of::<$type>();
- fn serialise(&self, stream: &mut Sstream) {
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
self.get().serialise(stream)
}
}
@@ -74,62 +97,82 @@ macro_rules! impl_int {
impl<T0, T1> Serialise for (T0, T1)
where
- T0: Serialise,
- T1: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2> Serialise for (T0, T1, T2)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
+ T2::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3> Serialise for (T0, T1, T2, T3)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
+ T2::SERIALISE_LIMIT
+ T3::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3, T4> Serialise for (T0, T1, T2, T3, T4)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise,
- T4: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>,
+ T4: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
@@ -137,23 +180,29 @@ where
+ T3::SERIALISE_LIMIT
+ T4::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
- self.4.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+ count += self.4.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3, T4, T5> Serialise for (T0, T1, T2, T3, T4, T5)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise,
- T4: Serialise,
- T5: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>,
+ T4: Serialise<Error: StdError + 'static>,
+ T5: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
@@ -162,25 +211,31 @@ where
+ T4::SERIALISE_LIMIT
+ T5::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
- self.4.serialise(stream);
- self.5.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+ count += self.4.serialise(stream)?;
+ count += self.5.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3, T4, T5, T6> Serialise for (T0, T1, T2, T3, T4, T5, T6)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise,
- T4: Serialise,
- T5: Serialise,
- T6: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>,
+ T4: Serialise<Error: StdError + 'static>,
+ T5: Serialise<Error: StdError + 'static>,
+ T6: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
@@ -190,27 +245,33 @@ where
+ T5::SERIALISE_LIMIT
+ T6::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
- self.4.serialise(stream);
- self.5.serialise(stream);
- self.6.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+ count += self.4.serialise(stream)?;
+ count += self.5.serialise(stream)?;
+ count += self.6.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7> Serialise for (T0, T1, T2, T3, T4, T5, T6, T7)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise,
- T4: Serialise,
- T5: Serialise,
- T6: Serialise,
- T7: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>,
+ T4: Serialise<Error: StdError + 'static>,
+ T5: Serialise<Error: StdError + 'static>,
+ T6: Serialise<Error: StdError + 'static>,
+ T7: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
@@ -221,29 +282,35 @@ where
+ T6::SERIALISE_LIMIT
+ T7::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
- self.4.serialise(stream);
- self.5.serialise(stream);
- self.6.serialise(stream);
- self.7.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+ count += self.4.serialise(stream)?;
+ count += self.5.serialise(stream)?;
+ count += self.6.serialise(stream)?;
+ count += self.7.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> Serialise for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise,
- T4: Serialise,
- T5: Serialise,
- T6: Serialise,
- T7: Serialise,
- T8: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>,
+ T4: Serialise<Error: StdError + 'static>,
+ T5: Serialise<Error: StdError + 'static>,
+ T6: Serialise<Error: StdError + 'static>,
+ T7: Serialise<Error: StdError + 'static>,
+ T8: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
@@ -255,31 +322,37 @@ where
+ T7::SERIALISE_LIMIT
+ T8::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
- self.4.serialise(stream);
- self.5.serialise(stream);
- self.6.serialise(stream);
- self.7.serialise(stream);
- self.8.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+ count += self.4.serialise(stream)?;
+ count += self.5.serialise(stream)?;
+ count += self.6.serialise(stream)?;
+ count += self.7.serialise(stream)?;
+ count += self.8.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> Serialise for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise,
- T4: Serialise,
- T5: Serialise,
- T6: Serialise,
- T7: Serialise,
- T8: Serialise,
- T9: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>,
+ T4: Serialise<Error: StdError + 'static>,
+ T5: Serialise<Error: StdError + 'static>,
+ T6: Serialise<Error: StdError + 'static>,
+ T7: Serialise<Error: StdError + 'static>,
+ T8: Serialise<Error: StdError + 'static>,
+ T9: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
@@ -292,33 +365,39 @@ where
+ T8::SERIALISE_LIMIT
+ T9::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
- self.4.serialise(stream);
- self.5.serialise(stream);
- self.6.serialise(stream);
- self.7.serialise(stream);
- self.8.serialise(stream);
- self.9.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+ count += self.4.serialise(stream)?;
+ count += self.5.serialise(stream)?;
+ count += self.6.serialise(stream)?;
+ count += self.7.serialise(stream)?;
+ count += self.8.serialise(stream)?;
+ count += self.9.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Serialise for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise,
- T4: Serialise,
- T5: Serialise,
- T6: Serialise,
- T7: Serialise,
- T8: Serialise,
- T9: Serialise,
- T10: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>,
+ T4: Serialise<Error: StdError + 'static>,
+ T5: Serialise<Error: StdError + 'static>,
+ T6: Serialise<Error: StdError + 'static>,
+ T7: Serialise<Error: StdError + 'static>,
+ T8: Serialise<Error: StdError + 'static>,
+ T9: Serialise<Error: StdError + 'static>,
+ T10: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
@@ -332,35 +411,41 @@ where
+ T9::SERIALISE_LIMIT
+ T10::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
- self.4.serialise(stream);
- self.5.serialise(stream);
- self.6.serialise(stream);
- self.7.serialise(stream);
- self.8.serialise(stream);
- self.9.serialise(stream);
- self.10.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+ count += self.4.serialise(stream)?;
+ count += self.5.serialise(stream)?;
+ count += self.6.serialise(stream)?;
+ count += self.7.serialise(stream)?;
+ count += self.8.serialise(stream)?;
+ count += self.9.serialise(stream)?;
+ count += self.10.serialise(stream)?;
+
+ Ok(count)
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Serialise for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where
- T0: Serialise,
- T1: Serialise,
- T2: Serialise,
- T3: Serialise,
- T4: Serialise,
- T5: Serialise,
- T6: Serialise,
- T7: Serialise,
- T8: Serialise,
- T9: Serialise,
- T10: Serialise,
- T11: Serialise, {
+ T0: Serialise<Error: StdError + 'static>,
+ T1: Serialise<Error: StdError + 'static>,
+ T2: Serialise<Error: StdError + 'static>,
+ T3: Serialise<Error: StdError + 'static>,
+ T4: Serialise<Error: StdError + 'static>,
+ T5: Serialise<Error: StdError + 'static>,
+ T6: Serialise<Error: StdError + 'static>,
+ T7: Serialise<Error: StdError + 'static>,
+ T8: Serialise<Error: StdError + 'static>,
+ T9: Serialise<Error: StdError + 'static>,
+ T10: Serialise<Error: StdError + 'static>,
+ T11: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize =
T0::SERIALISE_LIMIT
+ T1::SERIALISE_LIMIT
@@ -375,79 +460,124 @@ where
+ T10::SERIALISE_LIMIT
+ T11::SERIALISE_LIMIT;
- fn serialise(&self, stream: &mut Sstream) {
- self.0.serialise(stream);
- self.1.serialise(stream);
- self.2.serialise(stream);
- self.3.serialise(stream);
- self.4.serialise(stream);
- self.5.serialise(stream);
- self.6.serialise(stream);
- self.7.serialise(stream);
- self.8.serialise(stream);
- self.9.serialise(stream);
- self.10.serialise(stream);
- self.11.serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
+ count += self.0.serialise(stream)?;
+ count += self.1.serialise(stream)?;
+ count += self.2.serialise(stream)?;
+ count += self.3.serialise(stream)?;
+ count += self.4.serialise(stream)?;
+ count += self.5.serialise(stream)?;
+ count += self.6.serialise(stream)?;
+ count += self.7.serialise(stream)?;
+ count += self.8.serialise(stream)?;
+ count += self.9.serialise(stream)?;
+ count += self.10.serialise(stream)?;
+ count += self.11.serialise(stream)?;
+
+ Ok(count)
}
}
-impl<T: Serialise, const N: usize> Serialise for [T; N] {
+impl<T: Serialise<Error: StdError + 'static>, const N: usize> Serialise for [T; N] {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize = T::SERIALISE_LIMIT * N;
- fn serialise(&self, stream: &mut Sstream) {
- u64::try_from(self.len()).unwrap().serialise(stream);
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
- for v in self { v.serialise(stream) }
+ self.len().serialise(stream)?;
+ for v in self { count += v.serialise(stream)? }
+
+ Ok(count)
}
}
impl Serialise for () {
+ type Error = Infallible;
+
const SERIALISE_LIMIT: usize = size_of::<Self>();
- fn serialise(&self, _stream: &mut Sstream) { }
+ #[inline(always)]
+ fn serialise(&self, mut _stream: &mut Sstream) -> Result<usize, Self::Error> {
+ Ok(Self::SERIALISE_LIMIT)
+ }
}
impl Serialise for bool {
+ type Error = Error;
+
const SERIALISE_LIMIT: usize = size_of::<Self>();
- fn serialise(&self, stream: &mut Sstream) {
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
u8::from(*self).serialise(stream)
}
}
impl Serialise for char {
+ type Error = Error;
+
const SERIALISE_LIMIT: usize = size_of::<Self>();
- fn serialise(&self, stream: &mut Sstream) {
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
u32::from(*self).serialise(stream)
}
}
+// Especially useful for `Result<T, Infallible>`.
impl Serialise for Infallible {
+ type Error = Self;
+
const SERIALISE_LIMIT: usize = size_of::<Self>();
- fn serialise(&self, _stream: &mut Sstream) { unreachable!() }
+ fn serialise(&self, mut _stream: &mut Sstream) -> Result<usize, Self::Error> { unreachable!() }
+}
+
+impl Serialise for isize {
+ type Error = Error;
+
+ const SERIALISE_LIMIT: usize = size_of::<i16>();
+
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let value = i16::try_from(*self)
+ .map_err(|_| Error::IsizeOutOfRange { value: *self })?;
+
+ value.serialise(stream)
+ }
}
-impl<T: Serialise> Serialise for Option<T> {
+impl<T: Serialise<Error: StdError + 'static>> Serialise for Option<T> {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize = T::SERIALISE_LIMIT + 0x1;
- fn serialise(&self, stream: &mut Sstream) {
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let mut count = 0x0;
+
match *self {
None => {
- stream.append_byte(0x00);
- stream.append(&vec![0x00; size_of::<T>()]);
+ count += false.serialise(stream)?;
+ // No need to zero-fill.
},
Some(ref v) => {
- stream.append_byte(0x01);
- v.serialise(stream);
+ count += true.serialise(stream)?;
+ count += v.serialise(stream)?;
},
};
+
+ Ok(count)
}
}
-impl<T: Serialise, E: Serialise> Serialise for Result<T, E> {
+impl<T, E> Serialise for core::result::Result<T, E>
+where
+ T: Serialise<Error: StdError + 'static>,
+ E: Serialise<Error: StdError + 'static>, {
+ type Error = Box<dyn StdError>;
+
const SERIALISE_LIMIT: usize = const {
if size_of::<T>() > size_of::<T>() {
size_of::<T>()
@@ -456,18 +586,36 @@ impl<T: Serialise, E: Serialise> Serialise for Result<T, E> {
}
};
- fn serialise(&self, stream: &mut Sstream) {
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ // Remember the descriminant.
+ let mut count = 0x0;
+
match *self {
Ok(ref v) => {
- stream.append_byte(0x00);
- v.serialise(stream);
+ count += false.serialise(stream)?;
+ count += v.serialise(stream)?;
},
Err(ref e) => {
- stream.append_byte(0x01);
- e.serialise(stream);
+ count += true.serialise(stream)?;
+ count += e.serialise(stream)?;
},
};
+
+ Ok(count)
+ }
+}
+
+impl Serialise for usize {
+ type Error = Error;
+
+ const SERIALISE_LIMIT: Self = size_of::<u16>();
+
+ fn serialise(&self, stream: &mut Sstream) -> Result<usize, Self::Error> {
+ let value = u16::try_from(*self)
+ .map_err(|_| Error::UsizeOutOfRange { value: *self })?;
+
+ value.serialise(stream)
}
}
@@ -484,3 +632,16 @@ impl_int!(u16);
impl_int!(u32);
impl_int!(u64);
impl_int!(u8);
+
+impl_non_zero!(i128);
+impl_non_zero!(i16);
+impl_non_zero!(i32);
+impl_non_zero!(i64);
+impl_non_zero!(i8);
+impl_non_zero!(isize);
+impl_non_zero!(u128);
+impl_non_zero!(u16);
+impl_non_zero!(u32);
+impl_non_zero!(u64);
+impl_non_zero!(u8);
+impl_non_zero!(usize);
diff --git a/src/serialise/test.rs b/src/serialise/test.rs
index 245c2c3..43f2b9f 100644
--- a/src/serialise/test.rs
+++ b/src/serialise/test.rs
@@ -21,34 +21,38 @@
use crate::{FixedString, Serialise, Sstream};
+use alloc::boxed::Box;
+use alloc::vec;
+
#[test]
fn test_serialise() {
- let mut stream = Sstream::new();
+ let mut buf = vec![0x00; 0x50];
+ let mut stream = Sstream::new(&mut buf);
- 0x00_u8.serialise(&mut stream);
- 0xFF_u8.serialise(&mut stream);
- 0x7F_u8.serialise(&mut stream);
+ 0x00_u8.serialise(&mut stream).unwrap();
+ 0xFF_u8.serialise(&mut stream).unwrap();
+ 0x7F_u8.serialise(&mut stream).unwrap();
- 0x0F_7E_u16.serialise(&mut stream);
+ 0x0F_7E_u16.serialise(&mut stream).unwrap();
- 0x00_2F_87_E7_u32.serialise(&mut stream);
+ 0x00_2F_87_E7_u32.serialise(&mut stream).unwrap();
- 0xF3_37_CF_8B_DB_03_2B_39_u64.serialise(&mut stream);
+ 0xF3_37_CF_8B_DB_03_2B_39_u64.serialise(&mut stream).unwrap();
- 0x45_A0_15_6A_36_77_17_8A_83_2E_3C_2C_84_10_58_1A_u128.serialise(&mut stream);
+ 0x45_A0_15_6A_36_77_17_8A_83_2E_3C_2C_84_10_58_1A_u128.serialise(&mut stream).unwrap();
- FixedString::<0x1>::new("A").unwrap().serialise(&mut stream);
- FixedString::<0x8>::new("l\u{00F8}gma\u{00F0}ur").unwrap().serialise(&mut stream);
+ FixedString::<0x1>::new("A").unwrap().serialise(&mut stream).unwrap();
+ FixedString::<0x8>::new("l\u{00F8}gma\u{00F0}ur").unwrap().serialise(&mut stream).unwrap();
- ['\u{03B4}', '\u{0190}', '\u{03BB}', '\u{03A4}', '\u{03B1}'].serialise(&mut stream);
+ ['\u{03B4}', '\u{0190}', '\u{03BB}', '\u{03A4}', '\u{03B1}'].serialise(&mut stream).unwrap();
- Result::<u16, char>::Ok(0x45_45).serialise(&mut stream);
- Result::<u16, char>::Err(char::REPLACEMENT_CHARACTER).serialise(&mut stream);
+ Ok::<u16, char>(0x45_45).serialise(&mut stream).unwrap();
+ Err::<u16, char>(char::REPLACEMENT_CHARACTER).serialise(&mut stream).unwrap();
- Option::<()>::None.serialise(&mut stream);
- Option::<()>::Some(()).serialise(&mut stream);
+ None::<()>.serialise(&mut stream).unwrap();
+ Some::<()>(()).serialise(&mut stream).unwrap();
- let data: Box<[u8]> = stream.into();
+ let data: Box<[u8]> = buf.into();
assert_eq!(
data.as_ref(),
@@ -57,15 +61,12 @@ fn test_serialise() {
0xE7, 0xF3, 0x37, 0xCF, 0x8B, 0xDB, 0x03, 0x2B,
0x39, 0x45, 0xA0, 0x15, 0x6A, 0x36, 0x77, 0x17,
0x8A, 0x83, 0x2E, 0x3C, 0x2C, 0x84, 0x10, 0x58,
- 0x1A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 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,
- ]
+ 0x1A, 0x00, 0x01, 0x41, 0x00, 0x0A, 0x6C, 0xC3,
+ 0xB8, 0x67, 0x6D, 0x61, 0xC3, 0xB0, 0x75, 0x72,
+ 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