@@ -205,6 +205,62 @@ impl<T> Cursor<T> {
205
205
}
206
206
}
207
207
208
+ impl < T > Cursor < T >
209
+ where
210
+ T : AsRef < [ u8 ] > ,
211
+ {
212
+ /// Returns the remaining slice.
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(), &[1, 2, 3, 4, 5]);
223
+ ///
224
+ /// buff.set_position(2);
225
+ /// assert_eq!(buff.remaining(), &[3, 4, 5]);
226
+ ///
227
+ /// buff.set_position(4);
228
+ /// assert_eq!(buff.remaining(), &[5]);
229
+ ///
230
+ /// buff.set_position(6);
231
+ /// assert_eq!(buff.remaining(), &[]);
232
+ /// ```
233
+ #[ unstable( feature = "cursor_remaining" , issue = "none" ) ]
234
+ pub fn remaining ( & self ) -> & [ u8 ] {
235
+ let len = self . pos . min ( self . inner . as_ref ( ) . len ( ) as u64 ) ;
236
+ & self . inner . as_ref ( ) [ ( len as usize ) ..]
237
+ }
238
+
239
+ /// Returns `true` if the remaining slice is empty.
240
+ ///
241
+ /// # Examples
242
+ ///
243
+ /// ```
244
+ /// #![feature(cursor_remaining)]
245
+ /// use std::io::Cursor;
246
+ ///
247
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
248
+ ///
249
+ /// buff.set_position(2);
250
+ /// assert!(!buff.is_empty());
251
+ ///
252
+ /// buff.set_position(5);
253
+ /// assert!(buff.is_empty());
254
+ ///
255
+ /// buff.set_position(10);
256
+ /// assert!(buff.is_empty());
257
+ /// ```
258
+ #[ unstable( feature = "cursor_remaining" , issue = "none" ) ]
259
+ pub fn is_empty ( & self ) -> bool {
260
+ self . pos >= self . inner . as_ref ( ) . len ( ) as u64
261
+ }
262
+ }
263
+
208
264
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
209
265
impl < T > Clone for Cursor < T >
210
266
where
@@ -268,7 +324,7 @@ where
268
324
T : AsRef < [ u8 ] > ,
269
325
{
270
326
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
271
- let n = Read :: read ( & mut self . fill_buf ( ) ? , buf) ?;
327
+ let n = Read :: read ( & mut self . remaining ( ) , buf) ?;
272
328
self . pos += n as u64 ;
273
329
Ok ( n)
274
330
}
@@ -291,7 +347,7 @@ where
291
347
292
348
fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < ( ) > {
293
349
let n = buf. len ( ) ;
294
- Read :: read_exact ( & mut self . fill_buf ( ) ? , buf) ?;
350
+ Read :: read_exact ( & mut self . remaining ( ) , buf) ?;
295
351
self . pos += n as u64 ;
296
352
Ok ( ( ) )
297
353
}
@@ -308,8 +364,7 @@ where
308
364
T : AsRef < [ u8 ] > ,
309
365
{
310
366
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
311
- let amt = cmp:: min ( self . pos , self . inner . as_ref ( ) . len ( ) as u64 ) ;
312
- Ok ( & self . inner . as_ref ( ) [ ( amt as usize ) ..] )
367
+ Ok ( self . remaining ( ) )
313
368
}
314
369
fn consume ( & mut self , amt : usize ) {
315
370
self . pos += amt as u64 ;
0 commit comments