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
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
use core::fmt;
#[cfg(feature = "std")]
use std::io;
use crate::error;
#[cfg(not(feature = "unsealed_read_write"))]
pub trait Write: private::Sealed {
#[doc(hidden)]
type Error: Into<error::Error>;
#[doc(hidden)]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
}
#[cfg(feature = "unsealed_read_write")]
pub trait Write {
type Error: Into<error::Error>;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
}
#[cfg(not(feature = "unsealed_read_write"))]
mod private {
pub trait Sealed {}
}
impl<W> Write for &mut W
where
W: Write,
{
type Error = W::Error;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
(*self).write_all(buf)
}
}
#[cfg(not(feature = "unsealed_read_write"))]
impl<W> private::Sealed for &mut W where W: Write {}
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct IoWrite<W>(W);
#[cfg(feature = "std")]
impl<W: io::Write> IoWrite<W> {
pub fn new(w: W) -> IoWrite<W> {
IoWrite(w)
}
}
#[cfg(feature = "std")]
impl<W: io::Write> Write for IoWrite<W> {
type Error = io::Error;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.0.write_all(buf)
}
}
#[cfg(all(feature = "std", not(feature = "unsealed_read_write")))]
impl<W> private::Sealed for IoWrite<W> where W: io::Write {}
#[cfg(any(feature = "std", feature = "alloc"))]
impl Write for Vec<u8> {
type Error = error::Error;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.extend_from_slice(buf);
Ok(())
}
}
#[cfg(all(
any(feature = "std", feature = "alloc"),
not(feature = "unsealed_read_write")
))]
impl private::Sealed for Vec<u8> {}
#[cfg(not(feature = "std"))]
#[derive(Debug)]
pub struct FmtWrite<'a, W: Write>(&'a mut W);
#[cfg(not(feature = "std"))]
impl<'a, W: Write> FmtWrite<'a, W> {
pub fn new(w: &'a mut W) -> FmtWrite<'a, W> {
FmtWrite(w)
}
}
#[cfg(not(feature = "std"))]
impl<'a, W: Write> fmt::Write for FmtWrite<'a, W> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.0.write_all(s.as_bytes()).map_err(|_| fmt::Error)
}
}
#[cfg(all(not(feature = "std"), not(feature = "unsealed_read_write")))]
impl<'a, W> private::Sealed for FmtWrite<'a, W> where W: Write {}
#[derive(Debug)]
pub struct SliceWrite<'a> {
slice: &'a mut [u8],
index: usize,
}
impl<'a> SliceWrite<'a> {
pub fn new(slice: &'a mut [u8]) -> SliceWrite<'a> {
SliceWrite { slice, index: 0 }
}
pub fn bytes_written(&self) -> usize {
self.index
}
pub fn into_inner(self) -> &'a mut [u8] {
self.slice
}
}
impl<'a> Write for SliceWrite<'a> {
type Error = error::Error;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
if self.slice.len() - self.index < buf.len() {
return Err(error::Error::scratch_too_small(self.index as u64));
}
let end = self.index + buf.len();
self.slice[self.index..end].copy_from_slice(buf);
self.index = end;
Ok(())
}
}
#[cfg(not(feature = "unsealed_read_write"))]
impl<'a> private::Sealed for SliceWrite<'a> {}