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
#[cfg(feature = "layered-io")]
use layered_io::{ReadLayered, Status};
use std::io::{self, Read};
pub trait ReadStr: Read {
fn read_str(&mut self, buf: &mut str) -> io::Result<usize>;
#[inline]
fn read_exact_str(&mut self, buf: &mut str) -> io::Result<()> {
default_read_exact_str(self, buf)
}
}
#[cfg(feature = "layered-io")]
pub trait ReadStrLayered: ReadLayered + ReadStr {
fn read_str_with_status(&mut self, buf: &mut str) -> io::Result<(usize, Status)>;
#[inline]
fn read_exact_str_using_status(&mut self, buf: &mut str) -> io::Result<Status> {
default_read_exact_str_using_status(self, buf)
}
}
pub fn default_read_exact_str<Inner: ReadStr + ?Sized>(
inner: &mut Inner,
mut buf: &mut str,
) -> io::Result<()> {
while !buf.is_empty() {
match inner.read_str(buf) {
Ok(0) => break,
Ok(size) => buf = buf.split_at_mut(size).1,
Err(e) => return Err(e),
}
}
if buf.is_empty() {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
))
}
}
#[cfg(feature = "layered-io")]
pub fn default_read_exact_str_using_status<Inner: ReadStrLayered + ?Sized>(
inner: &mut Inner,
mut buf: &mut str,
) -> io::Result<Status> {
let mut result_status = Status::active();
while !buf.is_empty() {
match inner.read_str_with_status(buf) {
Ok((size, status)) => {
buf = buf.split_at_mut(size).1;
if status.is_end() {
result_status = status;
break;
}
}
Err(e) => return Err(e),
}
}
if buf.is_empty() {
Ok(result_status)
} else {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
))
}
}