@@ -209,6 +209,32 @@ impl<T> Cursor<T>
209
209
where
210
210
T : AsRef < [ u8 ] > ,
211
211
{
212
+ /// Returns the remaining length.
213
+ ///
214
+ /// # Examples
215
+ ///
216
+ /// ```
217
+ /// #![feature(cursor_remaining)]
218
+ /// use std::io::Cursor;
219
+ ///
220
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
221
+ ///
222
+ /// assert_eq!(buff.remaining(), 5);
223
+ ///
224
+ /// buff.set_position(2);
225
+ /// assert_eq!(buff.remaining(), 3);
226
+ ///
227
+ /// buff.set_position(4);
228
+ /// assert_eq!(buff.remaining(), 1);
229
+ ///
230
+ /// buff.set_position(6);
231
+ /// assert_eq!(buff.remaining(), 0);
232
+ /// ```
233
+ #[ unstable( feature = "cursor_remaining" , issue = "86369" ) ]
234
+ pub fn remaining ( & self ) -> u64 {
235
+ ( self . inner . as_ref ( ) . len ( ) as u64 ) . checked_sub ( self . pos ) . unwrap_or ( 0 )
236
+ }
237
+
212
238
/// Returns the remaining slice.
213
239
///
214
240
/// # Examples
@@ -219,19 +245,19 @@ where
219
245
///
220
246
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
221
247
///
222
- /// assert_eq!(buff.remaining (), &[1, 2, 3, 4, 5]);
248
+ /// assert_eq!(buff.remaining_slice (), &[1, 2, 3, 4, 5]);
223
249
///
224
250
/// buff.set_position(2);
225
- /// assert_eq!(buff.remaining (), &[3, 4, 5]);
251
+ /// assert_eq!(buff.remaining_slice (), &[3, 4, 5]);
226
252
///
227
253
/// buff.set_position(4);
228
- /// assert_eq!(buff.remaining (), &[5]);
254
+ /// assert_eq!(buff.remaining_slice (), &[5]);
229
255
///
230
256
/// buff.set_position(6);
231
- /// assert_eq!(buff.remaining (), &[]);
257
+ /// assert_eq!(buff.remaining_slice (), &[]);
232
258
/// ```
233
259
#[ unstable( feature = "cursor_remaining" , issue = "86369" ) ]
234
- pub fn remaining ( & self ) -> & [ u8 ] {
260
+ pub fn remaining_slice ( & self ) -> & [ u8 ] {
235
261
let len = self . pos . min ( self . inner . as_ref ( ) . len ( ) as u64 ) ;
236
262
& self . inner . as_ref ( ) [ ( len as usize ) ..]
237
263
}
@@ -324,7 +350,7 @@ where
324
350
T : AsRef < [ u8 ] > ,
325
351
{
326
352
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
327
- let n = Read :: read ( & mut self . remaining ( ) , buf) ?;
353
+ let n = Read :: read ( & mut self . remaining_slice ( ) , buf) ?;
328
354
self . pos += n as u64 ;
329
355
Ok ( n)
330
356
}
@@ -347,7 +373,7 @@ where
347
373
348
374
fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < ( ) > {
349
375
let n = buf. len ( ) ;
350
- Read :: read_exact ( & mut self . remaining ( ) , buf) ?;
376
+ Read :: read_exact ( & mut self . remaining_slice ( ) , buf) ?;
351
377
self . pos += n as u64 ;
352
378
Ok ( ( ) )
353
379
}
@@ -364,7 +390,7 @@ where
364
390
T : AsRef < [ u8 ] > ,
365
391
{
366
392
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
367
- Ok ( self . remaining ( ) )
393
+ Ok ( self . remaining_slice ( ) )
368
394
}
369
395
fn consume ( & mut self , amt : usize ) {
370
396
self . pos += amt as u64 ;
0 commit comments