Factor some common io::Error constants

This commit is contained in:
Benoît du Garreau 2024-04-10 19:40:42 +02:00
parent 5974fe87c4
commit 9c64068ddb
21 changed files with 67 additions and 159 deletions

View File

@ -383,12 +383,7 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
// buffer. // buffer.
let mut bytes = Vec::new(); let mut bytes = Vec::new();
self.read_to_end(&mut bytes)?; self.read_to_end(&mut bytes)?;
let string = crate::str::from_utf8(&bytes).map_err(|_| { let string = crate::str::from_utf8(&bytes).map_err(|_| io::Error::INVALID_UTF8)?;
io::const_io_error!(
io::ErrorKind::InvalidData,
"stream did not contain valid UTF-8",
)
})?;
*buf += string; *buf += string;
Ok(string.len()) Ok(string.len())
} }

View File

@ -75,6 +75,30 @@ impl fmt::Debug for Error {
} }
} }
/// Common errors constants for use in std
#[allow(dead_code)]
impl Error {
pub(crate) const INVALID_UTF8: Self =
const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8");
pub(crate) const READ_EXACT_EOF: Self =
const_io_error!(ErrorKind::UnexpectedEof, "failed to fill whole buffer");
pub(crate) const UNKNOWN_THREAD_COUNT: Self = const_io_error!(
ErrorKind::NotFound,
"The number of hardware threads is not known for the target platform"
);
pub(crate) const UNSUPPORTED_PLATFORM: Self =
const_io_error!(ErrorKind::Unsupported, "operation not supported on this platform");
pub(crate) const WRITE_ALL_EOF: Self =
const_io_error!(ErrorKind::WriteZero, "failed to write whole buffer");
pub(crate) const ZERO_TIMEOUT: Self =
const_io_error!(ErrorKind::InvalidInput, "cannot set a 0 duration timeout");
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl From<alloc::ffi::NulError> for Error { impl From<alloc::ffi::NulError> for Error {
/// Converts a [`alloc::ffi::NulError`] into a [`Error`]. /// Converts a [`alloc::ffi::NulError`] into a [`Error`].

View File

@ -5,9 +5,7 @@ use crate::alloc::Allocator;
use crate::cmp; use crate::cmp;
use crate::collections::VecDeque; use crate::collections::VecDeque;
use crate::fmt; use crate::fmt;
use crate::io::{ use crate::io::{self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
self, BorrowedCursor, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
};
use crate::mem; use crate::mem;
use crate::str; use crate::str;
@ -289,10 +287,7 @@ impl Read for &[u8] {
#[inline] #[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
if buf.len() > self.len() { if buf.len() > self.len() {
return Err(io::const_io_error!( return Err(io::Error::READ_EXACT_EOF);
ErrorKind::UnexpectedEof,
"failed to fill whole buffer"
));
} }
let (a, b) = self.split_at(buf.len()); let (a, b) = self.split_at(buf.len());
@ -312,10 +307,7 @@ impl Read for &[u8] {
#[inline] #[inline]
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> { fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
if cursor.capacity() > self.len() { if cursor.capacity() > self.len() {
return Err(io::const_io_error!( return Err(io::Error::READ_EXACT_EOF);
ErrorKind::UnexpectedEof,
"failed to fill whole buffer"
));
} }
let (a, b) = self.split_at(cursor.capacity()); let (a, b) = self.split_at(cursor.capacity());
@ -336,9 +328,7 @@ impl Read for &[u8] {
#[inline] #[inline]
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
let content = str::from_utf8(self).map_err(|_| { let content = str::from_utf8(self).map_err(|_| io::Error::INVALID_UTF8)?;
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
})?;
buf.push_str(content); buf.push_str(content);
let len = self.len(); let len = self.len();
*self = &self[len..]; *self = &self[len..];
@ -399,11 +389,7 @@ impl Write for &mut [u8] {
#[inline] #[inline]
fn write_all(&mut self, data: &[u8]) -> io::Result<()> { fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
if self.write(data)? == data.len() { if self.write(data)? == data.len() { Ok(()) } else { Err(io::Error::WRITE_ALL_EOF) }
Ok(())
} else {
Err(io::const_io_error!(ErrorKind::WriteZero, "failed to write whole buffer"))
}
} }
#[inline] #[inline]
@ -491,9 +477,7 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
// middle of an UTF-8 character. // middle of an UTF-8 character.
let len = self.len(); let len = self.len();
let content = self.make_contiguous(); let content = self.make_contiguous();
let string = str::from_utf8(content).map_err(|_| { let string = str::from_utf8(content).map_err(|_| io::Error::INVALID_UTF8)?;
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
})?;
buf.push_str(string); buf.push_str(string);
self.clear(); self.clear();
Ok(len) Ok(len)

View File

@ -385,12 +385,7 @@ where
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() }; let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() };
let ret = f(g.buf); let ret = f(g.buf);
if str::from_utf8(&g.buf[g.len..]).is_err() { if str::from_utf8(&g.buf[g.len..]).is_err() {
ret.and_then(|_| { ret.and_then(|_| Err(Error::INVALID_UTF8))
Err(error::const_io_error!(
ErrorKind::InvalidData,
"stream did not contain valid UTF-8"
))
})
} else { } else {
g.len = g.buf.len(); g.len = g.buf.len();
ret ret
@ -566,11 +561,7 @@ pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [
Err(e) => return Err(e), Err(e) => return Err(e),
} }
} }
if !buf.is_empty() { if !buf.is_empty() { Err(Error::READ_EXACT_EOF) } else { Ok(()) }
Err(error::const_io_error!(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
} else {
Ok(())
}
} }
pub(crate) fn default_read_buf<F>(read: F, mut cursor: BorrowedCursor<'_>) -> Result<()> 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>(
} }
if cursor.written() == prev_written { if cursor.written() == prev_written {
return Err(error::const_io_error!( return Err(Error::READ_EXACT_EOF);
ErrorKind::UnexpectedEof,
"failed to fill whole buffer"
));
} }
} }
@ -1709,10 +1697,7 @@ pub trait Write {
while !buf.is_empty() { while !buf.is_empty() {
match self.write(buf) { match self.write(buf) {
Ok(0) => { Ok(0) => {
return Err(error::const_io_error!( return Err(Error::WRITE_ALL_EOF);
ErrorKind::WriteZero,
"failed to write whole buffer",
));
} }
Ok(n) => buf = &buf[n..], Ok(n) => buf = &buf[n..],
Err(ref e) if e.is_interrupted() => {} Err(ref e) if e.is_interrupted() => {}
@ -1777,10 +1762,7 @@ pub trait Write {
while !bufs.is_empty() { while !bufs.is_empty() {
match self.write_vectored(bufs) { match self.write_vectored(bufs) {
Ok(0) => { Ok(0) => {
return Err(error::const_io_error!( return Err(Error::WRITE_ALL_EOF);
ErrorKind::WriteZero,
"failed to write whole buffer",
));
} }
Ok(n) => IoSlice::advance_slices(&mut bufs, n), Ok(n) => IoSlice::advance_slices(&mut bufs, n),
Err(ref e) if e.is_interrupted() => {} Err(ref e) if e.is_interrupted() => {}

View File

@ -117,10 +117,7 @@ impl BorrowedFd<'_> {
#[cfg(any(target_arch = "wasm32", target_os = "hermit"))] #[cfg(any(target_arch = "wasm32", target_os = "hermit"))]
#[stable(feature = "io_safety", since = "1.63.0")] #[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> { pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
Err(crate::io::const_io_error!( Err(crate::io::Error::UNSUPPORTED_PLATFORM)
crate::io::ErrorKind::Unsupported,
"operation not supported on this platform",
))
} }
} }

View File

@ -127,11 +127,7 @@ pub trait FileExt {
Err(e) => return Err(e), Err(e) => return Err(e),
} }
} }
if !buf.is_empty() { if !buf.is_empty() { Err(io::Error::READ_EXACT_EOF) } else { Ok(()) }
Err(io::const_io_error!(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer",))
} else {
Ok(())
}
} }
/// Writes a number of bytes starting from a given offset. /// Writes a number of bytes starting from a given offset.
@ -249,10 +245,7 @@ pub trait FileExt {
while !buf.is_empty() { while !buf.is_empty() {
match self.write_at(buf, offset) { match self.write_at(buf, offset) {
Ok(0) => { Ok(0) => {
return Err(io::const_io_error!( return Err(io::Error::WRITE_ALL_EOF);
io::ErrorKind::WriteZero,
"failed to write whole buffer",
));
} }
Ok(n) => { Ok(n) => {
buf = &buf[n..]; buf = &buf[n..];

View File

@ -86,11 +86,7 @@ pub trait FileExt {
Err(e) => return Err(e), Err(e) => return Err(e),
} }
} }
if !buf.is_empty() { if !buf.is_empty() { Err(io::Error::READ_EXACT_EOF) } else { Ok(()) }
Err(io::const_io_error!(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
} else {
Ok(())
}
} }
/// Writes a number of bytes starting from a given offset. /// Writes a number of bytes starting from a given offset.
@ -153,10 +149,7 @@ pub trait FileExt {
while !buf.is_empty() { while !buf.is_empty() {
match self.write_at(buf, offset) { match self.write_at(buf, offset) {
Ok(0) => { Ok(0) => {
return Err(io::const_io_error!( return Err(io::Error::WRITE_ALL_EOF);
io::ErrorKind::WriteZero,
"failed to write whole buffer",
));
} }
Ok(n) => { Ok(n) => {
buf = &buf[n..]; buf = &buf[n..];

View File

@ -80,10 +80,7 @@ impl Socket {
let mut pollfd = netc::pollfd { fd: self.as_raw_fd(), events: netc::POLLOUT, revents: 0 }; let mut pollfd = netc::pollfd { fd: self.as_raw_fd(), events: netc::POLLOUT, revents: 0 };
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 { if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
let start = Instant::now(); let start = Instant::now();
@ -245,10 +242,7 @@ impl Socket {
let timeout = match dur { let timeout = match dur {
Some(dur) => { Some(dur) => {
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 { if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
let secs = if dur.as_secs() > netc::time_t::MAX as u64 { let secs = if dur.as_secs() > netc::time_t::MAX as u64 {

View File

@ -97,10 +97,7 @@ impl TcpStream {
pub fn connect_timeout(addr: &SocketAddr, dur: Duration) -> io::Result<TcpStream> { pub fn connect_timeout(addr: &SocketAddr, dur: Duration) -> io::Result<TcpStream> {
if dur == Duration::default() { if dur == Duration::default() {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
Self::connect(Ok(addr)) // FIXME: ignoring timeout Self::connect(Ok(addr)) // FIXME: ignoring timeout
} }
@ -108,10 +105,7 @@ impl TcpStream {
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> { pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
match dur { match dur {
Some(dur) if dur == Duration::default() => { Some(dur) if dur == Duration::default() => {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
_ => sgx_ineffective(()), _ => sgx_ineffective(()),
} }
@ -120,10 +114,7 @@ impl TcpStream {
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> { pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
match dur { match dur {
Some(dur) if dur == Duration::default() => { Some(dur) if dur == Duration::default() => {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
_ => sgx_ineffective(()), _ => sgx_ineffective(()),
} }

View File

@ -50,10 +50,7 @@ pub fn unsupported<T>() -> crate::io::Result<T> {
} }
pub fn unsupported_err() -> crate::io::Error { pub fn unsupported_err() -> crate::io::Error {
crate::io::const_io_error!( crate::io::Error::UNSUPPORTED_PLATFORM
crate::io::ErrorKind::Unsupported,
"operation not supported on this platform",
)
} }
#[inline] #[inline]

View File

@ -154,10 +154,7 @@ impl Socket {
} }
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 { if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
let mut timeout = let mut timeout =
@ -306,10 +303,7 @@ impl Socket {
let timeout = match dur { let timeout = match dur {
Some(dur) => { Some(dur) => {
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 { if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
let secs = if dur.as_secs() > netc::c_long::MAX as u64 { let secs = if dur.as_secs() > netc::c_long::MAX as u64 {

View File

@ -141,10 +141,7 @@ impl Drop for Thread {
// Note: Both `sched_getaffinity` and `sysconf` are available but not functional on // Note: Both `sched_getaffinity` and `sysconf` are available but not functional on
// teeos, so this function always returns an Error! // teeos, so this function always returns an Error!
pub fn available_parallelism() -> io::Result<NonZero<usize>> { pub fn available_parallelism() -> io::Result<NonZero<usize>> {
Err(io::Error::new( Err(io::Error::UNKNOWN_THREAD_COUNT)
io::ErrorKind::NotFound,
"The number of hardware threads is not known for the target platform",
))
} }
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {

View File

@ -433,6 +433,6 @@ mod unsupported {
} }
pub fn unsupported_err() -> io::Error { pub fn unsupported_err() -> io::Error {
io::const_io_error!(io::ErrorKind::Unsupported, "operation not supported on this platform",) io::Error::UNSUPPORTED_PLATFORM
} }
} }

View File

@ -175,10 +175,7 @@ impl Socket {
let mut pollfd = libc::pollfd { fd: self.as_raw_fd(), events: libc::POLLOUT, revents: 0 }; let mut pollfd = libc::pollfd { fd: self.as_raw_fd(), events: libc::POLLOUT, revents: 0 };
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 { if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
let start = Instant::now(); let start = Instant::now();
@ -360,10 +357,7 @@ impl Socket {
let timeout = match dur { let timeout = match dur {
Some(dur) => { Some(dur) => {
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 { if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
let secs = if dur.as_secs() > libc::time_t::MAX as u64 { let secs = if dur.as_secs() > libc::time_t::MAX as u64 {

View File

@ -356,7 +356,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
} }
match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } { match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
-1 => Err(io::Error::last_os_error()), -1 => Err(io::Error::last_os_error()),
0 => Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")), 0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
cpus => { cpus => {
let count = cpus as usize; let count = cpus as usize;
// Cover the unusual situation where we were able to get the quota but not the affinity mask // Cover the unusual situation where we were able to get the quota but not the affinity mask
@ -439,7 +439,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
if res == -1 { if res == -1 {
return Err(io::Error::last_os_error()); return Err(io::Error::last_os_error());
} else if cpus == 0 { } else if cpus == 0 {
return Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")); return Err(io::Error::UNKNOWN_THREAD_COUNT);
} }
} }
@ -452,13 +452,13 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
} else { } else {
let cpus = (*_syspage_ptr).num_cpu; let cpus = (*_syspage_ptr).num_cpu;
NonZero::new(cpus as usize) NonZero::new(cpus as usize)
.ok_or(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")) .ok_or(io::Error::UNKNOWN_THREAD_COUNT)
} }
} }
} else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] { } else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
let mut cpus = 0u32; let mut cpus = 0u32;
if unsafe { libc::pset_info(libc::PS_MYID, core::ptr::null_mut(), &mut cpus, core::ptr::null_mut()) } != 0 { if unsafe { libc::pset_info(libc::PS_MYID, core::ptr::null_mut(), &mut cpus, core::ptr::null_mut()) } != 0 {
return Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")); return Err(io::Error::UNKNOWN_THREAD_COUNT);
} }
Ok(unsafe { NonZero::new_unchecked(cpus as usize) }) Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
} else if #[cfg(target_os = "haiku")] { } else if #[cfg(target_os = "haiku")] {
@ -469,7 +469,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
let res = libc::get_system_info(&mut sinfo); let res = libc::get_system_info(&mut sinfo);
if res != libc::B_OK { if res != libc::B_OK {
return Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")); return Err(io::Error::UNKNOWN_THREAD_COUNT);
} }
Ok(NonZero::new_unchecked(sinfo.cpu_count as usize)) Ok(NonZero::new_unchecked(sinfo.cpu_count as usize))

View File

@ -13,10 +13,7 @@ pub fn unsupported<T>() -> std_io::Result<T> {
} }
pub fn unsupported_err() -> std_io::Error { pub fn unsupported_err() -> std_io::Error {
std_io::const_io_error!( std_io::Error::UNSUPPORTED_PLATFORM
std_io::ErrorKind::Unsupported,
"operation not supported on this platform",
)
} }
pub fn is_interrupted(_code: i32) -> bool { pub fn is_interrupted(_code: i32) -> bool {

View File

@ -154,10 +154,7 @@ impl Socket {
match result { match result {
Err(ref error) if error.kind() == io::ErrorKind::WouldBlock => { Err(ref error) if error.kind() == io::ErrorKind::WouldBlock => {
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 { if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
let mut timeout = c::timeval { let mut timeout = c::timeval {
@ -364,10 +361,7 @@ impl Socket {
Some(dur) => { Some(dur) => {
let timeout = sys::dur2timeout(dur); let timeout = sys::dur2timeout(dur);
if timeout == 0 { if timeout == 0 {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
"cannot set a 0 duration timeout",
));
} }
timeout timeout
} }

View File

@ -116,10 +116,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
sysinfo.dwNumberOfProcessors as usize sysinfo.dwNumberOfProcessors as usize
}; };
match res { match res {
0 => Err(io::const_io_error!( 0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
io::ErrorKind::NotFound,
"The number of hardware threads is not known for the target platform",
)),
cpus => Ok(unsafe { NonZero::new_unchecked(cpus) }), cpus => Ok(unsafe { NonZero::new_unchecked(cpus) }),
} }
} }

View File

@ -140,10 +140,7 @@ impl TcpStream {
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> { pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
if let Some(to) = timeout { if let Some(to) = timeout {
if to.is_zero() { if to.is_zero() {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
&"Zero is an invalid timeout",
));
} }
} }
self.read_timeout.store( self.read_timeout.store(
@ -156,10 +153,7 @@ impl TcpStream {
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> { pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
if let Some(to) = timeout { if let Some(to) = timeout {
if to.is_zero() { if to.is_zero() {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
&"Zero is an invalid timeout",
));
} }
} }
self.write_timeout.store( self.write_timeout.store(

View File

@ -331,10 +331,7 @@ impl UdpSocket {
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> { pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
if let Some(d) = timeout { if let Some(d) = timeout {
if d.is_zero() { if d.is_zero() {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
&"Zero duration is invalid"
));
} }
} }
self.read_timeout self.read_timeout
@ -345,10 +342,7 @@ impl UdpSocket {
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> { pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
if let Some(d) = timeout { if let Some(d) = timeout {
if d.is_zero() { if d.is_zero() {
return Err(io::const_io_error!( return Err(io::Error::ZERO_TIMEOUT);
io::ErrorKind::InvalidInput,
&"Zero duration is invalid"
));
} }
} }
self.write_timeout self.write_timeout

View File

@ -54,10 +54,7 @@ pub fn unsupported<T>() -> std_io::Result<T> {
} }
pub fn unsupported_err() -> std_io::Error { pub fn unsupported_err() -> std_io::Error {
std_io::const_io_error!( std_io::Error::UNSUPPORTED_PLATFORM
std_io::ErrorKind::Unsupported,
"operation not supported on this platform",
)
} }
pub fn is_interrupted(_code: i32) -> bool { pub fn is_interrupted(_code: i32) -> bool {