Skip to content

Commit 05e7396

Browse files
committed
Factor some common io::Error constants
1 parent 5974fe8 commit 05e7396

File tree

18 files changed

+55
-146
lines changed

18 files changed

+55
-146
lines changed

library/std/src/io/buffered/bufreader.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,7 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
383383
// buffer.
384384
let mut bytes = Vec::new();
385385
self.read_to_end(&mut bytes)?;
386-
let string = crate::str::from_utf8(&bytes).map_err(|_| {
387-
io::const_io_error!(
388-
io::ErrorKind::InvalidData,
389-
"stream did not contain valid UTF-8",
390-
)
391-
})?;
386+
let string = crate::str::from_utf8(&bytes).map_err(|_| io::Error::INVALID_UTF8)?;
392387
*buf += string;
393388
Ok(string.len())
394389
}

library/std/src/io/error.rs

+19
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ impl fmt::Debug for Error {
7575
}
7676
}
7777

78+
/// Common errors constants for use in std
79+
impl Error {
80+
pub(crate) const INVALID_UTF8: Self =
81+
const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8");
82+
83+
pub(crate) const READ_EXACT_EOF: Self =
84+
const_io_error!(ErrorKind::UnexpectedEof, "failed to fill whole buffer");
85+
86+
#[allow(dead_code)]
87+
pub(crate) const UNSUPPORTED_PLATFORM: Self =
88+
const_io_error!(ErrorKind::Unsupported, "operation not supported on this platform");
89+
90+
pub(crate) const WRITE_ALL_EOF: Self =
91+
const_io_error!(ErrorKind::WriteZero, "failed to write whole buffer");
92+
93+
pub(crate) const ZERO_TIMEOUT: Self =
94+
const_io_error!(ErrorKind::InvalidInput, "cannot set a 0 duration timeout");
95+
}
96+
7897
#[stable(feature = "rust1", since = "1.0.0")]
7998
impl From<alloc::ffi::NulError> for Error {
8099
/// Converts a [`alloc::ffi::NulError`] into a [`Error`].

library/std/src/io/impls.rs

+6-22
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use crate::alloc::Allocator;
55
use crate::cmp;
66
use crate::collections::VecDeque;
77
use crate::fmt;
8-
use crate::io::{
9-
self, BorrowedCursor, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
10-
};
8+
use crate::io::{self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
119
use crate::mem;
1210
use crate::str;
1311

@@ -289,10 +287,7 @@ impl Read for &[u8] {
289287
#[inline]
290288
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
291289
if buf.len() > self.len() {
292-
return Err(io::const_io_error!(
293-
ErrorKind::UnexpectedEof,
294-
"failed to fill whole buffer"
295-
));
290+
return Err(io::Error::READ_EXACT_EOF);
296291
}
297292
let (a, b) = self.split_at(buf.len());
298293

@@ -312,10 +307,7 @@ impl Read for &[u8] {
312307
#[inline]
313308
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
314309
if cursor.capacity() > self.len() {
315-
return Err(io::const_io_error!(
316-
ErrorKind::UnexpectedEof,
317-
"failed to fill whole buffer"
318-
));
310+
return Err(io::Error::READ_EXACT_EOF);
319311
}
320312
let (a, b) = self.split_at(cursor.capacity());
321313

@@ -336,9 +328,7 @@ impl Read for &[u8] {
336328

337329
#[inline]
338330
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
339-
let content = str::from_utf8(self).map_err(|_| {
340-
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
341-
})?;
331+
let content = str::from_utf8(self).map_err(|_| io::Error::INVALID_UTF8)?;
342332
buf.push_str(content);
343333
let len = self.len();
344334
*self = &self[len..];
@@ -399,11 +389,7 @@ impl Write for &mut [u8] {
399389

400390
#[inline]
401391
fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
402-
if self.write(data)? == data.len() {
403-
Ok(())
404-
} else {
405-
Err(io::const_io_error!(ErrorKind::WriteZero, "failed to write whole buffer"))
406-
}
392+
if self.write(data)? == data.len() { Ok(()) } else { Err(io::Error::WRITE_ALL_EOF) }
407393
}
408394

409395
#[inline]
@@ -491,9 +477,7 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
491477
// middle of an UTF-8 character.
492478
let len = self.len();
493479
let content = self.make_contiguous();
494-
let string = str::from_utf8(content).map_err(|_| {
495-
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
496-
})?;
480+
let string = str::from_utf8(content).map_err(|_| io::Error::INVALID_UTF8)?;
497481
buf.push_str(string);
498482
self.clear();
499483
Ok(len)

library/std/src/io/mod.rs

+5-23
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,7 @@ where
385385
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() };
386386
let ret = f(g.buf);
387387
if str::from_utf8(&g.buf[g.len..]).is_err() {
388-
ret.and_then(|_| {
389-
Err(error::const_io_error!(
390-
ErrorKind::InvalidData,
391-
"stream did not contain valid UTF-8"
392-
))
393-
})
388+
ret.and_then(|_| Err(Error::INVALID_UTF8))
394389
} else {
395390
g.len = g.buf.len();
396391
ret
@@ -566,11 +561,7 @@ pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [
566561
Err(e) => return Err(e),
567562
}
568563
}
569-
if !buf.is_empty() {
570-
Err(error::const_io_error!(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
571-
} else {
572-
Ok(())
573-
}
564+
if !buf.is_empty() { Err(Error::READ_EXACT_EOF) } else { Ok(()) }
574565
}
575566

576567
pub(crate) fn default_read_buf<F>(read: F, mut cursor: BorrowedCursor<'_>) -> Result<()>
@@ -595,10 +586,7 @@ pub(crate) fn default_read_buf_exact<R: Read + ?Sized>(
595586
}
596587

597588
if cursor.written() == prev_written {
598-
return Err(error::const_io_error!(
599-
ErrorKind::UnexpectedEof,
600-
"failed to fill whole buffer"
601-
));
589+
return Err(Error::READ_EXACT_EOF);
602590
}
603591
}
604592

@@ -1709,10 +1697,7 @@ pub trait Write {
17091697
while !buf.is_empty() {
17101698
match self.write(buf) {
17111699
Ok(0) => {
1712-
return Err(error::const_io_error!(
1713-
ErrorKind::WriteZero,
1714-
"failed to write whole buffer",
1715-
));
1700+
return Err(Error::WRITE_ALL_EOF);
17161701
}
17171702
Ok(n) => buf = &buf[n..],
17181703
Err(ref e) if e.is_interrupted() => {}
@@ -1777,10 +1762,7 @@ pub trait Write {
17771762
while !bufs.is_empty() {
17781763
match self.write_vectored(bufs) {
17791764
Ok(0) => {
1780-
return Err(error::const_io_error!(
1781-
ErrorKind::WriteZero,
1782-
"failed to write whole buffer",
1783-
));
1765+
return Err(Error::WRITE_ALL_EOF);
17841766
}
17851767
Ok(n) => IoSlice::advance_slices(&mut bufs, n),
17861768
Err(ref e) if e.is_interrupted() => {}

library/std/src/os/fd/owned.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ impl BorrowedFd<'_> {
117117
#[cfg(any(target_arch = "wasm32", target_os = "hermit"))]
118118
#[stable(feature = "io_safety", since = "1.63.0")]
119119
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
120-
Err(crate::io::const_io_error!(
121-
crate::io::ErrorKind::Unsupported,
122-
"operation not supported on this platform",
123-
))
120+
Err(crate::io::Error::UNSUPPORTED_PLATFORM)
124121
}
125122
}
126123

library/std/src/os/unix/fs.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,7 @@ pub trait FileExt {
127127
Err(e) => return Err(e),
128128
}
129129
}
130-
if !buf.is_empty() {
131-
Err(io::const_io_error!(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer",))
132-
} else {
133-
Ok(())
134-
}
130+
if !buf.is_empty() { Err(io::Error::READ_EXACT_EOF) } else { Ok(()) }
135131
}
136132

137133
/// Writes a number of bytes starting from a given offset.
@@ -249,10 +245,7 @@ pub trait FileExt {
249245
while !buf.is_empty() {
250246
match self.write_at(buf, offset) {
251247
Ok(0) => {
252-
return Err(io::const_io_error!(
253-
io::ErrorKind::WriteZero,
254-
"failed to write whole buffer",
255-
));
248+
return Err(io::Error::WRITE_ALL_EOF);
256249
}
257250
Ok(n) => {
258251
buf = &buf[n..];

library/std/src/os/wasi/fs.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ pub trait FileExt {
8686
Err(e) => return Err(e),
8787
}
8888
}
89-
if !buf.is_empty() {
90-
Err(io::const_io_error!(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
91-
} else {
92-
Ok(())
93-
}
89+
if !buf.is_empty() { Err(io::Error::READ_EXACT_EOF) } else { Ok(()) }
9490
}
9591

9692
/// Writes a number of bytes starting from a given offset.
@@ -153,10 +149,7 @@ pub trait FileExt {
153149
while !buf.is_empty() {
154150
match self.write_at(buf, offset) {
155151
Ok(0) => {
156-
return Err(io::const_io_error!(
157-
io::ErrorKind::WriteZero,
158-
"failed to write whole buffer",
159-
));
152+
return Err(io::Error::WRITE_ALL_EOF);
160153
}
161154
Ok(n) => {
162155
buf = &buf[n..];

library/std/src/sys/pal/hermit/net.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ impl Socket {
8080
let mut pollfd = netc::pollfd { fd: self.as_raw_fd(), events: netc::POLLOUT, revents: 0 };
8181

8282
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
83-
return Err(io::const_io_error!(
84-
io::ErrorKind::InvalidInput,
85-
"cannot set a 0 duration timeout",
86-
));
83+
return Err(io::Error::ZERO_TIMEOUT);
8784
}
8885

8986
let start = Instant::now();
@@ -245,10 +242,7 @@ impl Socket {
245242
let timeout = match dur {
246243
Some(dur) => {
247244
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
248-
return Err(io::const_io_error!(
249-
io::ErrorKind::InvalidInput,
250-
"cannot set a 0 duration timeout",
251-
));
245+
return Err(io::Error::ZERO_TIMEOUT);
252246
}
253247

254248
let secs = if dur.as_secs() > netc::time_t::MAX as u64 {

library/std/src/sys/pal/sgx/net.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,15 @@ impl TcpStream {
9797

9898
pub fn connect_timeout(addr: &SocketAddr, dur: Duration) -> io::Result<TcpStream> {
9999
if dur == Duration::default() {
100-
return Err(io::const_io_error!(
101-
io::ErrorKind::InvalidInput,
102-
"cannot set a 0 duration timeout",
103-
));
100+
return Err(io::Error::ZERO_TIMEOUT);
104101
}
105102
Self::connect(Ok(addr)) // FIXME: ignoring timeout
106103
}
107104

108105
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
109106
match dur {
110107
Some(dur) if dur == Duration::default() => {
111-
return Err(io::const_io_error!(
112-
io::ErrorKind::InvalidInput,
113-
"cannot set a 0 duration timeout",
114-
));
108+
return Err(io::Error::ZERO_TIMEOUT);
115109
}
116110
_ => sgx_ineffective(()),
117111
}
@@ -120,10 +114,7 @@ impl TcpStream {
120114
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
121115
match dur {
122116
Some(dur) if dur == Duration::default() => {
123-
return Err(io::const_io_error!(
124-
io::ErrorKind::InvalidInput,
125-
"cannot set a 0 duration timeout",
126-
));
117+
return Err(io::Error::ZERO_TIMEOUT);
127118
}
128119
_ => sgx_ineffective(()),
129120
}

library/std/src/sys/pal/solid/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ pub fn unsupported<T>() -> crate::io::Result<T> {
5050
}
5151

5252
pub fn unsupported_err() -> crate::io::Error {
53-
crate::io::const_io_error!(
54-
crate::io::ErrorKind::Unsupported,
55-
"operation not supported on this platform",
56-
)
53+
crate::io::Error::UNSUPPORTED_PLATFORM
5754
}
5855

5956
#[inline]

library/std/src/sys/pal/solid/net.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ impl Socket {
154154
}
155155

156156
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
157-
return Err(io::const_io_error!(
158-
io::ErrorKind::InvalidInput,
159-
"cannot set a 0 duration timeout",
160-
));
157+
return Err(io::Error::ZERO_TIMEOUT);
161158
}
162159

163160
let mut timeout =
@@ -306,10 +303,7 @@ impl Socket {
306303
let timeout = match dur {
307304
Some(dur) => {
308305
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
309-
return Err(io::const_io_error!(
310-
io::ErrorKind::InvalidInput,
311-
"cannot set a 0 duration timeout",
312-
));
306+
return Err(io::Error::ZERO_TIMEOUT);
313307
}
314308

315309
let secs = if dur.as_secs() > netc::c_long::MAX as u64 {

library/std/src/sys/pal/unix/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,6 @@ mod unsupported {
433433
}
434434

435435
pub fn unsupported_err() -> io::Error {
436-
io::const_io_error!(io::ErrorKind::Unsupported, "operation not supported on this platform",)
436+
io::Error::UNSUPPORTED_PLATFORM
437437
}
438438
}

library/std/src/sys/pal/unix/net.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ impl Socket {
175175
let mut pollfd = libc::pollfd { fd: self.as_raw_fd(), events: libc::POLLOUT, revents: 0 };
176176

177177
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
178-
return Err(io::const_io_error!(
179-
io::ErrorKind::InvalidInput,
180-
"cannot set a 0 duration timeout",
181-
));
178+
return Err(io::Error::ZERO_TIMEOUT);
182179
}
183180

184181
let start = Instant::now();
@@ -360,10 +357,7 @@ impl Socket {
360357
let timeout = match dur {
361358
Some(dur) => {
362359
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
363-
return Err(io::const_io_error!(
364-
io::ErrorKind::InvalidInput,
365-
"cannot set a 0 duration timeout",
366-
));
360+
return Err(io::Error::ZERO_TIMEOUT);
367361
}
368362

369363
let secs = if dur.as_secs() > libc::time_t::MAX as u64 {

library/std/src/sys/pal/unsupported/common.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ pub fn unsupported<T>() -> std_io::Result<T> {
1313
}
1414

1515
pub fn unsupported_err() -> std_io::Error {
16-
std_io::const_io_error!(
17-
std_io::ErrorKind::Unsupported,
18-
"operation not supported on this platform",
19-
)
16+
std_io::Error::UNSUPPORTED_PLATFORM
2017
}
2118

2219
pub fn is_interrupted(_code: i32) -> bool {

library/std/src/sys/pal/windows/net.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ impl Socket {
154154
match result {
155155
Err(ref error) if error.kind() == io::ErrorKind::WouldBlock => {
156156
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
157-
return Err(io::const_io_error!(
158-
io::ErrorKind::InvalidInput,
159-
"cannot set a 0 duration timeout",
160-
));
157+
return Err(io::Error::ZERO_TIMEOUT);
161158
}
162159

163160
let mut timeout = c::timeval {
@@ -364,10 +361,7 @@ impl Socket {
364361
Some(dur) => {
365362
let timeout = sys::dur2timeout(dur);
366363
if timeout == 0 {
367-
return Err(io::const_io_error!(
368-
io::ErrorKind::InvalidInput,
369-
"cannot set a 0 duration timeout",
370-
));
364+
return Err(io::Error::ZERO_TIMEOUT);
371365
}
372366
timeout
373367
}

0 commit comments

Comments
 (0)