Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 15d8363

Browse files
committedMar 11, 2025
Implement exact reads for StdinRaw
1 parent 4b59964 commit 15d8363

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed
 

‎library/std/src/io/stdio.rs

+35-28
Original file line numberDiff line numberDiff line change
@@ -97,39 +97,53 @@ const fn stderr_raw() -> StderrRaw {
9797

9898
impl Read for StdinRaw {
9999
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
100-
handle_ebadf(self.0.read(buf), 0)
100+
handle_ebadf(self.0.read(buf), || Ok(0))
101101
}
102102

103103
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
104-
handle_ebadf(self.0.read_buf(buf), ())
104+
handle_ebadf(self.0.read_buf(buf), || Ok(()))
105105
}
106106

107107
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
108-
handle_ebadf(self.0.read_vectored(bufs), 0)
108+
handle_ebadf(self.0.read_vectored(bufs), || Ok(0))
109109
}
110110

111111
#[inline]
112112
fn is_read_vectored(&self) -> bool {
113113
self.0.is_read_vectored()
114114
}
115115

116+
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
117+
if buf.is_empty() {
118+
return Ok(());
119+
}
120+
handle_ebadf(self.0.read_exact(buf), || Err(io::Error::READ_EXACT_EOF))
121+
}
122+
123+
fn read_buf_exact(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
124+
if buf.capacity() == 0 {
125+
return Ok(());
126+
}
127+
handle_ebadf(self.0.read_buf_exact(buf), || Err(io::Error::READ_EXACT_EOF))
128+
}
129+
116130
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
117-
handle_ebadf(self.0.read_to_end(buf), 0)
131+
handle_ebadf(self.0.read_to_end(buf), || Ok(0))
118132
}
119133

120134
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
121-
handle_ebadf(self.0.read_to_string(buf), 0)
135+
handle_ebadf(self.0.read_to_string(buf), || Ok(0))
122136
}
123137
}
124138

125139
impl Write for StdoutRaw {
126140
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
127-
handle_ebadf(self.0.write(buf), buf.len())
141+
handle_ebadf(self.0.write(buf), || Ok(buf.len()))
128142
}
129143

130144
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
131-
let total = || bufs.iter().map(|b| b.len()).sum();
132-
handle_ebadf_lazy(self.0.write_vectored(bufs), total)
145+
let total = || Ok(bufs.iter().map(|b| b.len()).sum());
146+
handle_ebadf(self.0.write_vectored(bufs), total)
133147
}
134148

135149
#[inline]
@@ -138,30 +152,30 @@ impl Write for StdoutRaw {
138152
}
139153

140154
fn flush(&mut self) -> io::Result<()> {
141-
handle_ebadf(self.0.flush(), ())
155+
handle_ebadf(self.0.flush(), || Ok(()))
142156
}
143157

144158
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
145-
handle_ebadf(self.0.write_all(buf), ())
159+
handle_ebadf(self.0.write_all(buf), || Ok(()))
146160
}
147161

148162
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
149-
handle_ebadf(self.0.write_all_vectored(bufs), ())
163+
handle_ebadf(self.0.write_all_vectored(bufs), || Ok(()))
150164
}
151165

152166
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
153-
handle_ebadf(self.0.write_fmt(fmt), ())
167+
handle_ebadf(self.0.write_fmt(fmt), || Ok(()))
154168
}
155169
}
156170

157171
impl Write for StderrRaw {
158172
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
159-
handle_ebadf(self.0.write(buf), buf.len())
173+
handle_ebadf(self.0.write(buf), || Ok(buf.len()))
160174
}
161175

162176
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
163-
let total = || bufs.iter().map(|b| b.len()).sum();
164-
handle_ebadf_lazy(self.0.write_vectored(bufs), total)
177+
let total = || Ok(bufs.iter().map(|b| b.len()).sum());
178+
handle_ebadf(self.0.write_vectored(bufs), total)
165179
}
166180

167181
#[inline]
@@ -170,32 +184,25 @@ impl Write for StderrRaw {
170184
}
171185

172186
fn flush(&mut self) -> io::Result<()> {
173-
handle_ebadf(self.0.flush(), ())
187+
handle_ebadf(self.0.flush(), || Ok(()))
174188
}
175189

176190
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
177-
handle_ebadf(self.0.write_all(buf), ())
191+
handle_ebadf(self.0.write_all(buf), || Ok(()))
178192
}
179193

180194
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
181-
handle_ebadf(self.0.write_all_vectored(bufs), ())
195+
handle_ebadf(self.0.write_all_vectored(bufs), || Ok(()))
182196
}
183197

184198
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
185-
handle_ebadf(self.0.write_fmt(fmt), ())
186-
}
187-
}
188-
189-
fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
190-
match r {
191-
Err(ref e) if stdio::is_ebadf(e) => Ok(default),
192-
r => r,
199+
handle_ebadf(self.0.write_fmt(fmt), || Ok(()))
193200
}
194201
}
195202

196-
fn handle_ebadf_lazy<T>(r: io::Result<T>, default: impl FnOnce() -> T) -> io::Result<T> {
203+
fn handle_ebadf<T>(r: io::Result<T>, default: impl FnOnce() -> io::Result<T>) -> io::Result<T> {
197204
match r {
198-
Err(ref e) if stdio::is_ebadf(e) => Ok(default()),
205+
Err(ref e) if stdio::is_ebadf(e) => default(),
199206
r => r,
200207
}
201208
}

0 commit comments

Comments
 (0)
Please sign in to comment.