Skip to content

Commit 4765c51

Browse files
committed
Feature request, move part of std::io into core::io to be used by rust-libc such as relibc
Get libstd io to be portable. And makes platform dependent IoSliceMut can be customized by the user And give the default impl for windows/posix.
1 parent 6b8d791 commit 4765c51

File tree

8 files changed

+204
-368
lines changed

8 files changed

+204
-368
lines changed

library/std/src/io/buffered.rs

+17-59
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
//! Buffering wrappers for I/O traits
21

3-
#[cfg(test)]
4-
mod tests;
2+
//! Buffering wrappers for I/O traits
53
6-
use crate::io::prelude::*;
4+
use core::prelude::v1::*;
5+
use io::prelude::*;
76

8-
use crate::cmp;
9-
use crate::error;
10-
use crate::fmt;
11-
use crate::io::{
12-
self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, SeekFrom, DEFAULT_BUF_SIZE,
13-
};
14-
use crate::memchr;
7+
use core::cmp;
8+
use core::fmt;
9+
use io::{self, Initializer, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom, IoSlice, IoSliceMut};
10+
use io::memchr;
1511

1612
/// The `BufReader<R>` struct adds buffering to any reader.
1713
///
@@ -52,7 +48,6 @@ use crate::memchr;
5248
/// Ok(())
5349
/// }
5450
/// ```
55-
#[stable(feature = "rust1", since = "1.0.0")]
5651
pub struct BufReader<R> {
5752
inner: R,
5853
buf: Box<[u8]>,
@@ -76,7 +71,6 @@ impl<R: Read> BufReader<R> {
7671
/// Ok(())
7772
/// }
7873
/// ```
79-
#[stable(feature = "rust1", since = "1.0.0")]
8074
pub fn new(inner: R) -> BufReader<R> {
8175
BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
8276
}
@@ -97,7 +91,6 @@ impl<R: Read> BufReader<R> {
9791
/// Ok(())
9892
/// }
9993
/// ```
100-
#[stable(feature = "rust1", since = "1.0.0")]
10194
pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
10295
unsafe {
10396
let mut buffer = Vec::with_capacity(capacity);
@@ -127,7 +120,6 @@ impl<R> BufReader<R> {
127120
/// Ok(())
128121
/// }
129122
/// ```
130-
#[stable(feature = "rust1", since = "1.0.0")]
131123
pub fn get_ref(&self) -> &R {
132124
&self.inner
133125
}
@@ -150,7 +142,6 @@ impl<R> BufReader<R> {
150142
/// Ok(())
151143
/// }
152144
/// ```
153-
#[stable(feature = "rust1", since = "1.0.0")]
154145
pub fn get_mut(&mut self) -> &mut R {
155146
&mut self.inner
156147
}
@@ -178,7 +169,6 @@ impl<R> BufReader<R> {
178169
/// Ok(())
179170
/// }
180171
/// ```
181-
#[stable(feature = "bufreader_buffer", since = "1.37.0")]
182172
pub fn buffer(&self) -> &[u8] {
183173
&self.buf[self.pos..self.cap]
184174
}
@@ -201,7 +191,6 @@ impl<R> BufReader<R> {
201191
/// Ok(())
202192
/// }
203193
/// ```
204-
#[stable(feature = "buffered_io_capacity", since = "1.46.0")]
205194
pub fn capacity(&self) -> usize {
206195
self.buf.len()
207196
}
@@ -225,7 +214,6 @@ impl<R> BufReader<R> {
225214
/// Ok(())
226215
/// }
227216
/// ```
228-
#[stable(feature = "rust1", since = "1.0.0")]
229217
pub fn into_inner(self) -> R {
230218
self.inner
231219
}
@@ -243,7 +231,6 @@ impl<R: Seek> BufReader<R> {
243231
/// the buffer will not be flushed, allowing for more efficient seeks.
244232
/// This method does not return the location of the underlying reader, so the caller
245233
/// must track this information themselves if it is required.
246-
#[unstable(feature = "bufreader_seek_relative", issue = "31100")]
247234
pub fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
248235
let pos = self.pos as u64;
249236
if offset < 0 {
@@ -263,7 +250,6 @@ impl<R: Seek> BufReader<R> {
263250
}
264251
}
265252

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

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

328-
#[stable(feature = "rust1", since = "1.0.0")]
329313
impl<R> fmt::Debug for BufReader<R>
330314
where
331315
R: fmt::Debug,
@@ -338,7 +322,6 @@ where
338322
}
339323
}
340324

341-
#[stable(feature = "rust1", since = "1.0.0")]
342325
impl<R: Seek> Seek for BufReader<R> {
343326
/// Seek to an offset, in bytes, in the underlying reader.
344327
///
@@ -492,10 +475,9 @@ impl<R: Seek> Seek for BufReader<R> {
492475
/// [`TcpStream::write`]: Write::write
493476
/// [`TcpStream`]: crate::net::TcpStream
494477
/// [`flush`]: Write::flush
495-
#[stable(feature = "rust1", since = "1.0.0")]
496478
pub struct BufWriter<W: Write> {
497479
inner: Option<W>,
498-
buf: Vec<u8>,
480+
pub buf: Vec<u8>,
499481
// #30888: If the inner writer panics in a call to write, we don't want to
500482
// write the buffered data a second time in BufWriter's destructor. This
501483
// flag tells the Drop impl if it should skip the flush.
@@ -527,7 +509,6 @@ pub struct BufWriter<W: Write> {
527509
/// };
528510
/// ```
529511
#[derive(Debug)]
530-
#[stable(feature = "rust1", since = "1.0.0")]
531512
pub struct IntoInnerError<W>(W, Error);
532513

533514
impl<W: Write> BufWriter<W> {
@@ -542,7 +523,6 @@ impl<W: Write> BufWriter<W> {
542523
///
543524
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
544525
/// ```
545-
#[stable(feature = "rust1", since = "1.0.0")]
546526
pub fn new(inner: W) -> BufWriter<W> {
547527
BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
548528
}
@@ -560,7 +540,6 @@ impl<W: Write> BufWriter<W> {
560540
/// let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
561541
/// let mut buffer = BufWriter::with_capacity(100, stream);
562542
/// ```
563-
#[stable(feature = "rust1", since = "1.0.0")]
564543
pub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W> {
565544
BufWriter { inner: Some(inner), buf: Vec::with_capacity(capacity), panicked: false }
566545
}
@@ -632,6 +611,10 @@ impl<W: Write> BufWriter<W> {
632611
Ok(())
633612
}
634613

614+
pub fn purge_buf(&mut self) {
615+
self.buf = vec![];
616+
}
617+
635618
/// Buffer some data without flushing it, regardless of the size of the
636619
/// data. Writes as much as possible without exceeding capacity. Returns
637620
/// the number of bytes written.
@@ -655,7 +638,6 @@ impl<W: Write> BufWriter<W> {
655638
/// // we can use reference just like buffer
656639
/// let reference = buffer.get_ref();
657640
/// ```
658-
#[stable(feature = "rust1", since = "1.0.0")]
659641
pub fn get_ref(&self) -> &W {
660642
self.inner.as_ref().unwrap()
661643
}
@@ -675,7 +657,6 @@ impl<W: Write> BufWriter<W> {
675657
/// // we can use reference just like buffer
676658
/// let reference = buffer.get_mut();
677659
/// ```
678-
#[stable(feature = "rust1", since = "1.0.0")]
679660
pub fn get_mut(&mut self) -> &mut W {
680661
self.inner.as_mut().unwrap()
681662
}
@@ -693,7 +674,6 @@ impl<W: Write> BufWriter<W> {
693674
/// // See how many bytes are currently buffered
694675
/// let bytes_buffered = buf_writer.buffer().len();
695676
/// ```
696-
#[stable(feature = "bufreader_buffer", since = "1.37.0")]
697677
pub fn buffer(&self) -> &[u8] {
698678
&self.buf
699679
}
@@ -713,7 +693,6 @@ impl<W: Write> BufWriter<W> {
713693
/// // Calculate how many bytes can be written without flushing
714694
/// let without_flush = capacity - buf_writer.buffer().len();
715695
/// ```
716-
#[stable(feature = "buffered_io_capacity", since = "1.46.0")]
717696
pub fn capacity(&self) -> usize {
718697
self.buf.capacity()
719698
}
@@ -737,7 +716,6 @@ impl<W: Write> BufWriter<W> {
737716
/// // unwrap the TcpStream and flush the buffer
738717
/// let stream = buffer.into_inner().unwrap();
739718
/// ```
740-
#[stable(feature = "rust1", since = "1.0.0")]
741719
pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
742720
match self.flush_buf() {
743721
Err(e) => Err(IntoInnerError(self, e)),
@@ -746,7 +724,6 @@ impl<W: Write> BufWriter<W> {
746724
}
747725
}
748726

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

813-
#[stable(feature = "rust1", since = "1.0.0")]
814790
impl<W: Write> fmt::Debug for BufWriter<W>
815791
where
816792
W: fmt::Debug,
@@ -823,7 +799,6 @@ where
823799
}
824800
}
825801

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

837-
#[stable(feature = "rust1", since = "1.0.0")]
838812
impl<W: Write> Drop for BufWriter<W> {
839813
fn drop(&mut self) {
840814
if self.inner.is_some() && !self.panicked {
@@ -874,7 +848,6 @@ impl<W> IntoInnerError<W> {
874848
/// }
875849
/// };
876850
/// ```
877-
#[stable(feature = "rust1", since = "1.0.0")]
878851
pub fn error(&self) -> &Error {
879852
&self.1
880853
}
@@ -909,28 +882,17 @@ impl<W> IntoInnerError<W> {
909882
/// }
910883
/// };
911884
/// ```
912-
#[stable(feature = "rust1", since = "1.0.0")]
913885
pub fn into_inner(self) -> W {
914886
self.0
915887
}
916888
}
917889

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

925-
#[stable(feature = "rust1", since = "1.0.0")]
926-
impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
927-
#[allow(deprecated, deprecated_in_future)]
928-
fn description(&self) -> &str {
929-
error::Error::description(self.error())
930-
}
931-
}
932-
933-
#[stable(feature = "rust1", since = "1.0.0")]
934896
impl<W> fmt::Display for IntoInnerError<W> {
935897
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
936898
self.error().fmt(f)
@@ -1267,9 +1229,8 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
12671229
/// Ok(())
12681230
/// }
12691231
/// ```
1270-
#[stable(feature = "rust1", since = "1.0.0")]
12711232
pub struct LineWriter<W: Write> {
1272-
inner: BufWriter<W>,
1233+
pub inner: BufWriter<W>,
12731234
}
12741235

12751236
impl<W: Write> LineWriter<W> {
@@ -1287,7 +1248,6 @@ impl<W: Write> LineWriter<W> {
12871248
/// Ok(())
12881249
/// }
12891250
/// ```
1290-
#[stable(feature = "rust1", since = "1.0.0")]
12911251
pub fn new(inner: W) -> LineWriter<W> {
12921252
// Lines typically aren't that long, don't use a giant buffer
12931253
LineWriter::with_capacity(1024, inner)
@@ -1308,7 +1268,6 @@ impl<W: Write> LineWriter<W> {
13081268
/// Ok(())
13091269
/// }
13101270
/// ```
1311-
#[stable(feature = "rust1", since = "1.0.0")]
13121271
pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
13131272
LineWriter { inner: BufWriter::with_capacity(capacity, inner) }
13141273
}
@@ -1329,7 +1288,6 @@ impl<W: Write> LineWriter<W> {
13291288
/// Ok(())
13301289
/// }
13311290
/// ```
1332-
#[stable(feature = "rust1", since = "1.0.0")]
13331291
pub fn get_ref(&self) -> &W {
13341292
self.inner.get_ref()
13351293
}
@@ -1354,7 +1312,6 @@ impl<W: Write> LineWriter<W> {
13541312
/// Ok(())
13551313
/// }
13561314
/// ```
1357-
#[stable(feature = "rust1", since = "1.0.0")]
13581315
pub fn get_mut(&mut self) -> &mut W {
13591316
self.inner.get_mut()
13601317
}
@@ -1382,15 +1339,17 @@ impl<W: Write> LineWriter<W> {
13821339
/// Ok(())
13831340
/// }
13841341
/// ```
1385-
#[stable(feature = "rust1", since = "1.0.0")]
13861342
pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
13871343
self.inner
13881344
.into_inner()
13891345
.map_err(|IntoInnerError(buf, e)| IntoInnerError(LineWriter { inner: buf }, e))
13901346
}
1347+
1348+
pub fn purge(&mut self) {
1349+
self.inner.purge_buf();
1350+
}
13911351
}
13921352

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

1424-
#[stable(feature = "rust1", since = "1.0.0")]
14251383
impl<W: Write> fmt::Debug for LineWriter<W>
14261384
where
14271385
W: fmt::Debug,

0 commit comments

Comments
 (0)