diff --git a/buffered.rs b/buffered.rs index b4c91cc..3c3cad2 100644 --- a/buffered.rs +++ b/buffered.rs @@ -1,14 +1,14 @@ //! Buffering wrappers for I/O traits +use core::prelude::v1::*; use crate::io::prelude::*; -use crate::cmp; -use crate::error; -use crate::fmt; +use core::cmp; +use core::fmt; use crate::io::{ self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, SeekFrom, DEFAULT_BUF_SIZE, }; -use crate::memchr; +use crate::io::memchr; /// The `BufReader` struct adds buffering to any reader. /// @@ -50,7 +50,6 @@ use crate::memchr; /// Ok(()) /// } /// ``` -#[stable(feature = "rust1", since = "1.0.0")] pub struct BufReader { inner: R, buf: Box<[u8]>, @@ -74,7 +73,6 @@ impl BufReader { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn new(inner: R) -> BufReader { BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) } @@ -95,7 +93,6 @@ impl BufReader { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn with_capacity(capacity: usize, inner: R) -> BufReader { unsafe { let mut buffer = Vec::with_capacity(capacity); @@ -125,7 +122,6 @@ impl BufReader { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn get_ref(&self) -> &R { &self.inner } @@ -148,7 +144,6 @@ impl BufReader { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut(&mut self) -> &mut R { &mut self.inner } @@ -174,7 +169,6 @@ impl BufReader { /// Ok(()) /// } /// ``` - #[stable(feature = "bufreader_buffer", since = "1.37.0")] pub fn buffer(&self) -> &[u8] { &self.buf[self.pos..self.cap] } @@ -197,7 +191,6 @@ impl BufReader { /// Ok(()) /// } /// ``` - #[stable(feature = "buffered_io_capacity", since = "1.46.0")] pub fn capacity(&self) -> usize { self.buf.len() } @@ -221,7 +214,6 @@ impl BufReader { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn into_inner(self) -> R { self.inner } @@ -239,7 +231,6 @@ impl BufReader { /// the buffer will not be flushed, allowing for more efficient seeks. /// This method does not return the location of the underlying reader, so the caller /// must track this information themselves if it is required. - #[unstable(feature = "bufreader_seek_relative", issue = "31100")] pub fn seek_relative(&mut self, offset: i64) -> io::Result<()> { let pos = self.pos as u64; if offset < 0 { @@ -259,7 +250,6 @@ impl BufReader { } } -#[stable(feature = "rust1", since = "1.0.0")] impl Read for BufReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { // If we don't have any buffered data and we're doing a massive read @@ -301,7 +291,6 @@ impl Read for BufReader { } } -#[stable(feature = "rust1", since = "1.0.0")] impl BufRead for BufReader { fn fill_buf(&mut self) -> io::Result<&[u8]> { // If we've reached the end of our internal buffer then we need to fetch @@ -321,7 +310,6 @@ impl BufRead for BufReader { } } -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for BufReader where R: fmt::Debug, @@ -334,7 +322,6 @@ where } } -#[stable(feature = "rust1", since = "1.0.0")] impl Seek for BufReader { /// Seek to an offset, in bytes, in the underlying reader. /// @@ -445,7 +432,6 @@ impl Seek for BufReader { /// [`TcpStream::write`]: ../../std/net/struct.TcpStream.html#method.write /// [`TcpStream`]: ../../std/net/struct.TcpStream.html /// [`flush`]: #method.flush -#[stable(feature = "rust1", since = "1.0.0")] pub struct BufWriter { inner: Option, buf: Vec, @@ -480,7 +466,6 @@ pub struct BufWriter { /// }; /// ``` #[derive(Debug)] -#[stable(feature = "rust1", since = "1.0.0")] pub struct IntoInnerError(W, Error); impl BufWriter { @@ -495,7 +480,6 @@ impl BufWriter { /// /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap()); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn new(inner: W) -> BufWriter { BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) } @@ -513,7 +497,6 @@ impl BufWriter { /// let stream = TcpStream::connect("127.0.0.1:34254").unwrap(); /// let mut buffer = BufWriter::with_capacity(100, stream); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn with_capacity(capacity: usize, inner: W) -> BufWriter { BufWriter { inner: Some(inner), buf: Vec::with_capacity(capacity), panicked: false } } @@ -560,7 +543,6 @@ impl BufWriter { /// // we can use reference just like buffer /// let reference = buffer.get_ref(); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() } @@ -580,7 +562,6 @@ impl BufWriter { /// // we can use reference just like buffer /// let reference = buffer.get_mut(); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() } @@ -598,7 +579,6 @@ impl BufWriter { /// // See how many bytes are currently buffered /// let bytes_buffered = buf_writer.buffer().len(); /// ``` - #[stable(feature = "bufreader_buffer", since = "1.37.0")] pub fn buffer(&self) -> &[u8] { &self.buf } @@ -618,7 +598,6 @@ impl BufWriter { /// // Calculate how many bytes can be written without flushing /// let without_flush = capacity - buf_writer.buffer().len(); /// ``` - #[stable(feature = "buffered_io_capacity", since = "1.46.0")] pub fn capacity(&self) -> usize { self.buf.capacity() } @@ -642,7 +621,6 @@ impl BufWriter { /// // unwrap the TcpStream and flush the buffer /// let stream = buffer.into_inner().unwrap(); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn into_inner(mut self) -> Result>> { match self.flush_buf() { Err(e) => Err(IntoInnerError(self, e)), @@ -651,7 +629,6 @@ impl BufWriter { } } -#[stable(feature = "rust1", since = "1.0.0")] impl Write for BufWriter { fn write(&mut self, buf: &[u8]) -> io::Result { if self.buf.len() + buf.len() > self.buf.capacity() { @@ -691,7 +668,6 @@ impl Write for BufWriter { } } -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for BufWriter where W: fmt::Debug, @@ -704,7 +680,6 @@ where } } -#[stable(feature = "rust1", since = "1.0.0")] impl Seek for BufWriter { /// Seek to the offset, in bytes, in the underlying writer. /// @@ -714,7 +689,6 @@ impl Seek for BufWriter { } } -#[stable(feature = "rust1", since = "1.0.0")] impl Drop for BufWriter { fn drop(&mut self) { if self.inner.is_some() && !self.panicked { @@ -753,7 +727,6 @@ impl IntoInnerError { /// } /// }; /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn error(&self) -> &Error { &self.1 } @@ -788,28 +761,17 @@ impl IntoInnerError { /// } /// }; /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn into_inner(self) -> W { self.0 } } -#[stable(feature = "rust1", since = "1.0.0")] impl From> for Error { fn from(iie: IntoInnerError) -> Error { iie.1 } } -#[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 { self.error().fmt(f) @@ -880,7 +842,6 @@ impl fmt::Display for IntoInnerError { /// Ok(()) /// } /// ``` -#[stable(feature = "rust1", since = "1.0.0")] pub struct LineWriter { inner: BufWriter, need_flush: bool, @@ -901,7 +862,6 @@ impl LineWriter { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn new(inner: W) -> LineWriter { // Lines typically aren't that long, don't use a giant buffer LineWriter::with_capacity(1024, inner) @@ -922,7 +882,6 @@ impl LineWriter { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn with_capacity(capacity: usize, inner: W) -> LineWriter { LineWriter { inner: BufWriter::with_capacity(capacity, inner), need_flush: false } } @@ -943,7 +902,6 @@ impl LineWriter { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn get_ref(&self) -> &W { self.inner.get_ref() } @@ -968,7 +926,6 @@ impl LineWriter { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() } @@ -996,7 +953,6 @@ impl LineWriter { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn into_inner(self) -> Result>> { self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { IntoInnerError(LineWriter { inner: buf, need_flush: false }, e) @@ -1004,7 +960,6 @@ impl LineWriter { } } -#[stable(feature = "rust1", since = "1.0.0")] impl Write for LineWriter { fn write(&mut self, buf: &[u8]) -> io::Result { if self.need_flush { @@ -1105,7 +1060,6 @@ impl Write for LineWriter { } } -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for LineWriter where W: fmt::Debug, diff --git a/cursor.rs b/cursor.rs index f4db5f8..4f20b8a 100644 --- a/cursor.rs +++ b/cursor.rs @@ -1,9 +1,9 @@ use crate::io::prelude::*; -use crate::cmp; +use core::cmp; use crate::io::{self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, SeekFrom}; -use core::convert::TryInto; +#[cfg(feature="collections")] use core::convert::TryInto; /// A `Cursor` wraps an in-memory buffer and provides it with a /// [`Seek`] implementation. @@ -71,7 +71,6 @@ use core::convert::TryInto; /// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); /// } /// ``` -#[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone, Debug, Default, Eq, PartialEq)] pub struct Cursor { inner: T, @@ -94,7 +93,6 @@ impl Cursor { /// # fn force_inference(_: &Cursor>) {} /// # force_inference(&buff); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn new(inner: T) -> Cursor { Cursor { pos: 0, inner } } @@ -112,7 +110,6 @@ impl Cursor { /// /// let vec = buff.into_inner(); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn into_inner(self) -> T { self.inner } @@ -130,7 +127,6 @@ impl Cursor { /// /// let reference = buff.get_ref(); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn get_ref(&self) -> &T { &self.inner } @@ -151,7 +147,6 @@ impl Cursor { /// /// let reference = buff.get_mut(); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut(&mut self) -> &mut T { &mut self.inner } @@ -175,7 +170,6 @@ impl Cursor { /// buff.seek(SeekFrom::Current(-1)).unwrap(); /// assert_eq!(buff.position(), 1); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn position(&self) -> u64 { self.pos } @@ -197,13 +191,11 @@ impl Cursor { /// buff.set_position(4); /// assert_eq!(buff.position(), 4); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn set_position(&mut self, pos: u64) { self.pos = pos; } } -#[stable(feature = "rust1", since = "1.0.0")] impl io::Seek for Cursor where T: AsRef<[u8]>, @@ -243,13 +235,12 @@ where } } -#[stable(feature = "rust1", since = "1.0.0")] impl Read for Cursor where T: AsRef<[u8]>, { fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; + let n = Read::read(&mut self.get_buf()?, buf)?; self.pos += n as u64; Ok(n) } @@ -272,7 +263,7 @@ where fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { let n = buf.len(); - Read::read_exact(&mut self.fill_buf()?, buf)?; + Read::read_exact(&mut self.get_buf()?, buf)?; self.pos += n as u64; Ok(()) } @@ -283,15 +274,24 @@ where } } -#[stable(feature = "rust1", since = "1.0.0")] -impl BufRead for Cursor +impl Cursor where T: AsRef<[u8]>, { - fn fill_buf(&mut self) -> io::Result<&[u8]> { + fn get_buf(&mut self) -> io::Result<&[u8]> { let amt = cmp::min(self.pos, self.inner.as_ref().len() as u64); Ok(&self.inner.as_ref()[(amt as usize)..]) } +} + +#[cfg(feature="collections")] +impl BufRead for Cursor +where + T: AsRef<[u8]>, +{ + fn fill_buf(&mut self) -> io::Result<&[u8]> { + self.get_buf() + } fn consume(&mut self, amt: usize) { self.pos += amt as u64; } @@ -324,6 +324,7 @@ fn slice_write_vectored( } // Resizing write implementation +#[cfg(feature="collections")] fn vec_write(pos_mut: &mut u64, vec: &mut Vec, buf: &[u8]) -> io::Result { let pos: usize = (*pos_mut).try_into().map_err(|_| { Error::new( @@ -352,6 +353,7 @@ fn vec_write(pos_mut: &mut u64, vec: &mut Vec, buf: &[u8]) -> io::Result, @@ -364,7 +366,6 @@ fn vec_write_vectored( Ok(nwritten) } -#[stable(feature = "rust1", since = "1.0.0")] impl Write for Cursor<&mut [u8]> { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { @@ -387,7 +388,7 @@ impl Write for Cursor<&mut [u8]> { } } -#[stable(feature = "cursor_mut_vec", since = "1.25.0")] +#[cfg(feature="collections")] impl Write for Cursor<&mut Vec> { fn write(&mut self, buf: &[u8]) -> io::Result { vec_write(&mut self.pos, self.inner, buf) @@ -408,7 +409,7 @@ impl Write for Cursor<&mut Vec> { } } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature = "collections")] impl Write for Cursor> { fn write(&mut self, buf: &[u8]) -> io::Result { vec_write(&mut self.pos, &mut self.inner, buf) @@ -429,8 +430,8 @@ impl Write for Cursor> { } } -#[stable(feature = "cursor_box_slice", since = "1.5.0")] -impl Write for Cursor> { +#[cfg(feature = "alloc")] +impl Write for Cursor<::alloc::boxed::Box<[u8]>> { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { slice_write(&mut self.pos, &mut self.inner, buf) diff --git a/error.rs b/error.rs index f7248e7..f7311ad 100644 --- a/error.rs +++ b/error.rs @@ -1,8 +1,14 @@ -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 core::convert::Into; +use core::marker::{Send, Sync}; +use core::option::Option::{self, Some, None}; +#[cfg(feature="alloc")] use alloc::boxed::Box; +#[cfg(not(feature="alloc"))] use ::FakeBox as Box; +#[cfg(feature="collections")] use collections::string::String; +#[cfg(not(feature="collections"))] use ::ErrorString as String; /// A specialized [`Result`](../result/enum.Result.html) type for I/O /// operations. @@ -38,7 +44,6 @@ use crate::sys; /// Ok(buffer) /// } /// ``` -#[stable(feature = "rust1", since = "1.0.0")] pub type Result = result::Result; /// The error type for I/O operations of the [`Read`], [`Write`], [`Seek`], and @@ -52,12 +57,10 @@ pub type Result = result::Result; /// [`Write`]: ../io/trait.Write.html /// [`Seek`]: ../io/trait.Seek.html /// [`ErrorKind`]: enum.ErrorKind.html -#[stable(feature = "rust1", since = "1.0.0")] pub struct Error { repr: Repr, } -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.repr, f) @@ -67,13 +70,16 @@ impl fmt::Debug for Error { enum Repr { Os(i32), Simple(ErrorKind), + #[cfg(feature="alloc")] Custom(Box), + #[cfg(not(feature="alloc"))] + Custom(Custom), } #[derive(Debug)] struct Custom { kind: ErrorKind, - error: Box, + error: String, } /// A list specifying general categories of I/O error. @@ -85,48 +91,35 @@ struct Custom { /// /// [`io::Error`]: struct.Error.html #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -#[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated)] #[non_exhaustive] pub enum ErrorKind { /// An entity was not found, often a file. - #[stable(feature = "rust1", since = "1.0.0")] NotFound, /// The operation lacked the necessary privileges to complete. - #[stable(feature = "rust1", since = "1.0.0")] PermissionDenied, /// The connection was refused by the remote server. - #[stable(feature = "rust1", since = "1.0.0")] ConnectionRefused, /// The connection was reset by the remote server. - #[stable(feature = "rust1", since = "1.0.0")] ConnectionReset, /// The connection was aborted (terminated) by the remote server. - #[stable(feature = "rust1", since = "1.0.0")] ConnectionAborted, /// The network operation failed because it was not connected yet. - #[stable(feature = "rust1", since = "1.0.0")] NotConnected, /// A socket address could not be bound because the address is already in /// use elsewhere. - #[stable(feature = "rust1", since = "1.0.0")] AddrInUse, /// A nonexistent interface was requested or the requested address was not /// local. - #[stable(feature = "rust1", since = "1.0.0")] AddrNotAvailable, /// The operation failed because a pipe was closed. - #[stable(feature = "rust1", since = "1.0.0")] BrokenPipe, /// An entity already exists, often a file. - #[stable(feature = "rust1", since = "1.0.0")] AlreadyExists, /// The operation needs to block to complete, but the blocking operation was /// requested to not occur. - #[stable(feature = "rust1", since = "1.0.0")] WouldBlock, /// A parameter was incorrect. - #[stable(feature = "rust1", since = "1.0.0")] InvalidInput, /// Data not valid for the operation were encountered. /// @@ -138,10 +131,8 @@ pub enum ErrorKind { /// `InvalidData` if the file's contents are not valid UTF-8. /// /// [`InvalidInput`]: #variant.InvalidInput - #[stable(feature = "io_invalid_data", since = "1.2.0")] InvalidData, /// The I/O operation's timeout expired, causing it to be canceled. - #[stable(feature = "rust1", since = "1.0.0")] TimedOut, /// An error returned when an operation could not be completed because a /// call to [`write`] returned [`Ok(0)`]. @@ -152,12 +143,10 @@ pub enum ErrorKind { /// /// [`write`]: ../../std/io/trait.Write.html#tymethod.write /// [`Ok(0)`]: ../../std/io/type.Result.html - #[stable(feature = "rust1", since = "1.0.0")] WriteZero, /// This operation was interrupted. /// /// Interrupted operations can typically be retried. - #[stable(feature = "rust1", since = "1.0.0")] Interrupted, /// Any I/O error not part of this list. /// @@ -165,7 +154,6 @@ pub enum ErrorKind { /// [`ErrorKind`] variant in the future. It is not recommended to match /// an error against `Other` and to expect any additional characteristics, /// e.g., a specific [`Error::raw_os_error`] return value. - #[stable(feature = "rust1", since = "1.0.0")] Other, /// An error returned when an operation could not be completed because an @@ -174,7 +162,6 @@ pub enum ErrorKind { /// This typically means that an operation could only succeed if it read a /// particular number of bytes but only a smaller number of bytes could be /// read. - #[stable(feature = "read_exact", since = "1.6.0")] UnexpectedEof, } @@ -205,7 +192,6 @@ impl ErrorKind { /// Intended for use for errors not exposed to the user, where allocating onto /// the heap (for normal construction via Error::new) is too costly. -#[stable(feature = "io_error_from_errorkind", since = "1.14.0")] impl From for Error { /// Converts an [`ErrorKind`] into an [`Error`]. /// @@ -248,36 +234,17 @@ impl Error { /// // errors can also be created from other errors /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_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 @@ -303,7 +270,6 @@ impl Error { /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput); /// # } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn from_raw_os_error(code: i32) -> Error { Error { repr: Repr::Os(code) } } @@ -334,7 +300,6 @@ impl Error { /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn raw_os_error(&self) -> Option { match self.repr { Repr::Os(i) => Some(i), @@ -368,12 +333,11 @@ impl Error { /// print_error(&Error::new(ErrorKind::Other, "oh no!")); /// } /// ``` - #[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), } } @@ -437,12 +401,11 @@ impl Error { /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new()))); /// } /// ``` - #[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), } } @@ -471,8 +434,7 @@ impl Error { /// print_error(Error::new(ErrorKind::Other, "oh no!")); /// } /// ``` - #[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, @@ -498,10 +460,9 @@ impl Error { /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!")); /// } /// ``` - #[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, } @@ -514,8 +475,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(), @@ -523,13 +482,11 @@ impl fmt::Debug for Repr { } } -#[stable(feature = "rust1", since = "1.0.0")] 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()), @@ -537,34 +494,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/impls.rs b/impls.rs index 01dff0b..eb78a0f 100644 --- a/impls.rs +++ b/impls.rs @@ -1,14 +1,18 @@ -use crate::cmp; -use crate::fmt; +use core::cmp; +use core::fmt; use crate::io::{ - self, BufRead, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write, + self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write, }; -use crate::mem; +#[cfg(feature="collections")] use crate::io::BufRead; +use core::mem; + +#[cfg(feature="alloc")] use alloc::boxed::Box; +#[cfg(feature="collections")] use collections::string::String; +#[cfg(feature="collections")] use collections::vec::Vec; // ============================================================================= // Forwarding implementations -#[stable(feature = "rust1", since = "1.0.0")] impl Read for &mut R { #[inline] fn read(&mut self, buf: &mut [u8]) -> io::Result { @@ -30,11 +34,13 @@ impl Read for &mut R { (**self).initializer() } + #[cfg(feature="collections")] #[inline] fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { (**self).read_to_end(buf) } + #[cfg(feature="collections")] #[inline] fn read_to_string(&mut self, buf: &mut String) -> io::Result { (**self).read_to_string(buf) @@ -45,7 +51,6 @@ impl Read for &mut R { (**self).read_exact(buf) } } -#[stable(feature = "rust1", since = "1.0.0")] impl Write for &mut W { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { @@ -77,14 +82,13 @@ impl Write for &mut W { (**self).write_fmt(fmt) } } -#[stable(feature = "rust1", since = "1.0.0")] impl Seek for &mut S { #[inline] fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] impl BufRead for &mut B { #[inline] fn fill_buf(&mut self) -> io::Result<&[u8]> { @@ -107,7 +111,7 @@ impl BufRead for &mut B { } } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="alloc")] impl Read for Box { #[inline] fn read(&mut self, buf: &mut [u8]) -> io::Result { @@ -129,11 +133,13 @@ impl Read for Box { (**self).initializer() } + #[cfg(feature="collections")] #[inline] fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { (**self).read_to_end(buf) } + #[cfg(feature="collections")] #[inline] fn read_to_string(&mut self, buf: &mut String) -> io::Result { (**self).read_to_string(buf) @@ -144,7 +150,7 @@ impl Read for Box { (**self).read_exact(buf) } } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="alloc")] impl Write for Box { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { @@ -176,14 +182,14 @@ impl Write for Box { (**self).write_fmt(fmt) } } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="alloc")] impl Seek for Box { #[inline] fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] impl BufRead for Box { #[inline] fn fill_buf(&mut self) -> io::Result<&[u8]> { @@ -227,7 +233,6 @@ impl Write for Box { /// /// Note that reading updates the slice to point to the yet unread part. /// The slice will be empty when EOF is reached. -#[stable(feature = "rust1", since = "1.0.0")] impl Read for &[u8] { #[inline] fn read(&mut self, buf: &mut [u8]) -> io::Result { @@ -290,6 +295,7 @@ impl Read for &[u8] { Ok(()) } + #[cfg(feature="collections")] #[inline] fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { buf.extend_from_slice(*self); @@ -299,7 +305,7 @@ impl Read for &[u8] { } } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] impl BufRead for &[u8] { #[inline] fn fill_buf(&mut self) -> io::Result<&[u8]> { @@ -317,7 +323,6 @@ impl BufRead for &[u8] { /// /// Note that writing updates the slice to point to the yet unwritten part. /// The slice will be empty when it has been completely overwritten. -#[stable(feature = "rust1", since = "1.0.0")] impl Write for &mut [u8] { #[inline] fn write(&mut self, data: &[u8]) -> io::Result { @@ -363,7 +368,7 @@ impl Write for &mut [u8] { /// Write is implemented for `Vec` by appending to the vector. /// The vector will grow as needed. -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] impl Write for Vec { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { diff --git a/mod.rs b/mod.rs index e90ee5c..09a607f 100644 --- a/mod.rs +++ b/mod.rs @@ -247,53 +247,43 @@ //! [`Result`]: crate::result::Result //! [`.unwrap()`]: crate::result::Result::unwrap -#![stable(feature = "rust1", since = "1.0.0")] - -use crate::cmp; -use crate::fmt; -use crate::memchr; -use crate::ops::{Deref, DerefMut}; -use crate::ptr; -use crate::slice; -use crate::str; -use crate::sys; - -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::buffered::IntoInnerError; -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[stable(feature = "rust1", since = "1.0.0")] +use core::cmp; +use core::fmt; +#[cfg(not(core_memchr))] +mod memchr; +#[cfg(all(feature="collections",core_memchr))] +use core::slice::memchr; +use core::ops::{Deref, DerefMut}; +use core::ptr; +use core::slice; +use core::str; + +#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; +#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; + pub use self::cursor::Cursor; -#[stable(feature = "rust1", since = "1.0.0")] pub use self::error::{Error, ErrorKind, Result}; -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::stdio::{stderr, stdin, stdout, Stderr, Stdin, Stdout}; -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::stdio::{StderrLock, StdinLock, StdoutLock}; -#[unstable(feature = "print_internals", issue = "none")] -pub use self::stdio::{_eprint, _print}; -#[unstable(feature = "libstd_io_internals", issue = "42788")] -#[doc(no_inline, hidden)] -pub use self::stdio::{set_panic, set_print}; -#[stable(feature = "rust1", since = "1.0.0")] pub use self::util::{copy, empty, repeat, sink, Empty, Repeat, Sink}; -mod buffered; +#[cfg(feature="collections")] use collections::string::String; +#[cfg(feature="collections")] use collections::vec::Vec; + +#[cfg(feature="collections")] mod buffered; mod cursor; mod error; mod impls; -mod lazy; pub mod prelude; -mod stdio; mod util; -const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; +const DEFAULT_BUF_SIZE: usize = 8 * 1024; +#[cfg(feature="collections")] struct Guard<'a> { buf: &'a mut Vec, len: usize, } +#[cfg(feature="collections")] impl Drop for Guard<'_> { fn drop(&mut self) { unsafe { @@ -320,6 +310,7 @@ impl Drop for Guard<'_> { // 2. We're passing a raw buffer to the function `f`, and it is expected that // the function only *appends* bytes to the buffer. We'll get undefined // behavior if existing bytes are overwritten to have non-UTF-8 data. +#[cfg(feature="collections")] fn append_to_string(buf: &mut String, f: F) -> Result where F: FnOnce(&mut Vec) -> Result, @@ -347,10 +338,12 @@ where // // Because we're extending the buffer with uninitialized data for trusted // readers, we need to make sure to truncate that if any of this panics. +#[cfg(feature="collections")] fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { read_to_end_with_reservation(r, buf, |_| 32) } +#[cfg(feature="collections")] fn read_to_end_with_reservation( r: &mut R, buf: &mut Vec, @@ -484,7 +477,6 @@ where /// [`std::io`]: self /// [`File`]: crate::fs::File /// [slice]: ../../std/primitive.slice.html -#[stable(feature = "rust1", since = "1.0.0")] #[doc(spotlight)] pub trait Read { /// Pull some bytes from this source into the specified buffer, returning @@ -555,7 +547,6 @@ pub trait Read { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn read(&mut self, buf: &mut [u8]) -> Result; /// Like `read`, except that it reads into a slice of buffers. @@ -567,7 +558,6 @@ pub trait Read { /// /// The default implementation calls `read` with either the first nonempty /// buffer provided, or an empty one if none exists. - #[stable(feature = "iovec", since = "1.36.0")] fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result { default_read_vectored(|b| self.read(b), bufs) } @@ -580,7 +570,6 @@ pub trait Read { /// and coalesce writes into a single buffer for higher performance. /// /// The default implementation returns `false`. - #[unstable(feature = "can_vector", issue = "69941")] fn is_read_vectored(&self) -> bool { false } @@ -604,7 +593,6 @@ pub trait Read { /// This method is unsafe because a `Read`er could otherwise return a /// non-zeroing `Initializer` from another `Read` type without an `unsafe` /// block. - #[unstable(feature = "read_initializer", issue = "42788")] #[inline] unsafe fn initializer(&self) -> Initializer { Initializer::zeroing() @@ -656,7 +644,7 @@ pub trait Read { /// file.) /// /// [`std::fs::read`]: crate::fs::read - #[stable(feature = "rust1", since = "1.0.0")] + #[cfg(feature="collections")] fn read_to_end(&mut self, buf: &mut Vec) -> Result { read_to_end(self, buf) } @@ -699,7 +687,7 @@ pub trait Read { /// reading from a file.) /// /// [`std::fs::read_to_string`]: crate::fs::read_to_string - #[stable(feature = "rust1", since = "1.0.0")] + #[cfg(feature="collections")] fn read_to_string(&mut self, buf: &mut String) -> Result { // Note that we do *not* call `.read_to_end()` here. We are passing // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` @@ -763,7 +751,6 @@ pub trait Read { /// Ok(()) /// } /// ``` - #[stable(feature = "read_exact", since = "1.6.0")] fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> { while !buf.is_empty() { match self.read(buf) { @@ -817,7 +804,6 @@ pub trait Read { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn by_ref(&mut self) -> &mut Self where Self: Sized, @@ -855,7 +841,6 @@ pub trait Read { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn bytes(self) -> Bytes where Self: Sized, @@ -893,7 +878,6 @@ pub trait Read { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn chain(self, next: R) -> Chain where Self: Sized, @@ -932,7 +916,6 @@ pub trait Read { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn take(self, limit: u64) -> Take where Self: Sized, @@ -941,22 +924,64 @@ pub trait Read { } } +#[derive(Copy, Clone)] +pub struct IoVecBuffer<'a>(&'a [u8]); + +impl<'a> IoVecBuffer<'a> { + #[inline] + pub fn new(buf: &'a [u8]) -> IoVecBuffer<'a> { + IoVecBuffer(buf) + } + + #[inline] + pub fn advance(&mut self, n: usize) { + self.0 = &self.0[n..] + } + + #[inline] + pub fn as_slice(&self) -> &[u8] { + self.0 + } +} + +pub struct IoVecMutBuffer<'a>(&'a mut [u8]); + +impl<'a> IoVecMutBuffer<'a> { + #[inline] + pub fn new(buf: &'a mut [u8]) -> IoVecMutBuffer<'a> { + IoVecMutBuffer(buf) + } + + #[inline] + pub fn advance(&mut self, n: usize) { + let slice = core::mem::replace(&mut self.0, &mut []); + let (_, remaining) = slice.split_at_mut(n); + self.0 = remaining; + } + + #[inline] + pub fn as_slice(&self) -> &[u8] { + self.0 + } + + #[inline] + pub fn as_mut_slice(&mut self) -> &mut [u8] { + self.0 + } +} + /// A buffer type used with `Read::read_vectored`. /// /// It is semantically a wrapper around an `&mut [u8]`, but is guaranteed to be /// ABI compatible with the `iovec` type on Unix platforms and `WSABUF` on /// Windows. -#[stable(feature = "iovec", since = "1.36.0")] #[repr(transparent)] -pub struct IoSliceMut<'a>(sys::io::IoSliceMut<'a>); +pub struct IoSliceMut<'a>(IoVecMutBuffer<'a>); -#[stable(feature = "iovec-send-sync", since = "1.44.0")] unsafe impl<'a> Send for IoSliceMut<'a> {} -#[stable(feature = "iovec-send-sync", since = "1.44.0")] 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) @@ -969,10 +994,9 @@ impl<'a> IoSliceMut<'a> { /// # Panics /// /// Panics on Windows if the slice is larger than 4GB. - #[stable(feature = "iovec", since = "1.36.0")] #[inline] pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> { - IoSliceMut(sys::io::IoSliceMut::new(buf)) + IoSliceMut(IoVecMutBuffer::new(buf)) } /// Advance the internal cursor of the slice. @@ -1007,7 +1031,6 @@ impl<'a> IoSliceMut<'a> { /// assert_eq!(bufs[0].deref(), [2; 14].as_ref()); /// assert_eq!(bufs[1].deref(), [3; 8].as_ref()); /// ``` - #[unstable(feature = "io_slice_advance", issue = "62726")] #[inline] pub fn advance<'b>(bufs: &'b mut [IoSliceMut<'a>], n: usize) -> &'b mut [IoSliceMut<'a>] { // Number of buffers to remove. @@ -1031,7 +1054,6 @@ impl<'a> IoSliceMut<'a> { } } -#[stable(feature = "iovec", since = "1.36.0")] impl<'a> Deref for IoSliceMut<'a> { type Target = [u8]; @@ -1041,7 +1063,6 @@ impl<'a> Deref for IoSliceMut<'a> { } } -#[stable(feature = "iovec", since = "1.36.0")] impl<'a> DerefMut for IoSliceMut<'a> { #[inline] fn deref_mut(&mut self) -> &mut [u8] { @@ -1054,18 +1075,14 @@ impl<'a> DerefMut for IoSliceMut<'a> { /// It is semantically a wrapper around an `&[u8]`, but is guaranteed to be /// ABI compatible with the `iovec` type on Unix platforms and `WSABUF` on /// Windows. -#[stable(feature = "iovec", since = "1.36.0")] #[derive(Copy, Clone)] #[repr(transparent)] -pub struct IoSlice<'a>(sys::io::IoSlice<'a>); +pub struct IoSlice<'a>(IoVecBuffer<'a>); -#[stable(feature = "iovec-send-sync", since = "1.44.0")] unsafe impl<'a> Send for IoSlice<'a> {} -#[stable(feature = "iovec-send-sync", since = "1.44.0")] 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) @@ -1078,10 +1095,9 @@ impl<'a> IoSlice<'a> { /// # Panics /// /// Panics on Windows if the slice is larger than 4GB. - #[stable(feature = "iovec", since = "1.36.0")] #[inline] pub fn new(buf: &'a [u8]) -> IoSlice<'a> { - IoSlice(sys::io::IoSlice::new(buf)) + IoSlice(IoVecBuffer::new(buf)) } /// Advance the internal cursor of the slice. @@ -1115,7 +1131,6 @@ impl<'a> IoSlice<'a> { /// bufs = IoSlice::advance(bufs, 10); /// assert_eq!(bufs[0].deref(), [2; 14].as_ref()); /// assert_eq!(bufs[1].deref(), [3; 8].as_ref()); - #[unstable(feature = "io_slice_advance", issue = "62726")] #[inline] pub fn advance<'b>(bufs: &'b mut [IoSlice<'a>], n: usize) -> &'b mut [IoSlice<'a>] { // Number of buffers to remove. @@ -1139,7 +1154,6 @@ impl<'a> IoSlice<'a> { } } -#[stable(feature = "iovec", since = "1.36.0")] impl<'a> Deref for IoSlice<'a> { type Target = [u8]; @@ -1150,13 +1164,11 @@ impl<'a> Deref for IoSlice<'a> { } /// A type used to conditionally initialize buffers passed to `Read` methods. -#[unstable(feature = "read_initializer", issue = "42788")] #[derive(Debug)] pub struct Initializer(bool); impl Initializer { /// Returns a new `Initializer` which will zero out buffers. - #[unstable(feature = "read_initializer", issue = "42788")] #[inline] pub fn zeroing() -> Initializer { Initializer(true) @@ -1170,21 +1182,18 @@ impl Initializer { /// read from buffers passed to `Read` methods, and that the return value of /// the method accurately reflects the number of bytes that have been /// written to the head of the buffer. - #[unstable(feature = "read_initializer", issue = "42788")] #[inline] pub unsafe fn nop() -> Initializer { Initializer(false) } /// Indicates if a buffer should be initialized. - #[unstable(feature = "read_initializer", issue = "42788")] #[inline] pub fn should_initialize(&self) -> bool { self.0 } /// Initializes a buffer if necessary. - #[unstable(feature = "read_initializer", issue = "42788")] #[inline] pub fn initialize(&self, buf: &mut [u8]) { if self.should_initialize() { @@ -1238,7 +1247,6 @@ impl Initializer { /// `write` in a loop until its entire input has been written. /// /// [`write_all`]: Self::write_all -#[stable(feature = "rust1", since = "1.0.0")] #[doc(spotlight)] pub trait Write { /// Write a buffer into this writer, returning how many bytes were written. @@ -1283,7 +1291,6 @@ pub trait Write { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn write(&mut self, buf: &[u8]) -> Result; /// Like `write`, except that it writes from a slice of buffers. @@ -1294,7 +1301,6 @@ pub trait Write { /// /// The default implementation calls `write` with either the first nonempty /// buffer provided, or an empty one if none exists. - #[stable(feature = "iovec", since = "1.36.0")] fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result { default_write_vectored(|b| self.write(b), bufs) } @@ -1307,7 +1313,6 @@ pub trait Write { /// and coalesce writes into a single buffer for higher performance. /// /// The default implementation returns `false`. - #[unstable(feature = "can_vector", issue = "69941")] fn is_write_vectored(&self) -> bool { false } @@ -1335,7 +1340,6 @@ pub trait Write { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn flush(&mut self) -> Result<()>; /// Attempts to write an entire buffer into this writer. @@ -1369,7 +1373,6 @@ pub trait Write { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn write_all(&mut self, mut buf: &[u8]) -> Result<()> { while !buf.is_empty() { match self.write(buf) { @@ -1432,7 +1435,6 @@ pub trait Write { /// assert_eq!(writer, &[1, 2, 3, 4, 5, 6]); /// # Ok(()) } /// ``` - #[unstable(feature = "write_all_vectored", issue = "70436")] fn write_all_vectored(&mut self, mut bufs: &mut [IoSlice<'_>]) -> Result<()> { // Guarantee that bufs is empty if it contains no data, // to avoid calling write_vectored if there is no data to be written. @@ -1485,7 +1487,6 @@ pub trait Write { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> { // Create a shim which translates a Write to a fmt::Write and saves // off I/O errors. instead of discarding them @@ -1541,7 +1542,6 @@ pub trait Write { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn by_ref(&mut self) -> &mut Self where Self: Sized, @@ -1576,7 +1576,6 @@ pub trait Write { /// Ok(()) /// } /// ``` -#[stable(feature = "rust1", since = "1.0.0")] pub trait Seek { /// Seek to an offset, in bytes, in a stream. /// @@ -1590,7 +1589,6 @@ pub trait Seek { /// # Errors /// /// Seeking to a negative offset is considered an error. - #[stable(feature = "rust1", since = "1.0.0")] fn seek(&mut self, pos: SeekFrom) -> Result; /// Returns the length of this stream (in bytes). @@ -1628,7 +1626,6 @@ pub trait Seek { /// Ok(()) /// } /// ``` - #[unstable(feature = "seek_convenience", issue = "59359")] fn stream_len(&mut self) -> Result { let old_pos = self.stream_position()?; let len = self.seek(SeekFrom::End(0))?; @@ -1667,7 +1664,6 @@ pub trait Seek { /// Ok(()) /// } /// ``` - #[unstable(feature = "seek_convenience", issue = "59359")] fn stream_position(&mut self) -> Result { self.seek(SeekFrom::Current(0)) } @@ -1677,29 +1673,26 @@ pub trait Seek { /// /// It is used by the [`Seek`] trait. #[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[stable(feature = "rust1", since = "1.0.0")] pub enum SeekFrom { /// Sets the offset to the provided number of bytes. - #[stable(feature = "rust1", since = "1.0.0")] - Start(#[stable(feature = "rust1", since = "1.0.0")] u64), + Start(u64), /// Sets the offset to the size of this object plus the specified number of /// bytes. /// /// It is possible to seek beyond the end of an object, but it's an error to /// seek before byte 0. - #[stable(feature = "rust1", since = "1.0.0")] - End(#[stable(feature = "rust1", since = "1.0.0")] i64), + End(i64), /// Sets the offset to the current position plus the specified number of /// bytes. /// /// It is possible to seek beyond the end of an object, but it's an error to /// seek before byte 0. - #[stable(feature = "rust1", since = "1.0.0")] - Current(#[stable(feature = "rust1", since = "1.0.0")] i64), + Current(i64), } +#[cfg(feature="collections")] fn read_until(r: &mut R, delim: u8, buf: &mut Vec) -> Result { let mut read = 0; loop { @@ -1776,7 +1769,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) -> R /// } /// ``` /// -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] pub trait BufRead: Read { /// Returns the contents of the internal buffer, filling it with more data /// from the inner reader if it is empty. @@ -1817,7 +1810,6 @@ pub trait BufRead: Read { /// let length = buffer.len(); /// stdin.consume(length); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn fill_buf(&mut self) -> Result<&[u8]>; /// Tells this buffer that `amt` bytes have been consumed from the buffer, @@ -1839,7 +1831,6 @@ pub trait BufRead: Read { /// that method's example includes an example of `consume()`. /// /// [`fill_buf`]: Self::fill_buf - #[stable(feature = "rust1", since = "1.0.0")] fn consume(&mut self, amt: usize); /// Read all bytes into `buf` until the delimiter `byte` or EOF is reached. @@ -1896,7 +1887,6 @@ pub trait BufRead: Read { /// assert_eq!(num_bytes, 0); /// assert_eq!(buf, b""); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { read_until(self, byte, buf) } @@ -1957,7 +1947,6 @@ pub trait BufRead: Read { /// assert_eq!(num_bytes, 0); /// assert_eq!(buf, ""); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn read_line(&mut self, buf: &mut String) -> Result { // Note that we are not calling the `.read_until` method here, but // rather our hardcoded implementation. For more details as to why, see @@ -1996,7 +1985,6 @@ pub trait BufRead: Read { /// assert_eq!(split_iter.next(), Some(b"dolor".to_vec())); /// assert_eq!(split_iter.next(), None); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] fn split(self, byte: u8) -> Split where Self: Sized, @@ -2033,7 +2021,6 @@ pub trait BufRead: Read { /// # Errors /// /// Each line of the iterator has the same error semantics as [`BufRead::read_line`]. - #[stable(feature = "rust1", since = "1.0.0")] fn lines(self) -> Lines where Self: Sized, @@ -2048,7 +2035,6 @@ pub trait BufRead: Read { /// Please see the documentation of [`chain`] for more details. /// /// [`chain`]: Read::chain -#[stable(feature = "rust1", since = "1.0.0")] pub struct Chain { first: T, second: U, @@ -2074,7 +2060,6 @@ impl Chain { /// Ok(()) /// } /// ``` - #[stable(feature = "more_io_inner_methods", since = "1.20.0")] pub fn into_inner(self) -> (T, U) { (self.first, self.second) } @@ -2097,7 +2082,6 @@ impl Chain { /// Ok(()) /// } /// ``` - #[stable(feature = "more_io_inner_methods", since = "1.20.0")] pub fn get_ref(&self) -> (&T, &U) { (&self.first, &self.second) } @@ -2124,20 +2108,17 @@ impl Chain { /// Ok(()) /// } /// ``` - #[stable(feature = "more_io_inner_methods", since = "1.20.0")] pub fn get_mut(&mut self) -> (&mut T, &mut U) { (&mut self.first, &mut self.second) } } -#[stable(feature = "std_debug", since = "1.16.0")] impl fmt::Debug for Chain { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Chain").field("t", &self.first).field("u", &self.second).finish() } } -#[stable(feature = "rust1", since = "1.0.0")] impl Read for Chain { fn read(&mut self, buf: &mut [u8]) -> Result { if !self.done_first { @@ -2165,7 +2146,7 @@ impl Read for Chain { } } -#[stable(feature = "chain_bufread", since = "1.9.0")] +#[cfg(feature="collections")] impl BufRead for Chain { fn fill_buf(&mut self) -> Result<&[u8]> { if !self.done_first { @@ -2190,7 +2171,6 @@ impl BufRead for Chain { /// Please see the documentation of [`take`] for more details. /// /// [`take`]: Read::take -#[stable(feature = "rust1", since = "1.0.0")] #[derive(Debug)] pub struct Take { inner: T, @@ -2223,7 +2203,6 @@ impl Take { /// Ok(()) /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub fn limit(&self) -> u64 { self.limit } @@ -2251,7 +2230,6 @@ impl Take { /// Ok(()) /// } /// ``` - #[stable(feature = "take_set_limit", since = "1.27.0")] pub fn set_limit(&mut self, limit: u64) { self.limit = limit; } @@ -2276,7 +2254,6 @@ impl Take { /// Ok(()) /// } /// ``` - #[stable(feature = "io_take_into_inner", since = "1.15.0")] pub fn into_inner(self) -> T { self.inner } @@ -2301,7 +2278,6 @@ impl Take { /// Ok(()) /// } /// ``` - #[stable(feature = "more_io_inner_methods", since = "1.20.0")] pub fn get_ref(&self) -> &T { &self.inner } @@ -2330,13 +2306,11 @@ impl Take { /// Ok(()) /// } /// ``` - #[stable(feature = "more_io_inner_methods", since = "1.20.0")] pub fn get_mut(&mut self) -> &mut T { &mut self.inner } } -#[stable(feature = "rust1", since = "1.0.0")] impl Read for Take { fn read(&mut self, buf: &mut [u8]) -> Result { // Don't call into inner reader at all at EOF because it may still block @@ -2354,6 +2328,7 @@ impl Read for Take { self.inner.initializer() } + #[cfg(feature="collections")] fn read_to_end(&mut self, buf: &mut Vec) -> Result { // Pass in a reservation_size closure that respects the current value // of limit for each read. If we hit the read limit, this prevents the @@ -2362,7 +2337,7 @@ impl Read for Take { } } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] impl BufRead for Take { fn fill_buf(&mut self) -> Result<&[u8]> { // Don't call into inner reader at all at EOF because it may still block @@ -2389,13 +2364,11 @@ impl BufRead for Take { /// Please see the documentation of [`bytes`] for more details. /// /// [`bytes`]: Read::bytes -#[stable(feature = "rust1", since = "1.0.0")] #[derive(Debug)] pub struct Bytes { inner: R, } -#[stable(feature = "rust1", since = "1.0.0")] impl Iterator for Bytes { type Item = Result; @@ -2419,14 +2392,14 @@ impl Iterator for Bytes { /// Please see the documentation of [`split`] for more details. /// /// [`split`]: BufRead::split -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] #[derive(Debug)] pub struct Split { buf: B, delim: u8, } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] impl Iterator for Split { type Item = Result>; @@ -2451,13 +2424,13 @@ impl Iterator for Split { /// Please see the documentation of [`lines`] for more details. /// /// [`lines`]: BufRead::lines -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] #[derive(Debug)] pub struct Lines { buf: B, } -#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(feature="collections")] impl Iterator for Lines { type Item = Result; diff --git a/prelude.rs b/prelude.rs index 3baab2b..7d96d23 100644 --- a/prelude.rs +++ b/prelude.rs @@ -8,7 +8,8 @@ //! use std::io::prelude::*; //! ``` -#![stable(feature = "rust1", since = "1.0.0")] +pub use super::{Read, Seek, Write}; +#[cfg(feature="collections")] pub use super::BufRead; -#[stable(feature = "rust1", since = "1.0.0")] -pub use super::{BufRead, Read, Seek, Write}; +#[cfg(feature="collections")] pub use alloc::boxed::Box; +#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/util.rs b/util.rs index b9d5dc2..07d090e 100644 --- a/util.rs +++ b/util.rs @@ -1,8 +1,9 @@ #![allow(missing_copy_implementations)] -use crate::fmt; -use crate::io::{self, BufRead, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Write}; -use crate::mem::MaybeUninit; +use core::fmt; +use crate::io::{self, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Write}; +#[cfg(feature="collections")] use crate::io::BufRead; +use core::mem::MaybeUninit; /// Copies the entire contents of a reader into a writer. /// @@ -39,7 +40,6 @@ use crate::mem::MaybeUninit; /// Ok(()) /// } /// ``` -#[stable(feature = "rust1", since = "1.0.0")] pub fn copy(reader: &mut R, writer: &mut W) -> io::Result where R: Read, @@ -74,7 +74,6 @@ where /// the documentation of [`empty()`][`empty`] for more details. /// /// [`empty`]: fn.empty.html -#[stable(feature = "rust1", since = "1.0.0")] pub struct Empty { _priv: (), } @@ -96,12 +95,10 @@ pub struct Empty { /// io::empty().read_to_string(&mut buffer).unwrap(); /// assert!(buffer.is_empty()); /// ``` -#[stable(feature = "rust1", since = "1.0.0")] pub fn empty() -> Empty { Empty { _priv: () } } -#[stable(feature = "rust1", since = "1.0.0")] impl Read for Empty { #[inline] fn read(&mut self, _buf: &mut [u8]) -> io::Result { @@ -113,7 +110,8 @@ impl Read for Empty { Initializer::nop() } } -#[stable(feature = "rust1", since = "1.0.0")] + +#[cfg(feature="collections")] impl BufRead for Empty { #[inline] fn fill_buf(&mut self) -> io::Result<&[u8]> { @@ -123,7 +121,6 @@ impl BufRead for Empty { fn consume(&mut self, _n: usize) {} } -#[stable(feature = "std_debug", since = "1.16.0")] impl fmt::Debug for Empty { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.pad("Empty { .. }") @@ -136,7 +133,6 @@ impl fmt::Debug for Empty { /// see the documentation of `repeat()` for more details. /// /// [repeat]: fn.repeat.html -#[stable(feature = "rust1", since = "1.0.0")] pub struct Repeat { byte: u8, } @@ -155,12 +151,10 @@ pub struct Repeat { /// io::repeat(0b101).read_exact(&mut buffer).unwrap(); /// assert_eq!(buffer, [0b101, 0b101, 0b101]); /// ``` -#[stable(feature = "rust1", since = "1.0.0")] pub fn repeat(byte: u8) -> Repeat { Repeat { byte } } -#[stable(feature = "rust1", since = "1.0.0")] impl Read for Repeat { #[inline] fn read(&mut self, buf: &mut [u8]) -> io::Result { @@ -190,7 +184,6 @@ impl Read for Repeat { } } -#[stable(feature = "std_debug", since = "1.16.0")] impl fmt::Debug for Repeat { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.pad("Repeat { .. }") @@ -203,7 +196,6 @@ impl fmt::Debug for Repeat { /// see the documentation of `sink()` for more details. /// /// [sink]: fn.sink.html -#[stable(feature = "rust1", since = "1.0.0")] pub struct Sink { _priv: (), } @@ -222,12 +214,10 @@ pub struct Sink { /// let num_bytes = io::sink().write(&buffer).unwrap(); /// assert_eq!(num_bytes, 5); /// ``` -#[stable(feature = "rust1", since = "1.0.0")] pub fn sink() -> Sink { Sink { _priv: () } } -#[stable(feature = "rust1", since = "1.0.0")] impl Write for Sink { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { @@ -251,7 +241,6 @@ impl Write for Sink { } } -#[stable(feature = "std_debug", since = "1.16.0")] impl fmt::Debug for Sink { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.pad("Sink { .. }")