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
#[cfg(not(feature="no-stdlib"))]
use std::io::{self, ErrorKind, Read, Write};
pub trait CustomWrite<ErrType> {
fn write(self: &mut Self, data: &[u8]) -> Result<usize, ErrType>;
}
pub trait CustomRead<ErrType> {
fn read(self: &mut Self, data: &mut [u8]) -> Result<usize, ErrType>;
}
#[allow(dead_code)]
pub fn write_all<ErrType, OutputType>(w: &mut OutputType, buf: &[u8]) -> Result<(), ErrType>
where OutputType: CustomWrite<ErrType>
{
let mut total_written: usize = 0;
while total_written < buf.len() {
match w.write(&buf[total_written..]) {
Err(e) => return Result::Err(e),
Ok(cur_written) => {
assert_eq!(cur_written == 0, false);
total_written += cur_written;
}
}
}
Ok(())
}
#[cfg(not(feature="no-stdlib"))]
pub struct IntoIoReader<InputType: Read>(pub InputType);
#[cfg(not(feature="no-stdlib"))]
pub struct IoWriterWrapper<'a, OutputType: Write + 'a>(pub &'a mut OutputType);
#[cfg(not(feature="no-stdlib"))]
pub struct IoReaderWrapper<'a, OutputType: Read + 'a>(pub &'a mut OutputType);
#[cfg(not(feature="no-stdlib"))]
impl<'a, OutputType: Write> CustomWrite<io::Error> for IoWriterWrapper<'a, OutputType> {
fn write(self: &mut Self, buf: &[u8]) -> Result<usize, io::Error> {
loop {
match self.0.write(buf) {
Err(e) => {
match e.kind() {
ErrorKind::Interrupted => continue,
_ => return Err(e),
}
}
Ok(cur_written) => return Ok(cur_written),
}
}
}
}
#[cfg(not(feature="no-stdlib"))]
impl<'a, InputType: Read> CustomRead<io::Error> for IoReaderWrapper<'a, InputType> {
fn read(self: &mut Self, buf: &mut [u8]) -> Result<usize, io::Error> {
loop {
match self.0.read(buf) {
Err(e) => {
match e.kind() {
ErrorKind::Interrupted => continue,
_ => return Err(e),
}
}
Ok(cur_read) => return Ok(cur_read),
}
}
}
}
#[cfg(not(feature="no-stdlib"))]
impl<InputType: Read> CustomRead<io::Error> for IntoIoReader<InputType> {
fn read(self: &mut Self, buf: &mut [u8]) -> Result<usize, io::Error> {
loop {
match self.0.read(buf) {
Err(e) => {
match e.kind() {
ErrorKind::Interrupted => continue,
_ => return Err(e),
}
}
Ok(cur_read) => return Ok(cur_read),
}
}
}
}