@@ -208,55 +208,152 @@ impl<T> Cursor<T>
208
208
where
209
209
T : AsRef < [ u8 ] > ,
210
210
{
211
- /// Returns the remaining slice.
211
+ /// Splits the underlying slice at the cursor position and returns them .
212
212
///
213
213
/// # Examples
214
214
///
215
215
/// ```
216
- /// #![feature(cursor_remaining )]
216
+ /// #![feature(cursor_split )]
217
217
/// use std::io::Cursor;
218
218
///
219
219
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
220
220
///
221
- /// assert_eq!(buff.remaining_slice (), &[ 1, 2, 3, 4, 5]);
221
+ /// assert_eq!(buff.split (), ([].as_slice(), [ 1, 2, 3, 4, 5].as_slice()) );
222
222
///
223
223
/// buff.set_position(2);
224
- /// assert_eq!(buff.remaining_slice (), &[ 3, 4, 5]);
224
+ /// assert_eq!(buff.split (), ([1, 2].as_slice(), [ 3, 4, 5].as_slice()) );
225
225
///
226
- /// buff.set_position(4);
227
- /// assert_eq!(buff.remaining_slice(), &[5]);
226
+ /// buff.set_position(6);
227
+ /// assert_eq!(buff.split(), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()));
228
+ /// ```
229
+ #[ unstable( feature = "cursor_split" , issue = "86369" ) ]
230
+ pub fn split ( & self ) -> ( & [ u8 ] , & [ u8 ] ) {
231
+ let slice = self . inner . as_ref ( ) ;
232
+ let pos = self . pos . min ( slice. len ( ) as u64 ) ;
233
+ slice. split_at ( pos as usize )
234
+ }
235
+
236
+ /// Returns the slice before the cursor position.
237
+ ///
238
+ /// # Examples
239
+ ///
240
+ /// ```
241
+ /// #![feature(cursor_split)]
242
+ /// use std::io::Cursor;
243
+ ///
244
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
245
+ ///
246
+ /// assert_eq!(buff.before(), &[]);
247
+ ///
248
+ /// buff.set_position(2);
249
+ /// assert_eq!(buff.before(), &[1, 2]);
228
250
///
229
251
/// buff.set_position(6);
230
- /// assert_eq!(buff.remaining_slice (), &[]);
252
+ /// assert_eq!(buff.before (), &[1, 2, 3, 4, 5 ]);
231
253
/// ```
232
- #[ unstable( feature = "cursor_remaining" , issue = "86369" ) ]
233
- pub fn remaining_slice ( & self ) -> & [ u8 ] {
234
- let len = self . pos . min ( self . inner . as_ref ( ) . len ( ) as u64 ) ;
235
- & self . inner . as_ref ( ) [ ( len as usize ) ..]
254
+ #[ unstable( feature = "cursor_split" , issue = "86369" ) ]
255
+ pub fn before ( & self ) -> & [ u8 ] {
256
+ self . split ( ) . 0
236
257
}
237
258
238
- /// Returns `true` if the remaining slice is empty .
259
+ /// Returns the slice after the cursor position .
239
260
///
240
261
/// # Examples
241
262
///
242
263
/// ```
243
- /// #![feature(cursor_remaining )]
264
+ /// #![feature(cursor_split )]
244
265
/// use std::io::Cursor;
245
266
///
246
267
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
247
268
///
269
+ /// assert_eq!(buff.after(), &[1, 2, 3, 4, 5]);
270
+ ///
248
271
/// buff.set_position(2);
249
- /// assert!(! buff.is_empty() );
272
+ /// assert_eq!( buff.after(), &[3, 4, 5] );
250
273
///
251
- /// buff.set_position(5);
252
- /// assert!(buff.is_empty());
274
+ /// buff.set_position(6);
275
+ /// assert_eq!(buff.after(), &[]);
276
+ /// ```
277
+ #[ unstable( feature = "cursor_split" , issue = "86369" ) ]
278
+ pub fn after ( & self ) -> & [ u8 ] {
279
+ self . split ( ) . 1
280
+ }
281
+ }
282
+
283
+ impl < T > Cursor < T >
284
+ where
285
+ T : AsMut < [ u8 ] > ,
286
+ {
287
+ /// Splits the underlying slice at the cursor position and returns them
288
+ /// mutably.
289
+ ///
290
+ /// # Examples
291
+ ///
292
+ /// ```
293
+ /// #![feature(cursor_split)]
294
+ /// use std::io::Cursor;
253
295
///
254
- /// buff.set_position(10);
255
- /// assert!(buff.is_empty());
296
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
297
+ ///
298
+ /// assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));
299
+ ///
300
+ /// buff.set_position(2);
301
+ /// assert_eq!(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice()));
302
+ ///
303
+ /// buff.set_position(6);
304
+ /// assert_eq!(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice()));
305
+ /// ```
306
+ #[ unstable( feature = "cursor_split" , issue = "86369" ) ]
307
+ pub fn split_mut ( & mut self ) -> ( & mut [ u8 ] , & mut [ u8 ] ) {
308
+ let slice = self . inner . as_mut ( ) ;
309
+ let pos = self . pos . min ( slice. len ( ) as u64 ) ;
310
+ slice. split_at_mut ( pos as usize )
311
+ }
312
+
313
+ /// Returns the mutable slice before the cursor position.
314
+ ///
315
+ /// # Examples
316
+ ///
317
+ /// ```
318
+ /// #![feature(cursor_split)]
319
+ /// use std::io::Cursor;
320
+ ///
321
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
322
+ ///
323
+ /// assert_eq!(buff.before_mut(), &[]);
324
+ ///
325
+ /// buff.set_position(2);
326
+ /// assert_eq!(buff.before_mut(), &[1, 2]);
327
+ ///
328
+ /// buff.set_position(6);
329
+ /// assert_eq!(buff.before_mut(), &[1, 2, 3, 4, 5]);
330
+ /// ```
331
+ #[ unstable( feature = "cursor_split" , issue = "86369" ) ]
332
+ pub fn before_mut ( & mut self ) -> & mut [ u8 ] {
333
+ self . split_mut ( ) . 0
334
+ }
335
+
336
+ /// Returns the mutable slice after the cursor position.
337
+ ///
338
+ /// # Examples
339
+ ///
340
+ /// ```
341
+ /// #![feature(cursor_split)]
342
+ /// use std::io::Cursor;
343
+ ///
344
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
345
+ ///
346
+ /// assert_eq!(buff.after_mut(), &[1, 2, 3, 4, 5]);
347
+ ///
348
+ /// buff.set_position(2);
349
+ /// assert_eq!(buff.after_mut(), &[3, 4, 5]);
350
+ ///
351
+ /// buff.set_position(6);
352
+ /// assert_eq!(buff.after_mut(), &[]);
256
353
/// ```
257
- #[ unstable( feature = "cursor_remaining " , issue = "86369" ) ]
258
- pub fn is_empty ( & self ) -> bool {
259
- self . pos >= self . inner . as_ref ( ) . len ( ) as u64
354
+ #[ unstable( feature = "cursor_split " , issue = "86369" ) ]
355
+ pub fn after_mut ( & mut self ) -> & mut [ u8 ] {
356
+ self . split_mut ( ) . 1
260
357
}
261
358
}
262
359
@@ -318,7 +415,7 @@ where
318
415
T : AsRef < [ u8 ] > ,
319
416
{
320
417
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
321
- let n = Read :: read ( & mut self . remaining_slice ( ) , buf) ?;
418
+ let n = Read :: read ( & mut self . after ( ) , buf) ?;
322
419
self . pos += n as u64 ;
323
420
Ok ( n)
324
421
}
@@ -351,7 +448,7 @@ where
351
448
352
449
fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < ( ) > {
353
450
let n = buf. len ( ) ;
354
- Read :: read_exact ( & mut self . remaining_slice ( ) , buf) ?;
451
+ Read :: read_exact ( & mut self . after ( ) , buf) ?;
355
452
self . pos += n as u64 ;
356
453
Ok ( ( ) )
357
454
}
@@ -363,7 +460,7 @@ where
363
460
T : AsRef < [ u8 ] > ,
364
461
{
365
462
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
366
- Ok ( self . remaining_slice ( ) )
463
+ Ok ( self . after ( ) )
367
464
}
368
465
fn consume ( & mut self , amt : usize ) {
369
466
self . pos += amt as u64 ;
0 commit comments