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
pub mod prelude;
pub mod cursor;
pub mod error;
#[cfg(feature = "std")]
pub use std::io::{Error, ErrorKind};
#[cfg(not(feature = "std"))]
pub use error::{Error, ErrorKind};
#[cfg(feature = "std")]
pub use std::io::Result;
#[cfg(not(feature = "std"))]
pub type Result<T> = core::result::Result<T, Error>;
pub trait Read {
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
if let Ok(n) = self.read(buf) {
if n == buf.len() {
return Ok(())
}
}
Err(Error::new(ErrorKind::UnexpectedEof, "Out of bytes in reader"))
}
fn iter_bytes(&mut self) -> Bytes<Self>
where Self: Sized,
{
Bytes {
inner: self
}
}
}
pub struct Bytes<'a, R: Read> {
inner: &'a mut R
}
impl<'a, R: Read> Iterator for Bytes<'a, R> {
type Item = Result<u8>;
fn next(&mut self) -> Option<Self::Item> {
let mut byte = [0u8];
Some(
self.inner.read_exact(&mut byte)
.map(|_| byte[0])
)
}
}
#[cfg(feature = "std")]
pub use std::io::SeekFrom;
#[cfg(not(feature = "std"))]
#[derive(Debug, Clone, Copy)]
pub enum SeekFrom {
Start(u64),
End(i64),
Current(i64),
}
pub trait Seek {
fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
}
#[cfg(feature = "std")]
impl<R: std::io::Read> Read for R {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.read(buf)
}
}
#[cfg(feature = "std")]
impl<S: std::io::Seek> Seek for S {
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
self.seek(pos)
}
}
#[cfg(feature = "std")]
pub use std::io::Cursor;
#[cfg(not(feature = "std"))]
pub use cursor::Cursor;