diff --git a/library/alloc/src/io/buffered/mod.rs b/library/alloc/src/io/buffered/mod.rs index 7139968648c9b..22fa92d7a1f53 100644 --- a/library/alloc/src/io/buffered/mod.rs +++ b/library/alloc/src/io/buffered/mod.rs @@ -179,14 +179,6 @@ impl From> for Error { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl error::Error for IntoInnerError { - #[allow(deprecated, deprecated_in_future)] - fn description(&self) -> &str { - error::Error::description(self.error()) - } -} - #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for IntoInnerError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/library/alloc/src/io/error.rs b/library/alloc/src/io/error.rs index c6cff1d0a71ae..4f39afa765ff9 100644 --- a/library/alloc/src/io/error.rs +++ b/library/alloc/src/io/error.rs @@ -74,7 +74,7 @@ enum Repr { #[derive(Debug)] struct Custom { kind: ErrorKind, - error: Box, + error: String, } /// A list specifying general categories of I/O error. @@ -177,6 +177,12 @@ pub enum ErrorKind { /// read. #[stable(feature = "read_exact", since = "1.6.0")] UnexpectedEof, + + /// A marker variant that tells the compiler that users of this enum cannot + /// match it exhaustively. + #[doc(hidden)] + #[stable(feature = "read_exact", since = "1.6.0")] + __Nonexhaustive, } impl ErrorKind { @@ -200,6 +206,7 @@ impl ErrorKind { ErrorKind::Interrupted => "operation interrupted", ErrorKind::Other => "other os error", ErrorKind::UnexpectedEof => "unexpected end of file", + _ => "unknown error", } } } @@ -249,33 +256,15 @@ impl Error { #[stable(feature = "rust1", since = "1.0.0")] pub fn new(kind: ErrorKind, error: E) -> Error where - E: Into>, + E: Into, { Self::_new(kind, error.into()) } - fn _new(kind: ErrorKind, error: Box) -> Error { + fn _new(kind: ErrorKind, error: String) -> Error { Error { repr: Repr::Custom(Box::new(Custom { kind, error })) } } - /// Returns an error representing the last OS error which occurred. - /// - /// This function reads the value of `errno` for the target platform (e.g. - /// `GetLastError` on Windows) and will return a corresponding instance of - /// [`Error`] for the error code. - /// - /// # Examples - /// - /// ``` - /// use std::io::Error; - /// - /// println!("last OS error: {:?}", Error::last_os_error()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - pub fn last_os_error() -> Error { - Error::from_raw_os_error(sys::os::errno() as i32) - } - /// Creates a new instance of an [`Error`] from a particular OS error code. /// /// # Examples @@ -372,11 +361,11 @@ impl Error { /// } /// ``` #[stable(feature = "io_error_inner", since = "1.3.0")] - pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> { + pub fn get_ref(&self) -> Option<&String> { match self.repr { Repr::Os(..) => None, Repr::Simple(..) => None, - Repr::Custom(ref c) => Some(&*c.error), + Repr::Custom(ref c) => Some(&c.error), } } @@ -443,11 +432,11 @@ impl Error { /// } /// ``` #[stable(feature = "io_error_inner", since = "1.3.0")] - pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> { + pub fn get_mut(&mut self) -> Option<&mut String> { match self.repr { Repr::Os(..) => None, Repr::Simple(..) => None, - Repr::Custom(ref mut c) => Some(&mut *c.error), + Repr::Custom(ref mut c) => Some(&mut c.error), } } @@ -479,7 +468,7 @@ impl Error { /// } /// ``` #[stable(feature = "io_error_inner", since = "1.3.0")] - pub fn into_inner(self) -> Option> { + pub fn into_inner(self) -> Option { match self.repr { Repr::Os(..) => None, Repr::Simple(..) => None, @@ -508,7 +497,7 @@ impl Error { #[stable(feature = "rust1", since = "1.0.0")] pub fn kind(&self) -> ErrorKind { match self.repr { - Repr::Os(code) => sys::decode_error_kind(code), + Repr::Os(_code) => ErrorKind::Other, Repr::Custom(ref c) => c.kind, Repr::Simple(kind) => kind, } @@ -521,8 +510,6 @@ impl fmt::Debug for Repr { Repr::Os(code) => fmt .debug_struct("Os") .field("code", &code) - .field("kind", &sys::decode_error_kind(code)) - .field("message", &sys::os::error_string(code)) .finish(), Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt), Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(), @@ -535,8 +522,7 @@ impl fmt::Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match self.repr { Repr::Os(code) => { - let detail = sys::os::error_string(code); - write!(fmt, "{} (os error {})", detail, code) + write!(fmt, "os error {}", code) } Repr::Custom(ref c) => c.error.fmt(fmt), Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()), @@ -544,34 +530,6 @@ impl fmt::Display for Error { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl error::Error for Error { - #[allow(deprecated, deprecated_in_future)] - fn description(&self) -> &str { - match self.repr { - Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(), - Repr::Custom(ref c) => c.error.description(), - } - } - - #[allow(deprecated)] - fn cause(&self) -> Option<&dyn error::Error> { - match self.repr { - Repr::Os(..) => None, - Repr::Simple(..) => None, - Repr::Custom(ref c) => c.error.cause(), - } - } - - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match self.repr { - Repr::Os(..) => None, - Repr::Simple(..) => None, - Repr::Custom(ref c) => c.error.source(), - } - } -} - fn _assert_error_is_sync_send() { fn _is_sync_send() {} _is_sync_send::(); diff --git a/library/alloc/src/io/mod.rs b/library/alloc/src/io/mod.rs index e696de34d0173..454377b98da1b 100644 --- a/library/alloc/src/io/mod.rs +++ b/library/alloc/src/io/mod.rs @@ -293,7 +293,7 @@ pub mod prelude; mod util; use crate::{vec::Vec, string::String}; -const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; +const DEFAULT_BUF_SIZE: usize = 8 * 1024; struct Guard<'a> { buf: &'a mut Vec, @@ -1003,7 +1003,10 @@ pub fn read_to_string(reader: &mut R) -> Result { /// Windows. #[stable(feature = "iovec", since = "1.36.0")] #[repr(transparent)] -pub struct IoSliceMut<'a>(sys::io::IoSliceMut<'a>); +pub struct IoSliceMut<'a> { + vec: iovec, + _p: PhantomData<&'a [u8]>, +} #[stable(feature = "iovec-send-sync", since = "1.44.0")] unsafe impl<'a> Send for IoSliceMut<'a> {} @@ -1014,7 +1017,7 @@ unsafe impl<'a> Sync for IoSliceMut<'a> {} #[stable(feature = "iovec", since = "1.36.0")] impl<'a> fmt::Debug for IoSliceMut<'a> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self.0.as_slice(), fmt) + fmt::Debug::fmt(self.as_slice(), fmt) } } @@ -1027,7 +1030,13 @@ impl<'a> IoSliceMut<'a> { #[stable(feature = "iovec", since = "1.36.0")] #[inline] pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> { - IoSliceMut(sys::io::IoSliceMut::new(buf)) + IoSliceMut { + vec: iovec { + iov_base: buf.as_mut_ptr() as *mut c_void, + iov_len: buf.len() as iov_len_t, + }, + _p: PhantomData, + } } /// Advance the internal cursor of the slice. @@ -1080,10 +1089,34 @@ impl<'a> IoSliceMut<'a> { let bufs = &mut bufs[remove..]; if !bufs.is_empty() { - bufs[0].0.advance(n - accumulated_len) + bufs[0].advance_self(n - accumulated_len) } bufs } + + #[inline] + fn advance_self(&mut self, n: usize) { + if (self.vec.iov_len as usize) < n { + panic!("advancing IoSliceMut beyond its length"); + } + + unsafe { + self.vec.iov_len -= n as iov_len_t; + self.vec.iov_base = self.vec.iov_base.add(n); + } + } + + #[inline] + fn as_slice(&self) -> &[u8] { + unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) } + } + + #[inline] + fn as_mut_slice(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) + } + } } #[stable(feature = "iovec", since = "1.36.0")] @@ -1092,7 +1125,7 @@ impl<'a> Deref for IoSliceMut<'a> { #[inline] fn deref(&self) -> &[u8] { - self.0.as_slice() + self.as_slice() } } @@ -1100,7 +1133,7 @@ impl<'a> Deref for IoSliceMut<'a> { impl<'a> DerefMut for IoSliceMut<'a> { #[inline] fn deref_mut(&mut self) -> &mut [u8] { - self.0.as_mut_slice() + self.as_mut_slice() } } @@ -1112,7 +1145,10 @@ impl<'a> DerefMut for IoSliceMut<'a> { #[stable(feature = "iovec", since = "1.36.0")] #[derive(Copy, Clone)] #[repr(transparent)] -pub struct IoSlice<'a>(sys::io::IoSlice<'a>); +pub struct IoSlice<'a> { + vec: iovec, + _p: PhantomData<&'a [u8]>, +} #[stable(feature = "iovec-send-sync", since = "1.44.0")] unsafe impl<'a> Send for IoSlice<'a> {} @@ -1123,7 +1159,7 @@ unsafe impl<'a> Sync for IoSlice<'a> {} #[stable(feature = "iovec", since = "1.36.0")] impl<'a> fmt::Debug for IoSlice<'a> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self.0.as_slice(), fmt) + fmt::Debug::fmt(self.as_slice(), fmt) } } @@ -1136,7 +1172,13 @@ impl<'a> IoSlice<'a> { #[stable(feature = "iovec", since = "1.36.0")] #[inline] pub fn new(buf: &'a [u8]) -> IoSlice<'a> { - IoSlice(sys::io::IoSlice::new(buf)) + IoSlice { + vec: iovec { + iov_base: buf.as_ptr() as *mut u8 as *mut c_void, + iov_len: buf.len() as iov_len_t, + }, + _p: PhantomData, + } } /// Advance the internal cursor of the slice. @@ -1188,10 +1230,27 @@ impl<'a> IoSlice<'a> { let bufs = &mut bufs[remove..]; if !bufs.is_empty() { - bufs[0].0.advance(n - accumulated_len) + bufs[0].advance_self(n - accumulated_len) } bufs } + + #[inline] + fn advance_self(&mut self, n: usize) { + if (self.vec.iov_len as usize) < n { + panic!("advancing IoSlice beyond its length"); + } + + unsafe { + self.vec.iov_len -= n as iov_len_t; + self.vec.iov_base = self.vec.iov_base.add(n); + } + } + + #[inline] + fn as_slice(&self) -> &[u8] { + unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) } + } } #[stable(feature = "iovec", since = "1.36.0")] @@ -1200,7 +1259,7 @@ impl<'a> Deref for IoSlice<'a> { #[inline] fn deref(&self) -> &[u8] { - self.0.as_slice() + self.as_slice() } }