wiwi/serialiser/
binary.rs1#![allow(
2 dead_code,
3 unused_imports,
4 reason = "wip (TODO: remove me)"
5)]
6
7use crate::prelude::*;
8
9mod int;
10
11pub trait Serialise<'h> {
12 type Serialiser: Serialiser<'h>;
13
14 fn serialiser(&'h self) -> Self::Serialiser;
15}
16
17pub trait Serialiser<'h>: Sized {
18 fn serialise<O>(&self, out: O)
19 where
20 O: Output;
21}
22
23pub trait Deserialise<'h>: Sized {
24 type Error: std::error::Error + From<Error>;
25
26 fn deserialise<I>(input: I) -> Result<Self, Self::Error>
27 where
28 I: Input<'h>;
29}
30
31pub trait Input<'h> {
32 fn read_bytes(&mut self, bytes: usize) -> Option<&'h [u8]>;
33}
34
35pub trait Output {
36 fn write_bytes(&mut self, bytes: &[u8]);
37}
38
39pub struct Error {
41 inner: ErrorInner
42}
43
44enum ErrorInner {
45 Something
47}
48
49impl<'h, T> Serialise<'h> for &T
50where
51 T: ?Sized + Serialise<'h>
52{
53 type Serialiser = T::Serialiser;
54
55 #[inline]
56 fn serialiser(&'h self) -> T::Serialiser {
57 T::serialiser(self)
58 }
59}
60
61impl<'h, T> Serialise<'h> for &mut T
62where
63 T: ?Sized + Serialise<'h>
64{
65 type Serialiser = T::Serialiser;
66
67 #[inline]
68 fn serialiser(&'h self) -> T::Serialiser {
69 T::serialiser(self)
70 }
71}
72
73impl<'h, T> Serialise<'h> for Box<T>
74where
75 T: ?Sized + Serialise<'h>
76{
77 type Serialiser = T::Serialiser;
78
79 #[inline]
80 fn serialiser(&'h self) -> T::Serialiser {
81 T::serialiser(self)
82 }
83}