summaryrefslogtreecommitdiff
path: root/src/serialise/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/serialise/mod.rs')
-rw-r--r--src/serialise/mod.rs559
1 files changed, 360 insertions, 199 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);