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

Feature request, move part of std::io into core::io to be used by rust-libc such as relibc #77781

Closed
wants to merge 1 commit 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
76 changes: 17 additions & 59 deletions library/std/src/io/buffered.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
//! Buffering wrappers for I/O traits

#[cfg(test)]
mod tests;
//! Buffering wrappers for I/O traits

use crate::io::prelude::*;
use core::prelude::v1::*;
use io::prelude::*;

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

/// The `BufReader<R>` struct adds buffering to any reader.
///
Expand Down Expand Up @@ -52,7 +48,6 @@ use crate::memchr;
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub struct BufReader<R> {
inner: R,
buf: Box<[u8]>,
Expand All @@ -76,7 +71,6 @@ impl<R: Read> BufReader<R> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(inner: R) -> BufReader<R> {
BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
}
Expand All @@ -97,7 +91,6 @@ impl<R: Read> BufReader<R> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
unsafe {
let mut buffer = Vec::with_capacity(capacity);
Expand Down Expand Up @@ -127,7 +120,6 @@ impl<R> BufReader<R> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_ref(&self) -> &R {
&self.inner
}
Expand All @@ -150,7 +142,6 @@ impl<R> BufReader<R> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
Expand Down Expand Up @@ -178,7 +169,6 @@ impl<R> BufReader<R> {
/// Ok(())
/// }
/// ```
#[stable(feature = "bufreader_buffer", since = "1.37.0")]
pub fn buffer(&self) -> &[u8] {
&self.buf[self.pos..self.cap]
}
Expand All @@ -201,7 +191,6 @@ impl<R> BufReader<R> {
/// Ok(())
/// }
/// ```
#[stable(feature = "buffered_io_capacity", since = "1.46.0")]
pub fn capacity(&self) -> usize {
self.buf.len()
}
Expand All @@ -225,7 +214,6 @@ impl<R> BufReader<R> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_inner(self) -> R {
self.inner
}
Expand All @@ -243,7 +231,6 @@ impl<R: Seek> BufReader<R> {
/// 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 {
Expand All @@ -263,7 +250,6 @@ impl<R: Seek> BufReader<R> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<R: Read> Read for BufReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
// If we don't have any buffered data and we're doing a massive read
Expand Down Expand Up @@ -305,7 +291,6 @@ impl<R: Read> Read for BufReader<R> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<R: Read> BufRead for BufReader<R> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
// If we've reached the end of our internal buffer then we need to fetch
Expand All @@ -325,7 +310,6 @@ impl<R: Read> BufRead for BufReader<R> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<R> fmt::Debug for BufReader<R>
where
R: fmt::Debug,
Expand All @@ -338,7 +322,6 @@ where
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<R: Seek> Seek for BufReader<R> {
/// Seek to an offset, in bytes, in the underlying reader.
///
Expand Down Expand Up @@ -492,10 +475,9 @@ impl<R: Seek> Seek for BufReader<R> {
/// [`TcpStream::write`]: Write::write
/// [`TcpStream`]: crate::net::TcpStream
/// [`flush`]: Write::flush
#[stable(feature = "rust1", since = "1.0.0")]
pub struct BufWriter<W: Write> {
inner: Option<W>,
buf: Vec<u8>,
pub buf: Vec<u8>,
// #30888: If the inner writer panics in a call to write, we don't want to
// write the buffered data a second time in BufWriter's destructor. This
// flag tells the Drop impl if it should skip the flush.
Expand Down Expand Up @@ -527,7 +509,6 @@ pub struct BufWriter<W: Write> {
/// };
/// ```
#[derive(Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoInnerError<W>(W, Error);

impl<W: Write> BufWriter<W> {
Expand All @@ -542,7 +523,6 @@ impl<W: Write> BufWriter<W> {
///
/// 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<W> {
BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
}
Expand All @@ -560,7 +540,6 @@ impl<W: Write> BufWriter<W> {
/// 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<W> {
BufWriter { inner: Some(inner), buf: Vec::with_capacity(capacity), panicked: false }
}
Expand Down Expand Up @@ -632,6 +611,10 @@ impl<W: Write> BufWriter<W> {
Ok(())
}

pub fn purge_buf(&mut self) {
self.buf = vec![];
}

/// Buffer some data without flushing it, regardless of the size of the
/// data. Writes as much as possible without exceeding capacity. Returns
/// the number of bytes written.
Expand All @@ -655,7 +638,6 @@ impl<W: Write> BufWriter<W> {
/// // 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()
}
Expand All @@ -675,7 +657,6 @@ impl<W: Write> BufWriter<W> {
/// // 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()
}
Expand All @@ -693,7 +674,6 @@ impl<W: Write> BufWriter<W> {
/// // 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
}
Expand All @@ -713,7 +693,6 @@ impl<W: Write> BufWriter<W> {
/// // 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()
}
Expand All @@ -737,7 +716,6 @@ impl<W: Write> BufWriter<W> {
/// // 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<W, IntoInnerError<BufWriter<W>>> {
match self.flush_buf() {
Err(e) => Err(IntoInnerError(self, e)),
Expand All @@ -746,7 +724,6 @@ impl<W: Write> BufWriter<W> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write> Write for BufWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.buf.len() + buf.len() > self.buf.capacity() {
Expand Down Expand Up @@ -810,7 +787,6 @@ impl<W: Write> Write for BufWriter<W> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write> fmt::Debug for BufWriter<W>
where
W: fmt::Debug,
Expand All @@ -823,7 +799,6 @@ where
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write + Seek> Seek for BufWriter<W> {
/// Seek to the offset, in bytes, in the underlying writer.
///
Expand All @@ -834,7 +809,6 @@ impl<W: Write + Seek> Seek for BufWriter<W> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write> Drop for BufWriter<W> {
fn drop(&mut self) {
if self.inner.is_some() && !self.panicked {
Expand Down Expand Up @@ -874,7 +848,6 @@ impl<W> IntoInnerError<W> {
/// }
/// };
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn error(&self) -> &Error {
&self.1
}
Expand Down Expand Up @@ -909,28 +882,17 @@ impl<W> IntoInnerError<W> {
/// }
/// };
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_inner(self) -> W {
self.0
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W> From<IntoInnerError<W>> for Error {
fn from(iie: IntoInnerError<W>) -> Error {
iie.1
}
}

#[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 {
self.error().fmt(f)
Expand Down Expand Up @@ -1267,9 +1229,8 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub struct LineWriter<W: Write> {
inner: BufWriter<W>,
pub inner: BufWriter<W>,
}

impl<W: Write> LineWriter<W> {
Expand All @@ -1287,7 +1248,6 @@ impl<W: Write> LineWriter<W> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(inner: W) -> LineWriter<W> {
// Lines typically aren't that long, don't use a giant buffer
LineWriter::with_capacity(1024, inner)
Expand All @@ -1308,7 +1268,6 @@ impl<W: Write> LineWriter<W> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
LineWriter { inner: BufWriter::with_capacity(capacity, inner) }
}
Expand All @@ -1329,7 +1288,6 @@ impl<W: Write> LineWriter<W> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_ref(&self) -> &W {
self.inner.get_ref()
}
Expand All @@ -1354,7 +1312,6 @@ impl<W: Write> LineWriter<W> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut(&mut self) -> &mut W {
self.inner.get_mut()
}
Expand Down Expand Up @@ -1382,15 +1339,17 @@ impl<W: Write> LineWriter<W> {
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
self.inner
.into_inner()
.map_err(|IntoInnerError(buf, e)| IntoInnerError(LineWriter { inner: buf }, e))
}

pub fn purge(&mut self) {
self.inner.purge_buf();
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write> Write for LineWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
LineWriterShim::new(&mut self.inner).write(buf)
Expand Down Expand Up @@ -1421,7 +1380,6 @@ impl<W: Write> Write for LineWriter<W> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write> fmt::Debug for LineWriter<W>
where
W: fmt::Debug,
Expand Down
Loading