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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use core::fmt::{Arguments, Debug};
use std::ffi;
use std::io;
use crate::{Flush, IntoTryWriteFn, WriteBytes, WriteStr};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct IoTryWriter<F1, F2>(F1, F2);
pub trait IntoIoWriteResult {
fn into_io_write_result(self, buf: &[u8]) -> io::Result<usize>;
}
pub trait IntoIoFlushResult {
fn into_io_flush_result(self) -> io::Result<()>;
}
impl<F1, F2> IoTryWriter<F1, F2>
where
F1: WriteStr,
F2: Flush,
{
pub fn new(write: F1, flush: F2) -> Self {
Self(write, flush)
}
}
impl<F1> IoTryWriter<F1, ()>
where
F1: WriteStr,
{
pub fn from_closure<F, Ts>(closure: F) -> Self
where
F: IntoTryWriteFn<Ts, TryWriteFn = F1>,
{
Self(closure.into_try_write_fn(), ())
}
}
impl<F1> IoTryWriter<F1, ()>
where
Self: io::Write,
{
pub fn write_fmt(&mut self, args: Arguments<'_>) -> io::Result<()> {
io::Write::write_fmt(self, args)
}
}
impl<F1, F2> io::Write for IoTryWriter<F1, F2>
where
Self: WriteBytes<Output = io::Result<usize>> + Flush<Output = io::Result<()>>,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
WriteBytes::write_bytes(self, buf)
}
fn flush(&mut self) -> io::Result<()> {
Flush::flush(self)
}
}
impl<F1, F2> WriteBytes for IoTryWriter<F1, F2>
where
F1: WriteBytes,
F1::Output: IntoIoWriteResult,
{
type Output = io::Result<usize>;
fn write_bytes(&mut self, buf: &[u8]) -> Self::Output {
self.0.write_bytes(buf).into_io_write_result(buf)
}
}
impl<F1, F2> Flush for IoTryWriter<F1, F2>
where
F2: Flush,
F2::Output: IntoIoFlushResult,
{
type Output = io::Result<()>;
fn flush(&mut self) -> Self::Output {
self.1.flush().into_io_flush_result()
}
}
impl IntoIoWriteResult for () {
fn into_io_write_result(self, buf: &[u8]) -> io::Result<usize> {
Ok(buf.len())
}
}
impl IntoIoWriteResult for usize {
fn into_io_write_result(self, buf: &[u8]) -> io::Result<usize> {
let _ = buf;
Ok(self)
}
}
impl IntoIoWriteResult for Result<(), ffi::NulError> {
fn into_io_write_result(self, buf: &[u8]) -> io::Result<usize> {
self.map_or_else(
|err| Err(io::Error::new(io::ErrorKind::InvalidData, err)),
|_| Ok(buf.len()),
)
}
}
impl IntoIoWriteResult for Result<usize, ffi::NulError> {
fn into_io_write_result(self, buf: &[u8]) -> io::Result<usize> {
let _ = buf;
self.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
}
}
impl IntoIoWriteResult for io::Result<()> {
fn into_io_write_result(self, buf: &[u8]) -> io::Result<usize> {
self.map(|_| buf.len())
}
}
impl IntoIoWriteResult for io::Result<usize> {
fn into_io_write_result(self, buf: &[u8]) -> io::Result<usize> {
let _ = buf;
self
}
}
impl IntoIoFlushResult for () {
fn into_io_flush_result(self) -> io::Result<()> {
Ok(())
}
}
impl IntoIoFlushResult for io::Result<()> {
fn into_io_flush_result(self) -> io::Result<()> {
self
}
}