wiwi/serialiser/
binary.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![allow(
	dead_code,
	unused_imports,
	reason = "wip (todo remove me)"
)]

use crate::prelude_std::*;

mod int;

pub trait Serialise<'h> {
	type Serialiser: Serialiser<'h>;

	fn serialiser(&'h self) -> Self::Serialiser;
}

pub trait Serialiser<'h>: Sized {
	fn serialise<O>(&self, out: O)
	where
		O: Output;
}

pub trait Deserialise<'h>: Sized {
	type Error: std::error::Error + From<Error>;

	fn deserialise<I>(input: I) -> Result<Self, Self::Error>
	where
		I: Input<'h>;
}

pub trait Input<'h> {
	fn read_bytes(&mut self, bytes: usize) -> Option<&'h [u8]>;
}

pub trait Output {
	fn write_bytes(&mut self, bytes: &[u8]);
}

/// Error type but it's blank (it will be filled soon™)
pub struct Error {
	inner: ErrorInner
}

enum ErrorInner {
	/// Something™ happened
	Something
}

impl<'h, T> Serialise<'h> for &T
where
	T: ?Sized + Serialise<'h>
{
	type Serialiser = T::Serialiser;

	#[inline]
	fn serialiser(&'h self) -> T::Serialiser {
		T::serialiser(self)
	}
}

impl<'h, T> Serialise<'h> for &mut T
where
	T: ?Sized + Serialise<'h>
{
	type Serialiser = T::Serialiser;

	#[inline]
	fn serialiser(&'h self) -> T::Serialiser {
		T::serialiser(self)
	}
}

impl<'h, T> Serialise<'h> for Box<T>
where
	T: ?Sized + Serialise<'h>
{
	type Serialiser = T::Serialiser;

	#[inline]
	fn serialiser(&'h self) -> T::Serialiser {
		T::serialiser(self)
	}
}