1
- //! Buffering wrappers for I/O traits
2
1
3
- #[ cfg( test) ]
4
- mod tests;
2
+ //! Buffering wrappers for I/O traits
5
3
6
- use crate :: io:: prelude:: * ;
4
+ use core:: prelude:: v1:: * ;
5
+ use io:: prelude:: * ;
7
6
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;
15
11
16
12
/// The `BufReader<R>` struct adds buffering to any reader.
17
13
///
@@ -52,7 +48,6 @@ use crate::memchr;
52
48
/// Ok(())
53
49
/// }
54
50
/// ```
55
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
56
51
pub struct BufReader < R > {
57
52
inner : R ,
58
53
buf : Box < [ u8 ] > ,
@@ -76,7 +71,6 @@ impl<R: Read> BufReader<R> {
76
71
/// Ok(())
77
72
/// }
78
73
/// ```
79
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
80
74
pub fn new ( inner : R ) -> BufReader < R > {
81
75
BufReader :: with_capacity ( DEFAULT_BUF_SIZE , inner)
82
76
}
@@ -97,7 +91,6 @@ impl<R: Read> BufReader<R> {
97
91
/// Ok(())
98
92
/// }
99
93
/// ```
100
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
101
94
pub fn with_capacity ( capacity : usize , inner : R ) -> BufReader < R > {
102
95
unsafe {
103
96
let mut buffer = Vec :: with_capacity ( capacity) ;
@@ -127,7 +120,6 @@ impl<R> BufReader<R> {
127
120
/// Ok(())
128
121
/// }
129
122
/// ```
130
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
131
123
pub fn get_ref ( & self ) -> & R {
132
124
& self . inner
133
125
}
@@ -150,7 +142,6 @@ impl<R> BufReader<R> {
150
142
/// Ok(())
151
143
/// }
152
144
/// ```
153
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
154
145
pub fn get_mut ( & mut self ) -> & mut R {
155
146
& mut self . inner
156
147
}
@@ -178,7 +169,6 @@ impl<R> BufReader<R> {
178
169
/// Ok(())
179
170
/// }
180
171
/// ```
181
- #[ stable( feature = "bufreader_buffer" , since = "1.37.0" ) ]
182
172
pub fn buffer ( & self ) -> & [ u8 ] {
183
173
& self . buf [ self . pos ..self . cap ]
184
174
}
@@ -201,7 +191,6 @@ impl<R> BufReader<R> {
201
191
/// Ok(())
202
192
/// }
203
193
/// ```
204
- #[ stable( feature = "buffered_io_capacity" , since = "1.46.0" ) ]
205
194
pub fn capacity ( & self ) -> usize {
206
195
self . buf . len ( )
207
196
}
@@ -225,7 +214,6 @@ impl<R> BufReader<R> {
225
214
/// Ok(())
226
215
/// }
227
216
/// ```
228
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
229
217
pub fn into_inner ( self ) -> R {
230
218
self . inner
231
219
}
@@ -243,7 +231,6 @@ impl<R: Seek> BufReader<R> {
243
231
/// the buffer will not be flushed, allowing for more efficient seeks.
244
232
/// This method does not return the location of the underlying reader, so the caller
245
233
/// must track this information themselves if it is required.
246
- #[ unstable( feature = "bufreader_seek_relative" , issue = "31100" ) ]
247
234
pub fn seek_relative ( & mut self , offset : i64 ) -> io:: Result < ( ) > {
248
235
let pos = self . pos as u64 ;
249
236
if offset < 0 {
@@ -263,7 +250,6 @@ impl<R: Seek> BufReader<R> {
263
250
}
264
251
}
265
252
266
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
267
253
impl < R : Read > Read for BufReader < R > {
268
254
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
269
255
// 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> {
305
291
}
306
292
}
307
293
308
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
309
294
impl < R : Read > BufRead for BufReader < R > {
310
295
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
311
296
// 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> {
325
310
}
326
311
}
327
312
328
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
329
313
impl < R > fmt:: Debug for BufReader < R >
330
314
where
331
315
R : fmt:: Debug ,
@@ -338,7 +322,6 @@ where
338
322
}
339
323
}
340
324
341
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
342
325
impl < R : Seek > Seek for BufReader < R > {
343
326
/// Seek to an offset, in bytes, in the underlying reader.
344
327
///
@@ -492,10 +475,9 @@ impl<R: Seek> Seek for BufReader<R> {
492
475
/// [`TcpStream::write`]: Write::write
493
476
/// [`TcpStream`]: crate::net::TcpStream
494
477
/// [`flush`]: Write::flush
495
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
496
478
pub struct BufWriter < W : Write > {
497
479
inner : Option < W > ,
498
- buf : Vec < u8 > ,
480
+ pub buf : Vec < u8 > ,
499
481
// #30888: If the inner writer panics in a call to write, we don't want to
500
482
// write the buffered data a second time in BufWriter's destructor. This
501
483
// flag tells the Drop impl if it should skip the flush.
@@ -527,7 +509,6 @@ pub struct BufWriter<W: Write> {
527
509
/// };
528
510
/// ```
529
511
#[ derive( Debug ) ]
530
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
531
512
pub struct IntoInnerError < W > ( W , Error ) ;
532
513
533
514
impl < W : Write > BufWriter < W > {
@@ -542,7 +523,6 @@ impl<W: Write> BufWriter<W> {
542
523
///
543
524
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
544
525
/// ```
545
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
546
526
pub fn new ( inner : W ) -> BufWriter < W > {
547
527
BufWriter :: with_capacity ( DEFAULT_BUF_SIZE , inner)
548
528
}
@@ -560,7 +540,6 @@ impl<W: Write> BufWriter<W> {
560
540
/// let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
561
541
/// let mut buffer = BufWriter::with_capacity(100, stream);
562
542
/// ```
563
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
564
543
pub fn with_capacity ( capacity : usize , inner : W ) -> BufWriter < W > {
565
544
BufWriter { inner : Some ( inner) , buf : Vec :: with_capacity ( capacity) , panicked : false }
566
545
}
@@ -632,6 +611,10 @@ impl<W: Write> BufWriter<W> {
632
611
Ok ( ( ) )
633
612
}
634
613
614
+ pub fn purge_buf ( & mut self ) {
615
+ self . buf = vec ! [ ] ;
616
+ }
617
+
635
618
/// Buffer some data without flushing it, regardless of the size of the
636
619
/// data. Writes as much as possible without exceeding capacity. Returns
637
620
/// the number of bytes written.
@@ -655,7 +638,6 @@ impl<W: Write> BufWriter<W> {
655
638
/// // we can use reference just like buffer
656
639
/// let reference = buffer.get_ref();
657
640
/// ```
658
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
659
641
pub fn get_ref ( & self ) -> & W {
660
642
self . inner . as_ref ( ) . unwrap ( )
661
643
}
@@ -675,7 +657,6 @@ impl<W: Write> BufWriter<W> {
675
657
/// // we can use reference just like buffer
676
658
/// let reference = buffer.get_mut();
677
659
/// ```
678
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
679
660
pub fn get_mut ( & mut self ) -> & mut W {
680
661
self . inner . as_mut ( ) . unwrap ( )
681
662
}
@@ -693,7 +674,6 @@ impl<W: Write> BufWriter<W> {
693
674
/// // See how many bytes are currently buffered
694
675
/// let bytes_buffered = buf_writer.buffer().len();
695
676
/// ```
696
- #[ stable( feature = "bufreader_buffer" , since = "1.37.0" ) ]
697
677
pub fn buffer ( & self ) -> & [ u8 ] {
698
678
& self . buf
699
679
}
@@ -713,7 +693,6 @@ impl<W: Write> BufWriter<W> {
713
693
/// // Calculate how many bytes can be written without flushing
714
694
/// let without_flush = capacity - buf_writer.buffer().len();
715
695
/// ```
716
- #[ stable( feature = "buffered_io_capacity" , since = "1.46.0" ) ]
717
696
pub fn capacity ( & self ) -> usize {
718
697
self . buf . capacity ( )
719
698
}
@@ -737,7 +716,6 @@ impl<W: Write> BufWriter<W> {
737
716
/// // unwrap the TcpStream and flush the buffer
738
717
/// let stream = buffer.into_inner().unwrap();
739
718
/// ```
740
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
741
719
pub fn into_inner ( mut self ) -> Result < W , IntoInnerError < BufWriter < W > > > {
742
720
match self . flush_buf ( ) {
743
721
Err ( e) => Err ( IntoInnerError ( self , e) ) ,
@@ -746,7 +724,6 @@ impl<W: Write> BufWriter<W> {
746
724
}
747
725
}
748
726
749
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
750
727
impl < W : Write > Write for BufWriter < W > {
751
728
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
752
729
if self . buf . len ( ) + buf. len ( ) > self . buf . capacity ( ) {
@@ -810,7 +787,6 @@ impl<W: Write> Write for BufWriter<W> {
810
787
}
811
788
}
812
789
813
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
814
790
impl < W : Write > fmt:: Debug for BufWriter < W >
815
791
where
816
792
W : fmt:: Debug ,
@@ -823,7 +799,6 @@ where
823
799
}
824
800
}
825
801
826
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
827
802
impl < W : Write + Seek > Seek for BufWriter < W > {
828
803
/// Seek to the offset, in bytes, in the underlying writer.
829
804
///
@@ -834,7 +809,6 @@ impl<W: Write + Seek> Seek for BufWriter<W> {
834
809
}
835
810
}
836
811
837
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
838
812
impl < W : Write > Drop for BufWriter < W > {
839
813
fn drop ( & mut self ) {
840
814
if self . inner . is_some ( ) && !self . panicked {
@@ -874,7 +848,6 @@ impl<W> IntoInnerError<W> {
874
848
/// }
875
849
/// };
876
850
/// ```
877
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
878
851
pub fn error ( & self ) -> & Error {
879
852
& self . 1
880
853
}
@@ -909,28 +882,17 @@ impl<W> IntoInnerError<W> {
909
882
/// }
910
883
/// };
911
884
/// ```
912
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
913
885
pub fn into_inner ( self ) -> W {
914
886
self . 0
915
887
}
916
888
}
917
889
918
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
919
890
impl < W > From < IntoInnerError < W > > for Error {
920
891
fn from ( iie : IntoInnerError < W > ) -> Error {
921
892
iie. 1
922
893
}
923
894
}
924
895
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" ) ]
934
896
impl < W > fmt:: Display for IntoInnerError < W > {
935
897
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
936
898
self . error ( ) . fmt ( f)
@@ -1267,9 +1229,8 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
1267
1229
/// Ok(())
1268
1230
/// }
1269
1231
/// ```
1270
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1271
1232
pub struct LineWriter < W : Write > {
1272
- inner : BufWriter < W > ,
1233
+ pub inner : BufWriter < W > ,
1273
1234
}
1274
1235
1275
1236
impl < W : Write > LineWriter < W > {
@@ -1287,7 +1248,6 @@ impl<W: Write> LineWriter<W> {
1287
1248
/// Ok(())
1288
1249
/// }
1289
1250
/// ```
1290
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1291
1251
pub fn new ( inner : W ) -> LineWriter < W > {
1292
1252
// Lines typically aren't that long, don't use a giant buffer
1293
1253
LineWriter :: with_capacity ( 1024 , inner)
@@ -1308,7 +1268,6 @@ impl<W: Write> LineWriter<W> {
1308
1268
/// Ok(())
1309
1269
/// }
1310
1270
/// ```
1311
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1312
1271
pub fn with_capacity ( capacity : usize , inner : W ) -> LineWriter < W > {
1313
1272
LineWriter { inner : BufWriter :: with_capacity ( capacity, inner) }
1314
1273
}
@@ -1329,7 +1288,6 @@ impl<W: Write> LineWriter<W> {
1329
1288
/// Ok(())
1330
1289
/// }
1331
1290
/// ```
1332
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1333
1291
pub fn get_ref ( & self ) -> & W {
1334
1292
self . inner . get_ref ( )
1335
1293
}
@@ -1354,7 +1312,6 @@ impl<W: Write> LineWriter<W> {
1354
1312
/// Ok(())
1355
1313
/// }
1356
1314
/// ```
1357
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1358
1315
pub fn get_mut ( & mut self ) -> & mut W {
1359
1316
self . inner . get_mut ( )
1360
1317
}
@@ -1382,15 +1339,17 @@ impl<W: Write> LineWriter<W> {
1382
1339
/// Ok(())
1383
1340
/// }
1384
1341
/// ```
1385
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1386
1342
pub fn into_inner ( self ) -> Result < W , IntoInnerError < LineWriter < W > > > {
1387
1343
self . inner
1388
1344
. into_inner ( )
1389
1345
. map_err ( |IntoInnerError ( buf, e) | IntoInnerError ( LineWriter { inner : buf } , e) )
1390
1346
}
1347
+
1348
+ pub fn purge ( & mut self ) {
1349
+ self . inner . purge_buf ( ) ;
1350
+ }
1391
1351
}
1392
1352
1393
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1394
1353
impl < W : Write > Write for LineWriter < W > {
1395
1354
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
1396
1355
LineWriterShim :: new ( & mut self . inner ) . write ( buf)
@@ -1421,7 +1380,6 @@ impl<W: Write> Write for LineWriter<W> {
1421
1380
}
1422
1381
}
1423
1382
1424
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1425
1383
impl < W : Write > fmt:: Debug for LineWriter < W >
1426
1384
where
1427
1385
W : fmt:: Debug ,
0 commit comments