Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wip]Tentative add alloc::io module. #77922

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 0 additions & 110 deletions Cargo.toml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::cmp;
use core::cmp;
use crate::fmt;
use crate::io::{self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, DEFAULT_BUF_SIZE};
use crate::{boxed::Box, vec::Vec};

/// The `BufReader<R>` struct adds buffering to any reader.
///
Expand Down Expand Up @@ -305,7 +306,7 @@ impl<R: Read> Read for BufReader<R> {

// we can't skip unconditionally because of the large buffer case in read.
unsafe fn initializer(&self) -> Initializer {
self.inner.initializer()
unsafe { self.inner.initializer() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::io::{
self, Error, ErrorKind, IntoInnerError, IoSlice, Seek, SeekFrom, Write, DEFAULT_BUF_SIZE,
};
use crate::mem;
use crate::vec::Vec;

/// Wraps a writer and buffers its output.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ impl<W: Write> LineWriter<W> {
self.inner.get_mut()
}

/// Returns a reference to the internally buffered data.
///
/// # Examples
///
/// ```no_run
/// use std::io::LineWriter;
/// use std::net::TcpStream;
///
/// let buf_writer = LineWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
///
/// // See how many bytes are currently buffered
/// let bytes_buffered = buf_writer.buffer().len();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn buffer(&self) -> &[u8] {
&self.inner.buffer()
}

/// Unwraps this `LineWriter`, returning the underlying writer.
///
/// The internal buffer is written out before returning the writer.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::io::{self, BufWriter, IoSlice, Write};
use crate::memchr;
use core::slice::memchr;

/// Private helper struct for implementing the line-buffered writing logic.
/// This shim temporarily wraps a BufWriter, and uses its internals to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod linewritershim;
#[cfg(test)]
mod tests;

use crate::error;
use crate::fmt;
use crate::io::Error;

Expand Down Expand Up @@ -180,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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ mod tests;

use crate::io::prelude::*;

use crate::cmp;
use core::cmp;
use crate::io::{self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, SeekFrom};

use core::convert::TryInto;
use crate::{vec::Vec, boxed::Box};

/// A `Cursor` wraps an in-memory buffer and provides it with a
/// [`Seek`] implementation.
Expand Down Expand Up @@ -281,7 +282,7 @@ where

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
unsafe { Initializer::nop() }
}
}

Expand Down
85 changes: 21 additions & 64 deletions library/std/src/io/error.rs → library/alloc/src/io/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#[cfg(test)]
mod tests;

use crate::convert::From;
use crate::error;
use crate::fmt;
use crate::result;
use crate::sys;
use core::convert::From;
use core::fmt;
use core::result;
use crate::{boxed::Box, string::String};

/// A specialized [`Result`] type for I/O operations.
///
Expand Down Expand Up @@ -75,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 @@ -178,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 @@ -201,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 @@ -250,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 @@ -373,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 @@ -444,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 @@ -480,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 @@ -509,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 @@ -522,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 @@ -536,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
File renamed without changes.
Loading