/
core.rs
158 lines (134 loc) · 3.32 KB
/
core.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Reimplements core logic and types from `std::io` in an `alloc`-friendly
//! fashion.
#![cfg(not(feature = "std"))]
use lib::*;
pub enum ErrorKind {
InvalidData,
WriteZero,
Other,
UnexpectedEof,
}
impl ErrorKind {
#[inline]
fn as_str(&self) -> &'static str {
match self {
ErrorKind::InvalidData => "invalid data",
ErrorKind::WriteZero => "write zero",
ErrorKind::Other => "other os error",
ErrorKind::UnexpectedEof => "unexpected end of file",
}
}
}
pub struct Error {
repr: Repr,
}
enum Repr {
Simple(ErrorKind),
Custom(ErrorKind, Box<dyn serde::de::StdError + Send + Sync>),
}
impl Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.repr {
Repr::Custom(_, msg) => write!(fmt, "{}", msg),
Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
}
}
}
impl Debug for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(self, fmt)
}
}
impl serde::de::StdError for Error {}
impl From<ErrorKind> for Error {
#[inline]
fn from(kind: ErrorKind) -> Error {
Error {
repr: Repr::Simple(kind),
}
}
}
impl Error {
#[inline]
pub fn new<E>(kind: ErrorKind, error: E) -> Error
where
E: Into<Box<dyn serde::de::StdError + Send + Sync>>,
{
Error {
repr: Repr::Custom(kind, error.into()),
}
}
}
pub type Result<T> = result::Result<T, Error>;
pub trait Write {
fn write(&mut self, buf: &[u8]) -> Result<usize>;
fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
while !buf.is_empty() {
match self.write(buf) {
Ok(0) => {
return Err(Error::new(
ErrorKind::WriteZero,
"failed to write whole buffer",
))
}
Ok(n) => buf = &buf[n..],
Err(e) => return Err(e),
}
}
Ok(())
}
fn flush(&mut self) -> Result<()>;
}
impl<W: Write> Write for &mut W {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
(*self).write(buf)
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
(*self).write_all(buf)
}
#[inline]
fn flush(&mut self) -> Result<()> {
(*self).flush()
}
}
impl Write for Vec<u8> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.extend_from_slice(buf);
Ok(buf.len())
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
self.extend_from_slice(buf);
Ok(())
}
#[inline]
fn flush(&mut self) -> Result<()> {
Ok(())
}
}
pub trait Read {
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
fn bytes(self) -> Bytes<Self>
where
Self: Sized,
{
Bytes { inner: self }
}
}
pub struct Bytes<R> {
inner: R,
}
impl<R: Read> Iterator for Bytes<R> {
type Item = Result<u8>;
fn next(&mut self) -> Option<Result<u8>> {
let mut byte = 0;
match self.inner.read(slice::from_mut(&mut byte)) {
Ok(0) => None,
Ok(..) => Some(Ok(byte)),
Err(e) => Some(Err(e)),
}
}
}