Skip to content

Commit

Permalink
Fixes alloc::io by removing use String instead complex Error parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
lygstate committed Feb 17, 2021
1 parent bd395d2 commit 74d53b5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 79 deletions.
8 changes: 0 additions & 8 deletions library/alloc/src/io/buffered/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,6 @@ impl<W> From<IntoInnerError<W>> for Error {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
#[allow(deprecated, deprecated_in_future)]
fn description(&self) -> &str {
error::Error::description(self.error())
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W> fmt::Display for IntoInnerError<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
76 changes: 17 additions & 59 deletions library/alloc/src/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum Repr {
#[derive(Debug)]
struct Custom {
kind: ErrorKind,
error: Box<dyn error::Error + Send + Sync>,
error: String,
}

/// A list specifying general categories of I/O error.
Expand Down Expand Up @@ -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 {
Expand All @@ -200,6 +206,7 @@ impl ErrorKind {
ErrorKind::Interrupted => "operation interrupted",
ErrorKind::Other => "other os error",
ErrorKind::UnexpectedEof => "unexpected end of file",
_ => "unknown error",
}
}
}
Expand Down Expand Up @@ -249,33 +256,15 @@ impl Error {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new<E>(kind: ErrorKind, error: E) -> Error
where
E: Into<Box<dyn error::Error + Send + Sync>>,
E: Into<String>,
{
Self::_new(kind, error.into())
}

fn _new(kind: ErrorKind, error: Box<dyn error::Error + Send + Sync>) -> 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
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -479,7 +468,7 @@ impl Error {
/// }
/// ```
#[stable(feature = "io_error_inner", since = "1.3.0")]
pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
pub fn into_inner(self) -> Option<String> {
match self.repr {
Repr::Os(..) => None,
Repr::Simple(..) => None,
Expand Down Expand Up @@ -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,
}
Expand All @@ -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(),
Expand All @@ -535,43 +522,14 @@ 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()),
}
}
}

#[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<T: Sync + Send>() {}
_is_sync_send::<Error>();
Expand Down
83 changes: 71 additions & 12 deletions library/alloc/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
Expand Down Expand Up @@ -1003,7 +1003,10 @@ pub fn read_to_string<R: Read>(reader: &mut R) -> Result<String> {
/// 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> {}
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand Down Expand Up @@ -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")]
Expand All @@ -1092,15 +1125,15 @@ impl<'a> Deref for IoSliceMut<'a> {

#[inline]
fn deref(&self) -> &[u8] {
self.0.as_slice()
self.as_slice()
}
}

#[stable(feature = "iovec", since = "1.36.0")]
impl<'a> DerefMut for IoSliceMut<'a> {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
self.0.as_mut_slice()
self.as_mut_slice()
}
}

Expand All @@ -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> {}
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand Down Expand Up @@ -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")]
Expand All @@ -1200,7 +1259,7 @@ impl<'a> Deref for IoSlice<'a> {

#[inline]
fn deref(&self) -> &[u8] {
self.0.as_slice()
self.as_slice()
}
}

Expand Down

0 comments on commit 74d53b5

Please sign in to comment.