@@ -210,55 +210,60 @@ impl<T> Cursor<T>
210
210
where
211
211
T : AsRef < [ u8 ] > ,
212
212
{
213
- /// Returns the remaining slice.
213
+ /// Splits the underlying slice at the cursor position and returns them .
214
214
///
215
215
/// # Examples
216
216
///
217
217
/// ```
218
- /// #![feature(cursor_remaining )]
218
+ /// #![feature(cursor_split )]
219
219
/// use std::io::Cursor;
220
220
///
221
221
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
222
222
///
223
- /// assert_eq!(buff.remaining_slice (), &[ 1, 2, 3, 4, 5]);
223
+ /// assert_eq!(buff.split (), ([].as_slice(), [ 1, 2, 3, 4, 5].as_slice()) );
224
224
///
225
225
/// buff.set_position(2);
226
- /// assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
227
- ///
228
- /// buff.set_position(4);
229
- /// assert_eq!(buff.remaining_slice(), &[5]);
226
+ /// assert_eq!(buff.split(), ([1, 2].as_slice(), [3, 4, 5].as_slice()));
230
227
///
231
228
/// buff.set_position(6);
232
- /// assert_eq!(buff.remaining_slice (), &[] );
229
+ /// assert_eq!(buff.split (), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()) );
233
230
/// ```
234
- #[ unstable( feature = "cursor_remaining" , issue = "86369" ) ]
235
- pub fn remaining_slice ( & self ) -> & [ u8 ] {
236
- let len = self . pos . min ( self . inner . as_ref ( ) . len ( ) as u64 ) ;
237
- & self . inner . as_ref ( ) [ ( len as usize ) ..]
231
+ #[ unstable( feature = "cursor_split" , issue = "86369" ) ]
232
+ pub fn split ( & self ) -> ( & [ u8 ] , & [ u8 ] ) {
233
+ let slice = self . inner . as_ref ( ) ;
234
+ let pos = self . pos . min ( slice. len ( ) as u64 ) ;
235
+ slice. split_at ( pos as usize )
238
236
}
237
+ }
239
238
240
- /// Returns `true` if the remaining slice is empty.
239
+ impl < T > Cursor < T >
240
+ where
241
+ T : AsMut < [ u8 ] > ,
242
+ {
243
+ /// Splits the underlying slice at the cursor position and returns them
244
+ /// mutably.
241
245
///
242
246
/// # Examples
243
247
///
244
248
/// ```
245
- /// #![feature(cursor_remaining )]
249
+ /// #![feature(cursor_split )]
246
250
/// use std::io::Cursor;
247
251
///
248
252
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
249
253
///
250
- /// buff.set_position(2);
251
- /// assert!(!buff.is_empty());
254
+ /// assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));
252
255
///
253
- /// buff.set_position(5 );
254
- /// assert !(buff.is_empty( ));
256
+ /// buff.set_position(2 );
257
+ /// assert_eq !(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice() ));
255
258
///
256
- /// buff.set_position(10 );
257
- /// assert !(buff.is_empty( ));
259
+ /// buff.set_position(6 );
260
+ /// assert_eq !(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice() ));
258
261
/// ```
259
- #[ unstable( feature = "cursor_remaining" , issue = "86369" ) ]
260
- pub fn is_empty ( & self ) -> bool {
261
- self . pos >= self . inner . as_ref ( ) . len ( ) as u64
262
+ #[ unstable( feature = "cursor_split" , issue = "86369" ) ]
263
+ pub fn split_mut ( & mut self ) -> ( & mut [ u8 ] , & mut [ u8 ] ) {
264
+ let slice = self . inner . as_mut ( ) ;
265
+ let pos = self . pos . min ( slice. len ( ) as u64 ) ;
266
+ slice. split_at_mut ( pos as usize )
262
267
}
263
268
}
264
269
@@ -320,15 +325,15 @@ where
320
325
T : AsRef < [ u8 ] > ,
321
326
{
322
327
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
323
- let n = Read :: read ( & mut self . remaining_slice ( ) , buf) ?;
328
+ let n = Read :: read ( & mut Cursor :: split ( self ) . 1 , buf) ?;
324
329
self . pos += n as u64 ;
325
330
Ok ( n)
326
331
}
327
332
328
333
fn read_buf ( & mut self , mut cursor : BorrowedCursor < ' _ > ) -> io:: Result < ( ) > {
329
334
let prev_written = cursor. written ( ) ;
330
335
331
- Read :: read_buf ( & mut self . remaining_slice ( ) , cursor. reborrow ( ) ) ?;
336
+ Read :: read_buf ( & mut Cursor :: split ( self ) . 1 , cursor. reborrow ( ) ) ?;
332
337
333
338
self . pos += ( cursor. written ( ) - prev_written) as u64 ;
334
339
@@ -352,7 +357,7 @@ where
352
357
}
353
358
354
359
fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < ( ) > {
355
- let result = Read :: read_exact ( & mut self . remaining_slice ( ) , buf) ;
360
+ let result = Read :: read_exact ( & mut Cursor :: split ( self ) . 1 , buf) ;
356
361
357
362
match result {
358
363
Ok ( _) => self . pos += buf. len ( ) as u64 ,
@@ -366,14 +371,14 @@ where
366
371
fn read_buf_exact ( & mut self , mut cursor : BorrowedCursor < ' _ > ) -> io:: Result < ( ) > {
367
372
let prev_written = cursor. written ( ) ;
368
373
369
- let result = Read :: read_buf_exact ( & mut self . remaining_slice ( ) , cursor. reborrow ( ) ) ;
374
+ let result = Read :: read_buf_exact ( & mut Cursor :: split ( self ) . 1 , cursor. reborrow ( ) ) ;
370
375
self . pos += ( cursor. written ( ) - prev_written) as u64 ;
371
376
372
377
result
373
378
}
374
379
375
380
fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
376
- let content = self . remaining_slice ( ) ;
381
+ let content = Cursor :: split ( self ) . 1 ;
377
382
let len = content. len ( ) ;
378
383
buf. try_reserve ( len) ?;
379
384
buf. extend_from_slice ( content) ;
@@ -384,7 +389,7 @@ where
384
389
385
390
fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
386
391
let content =
387
- crate :: str:: from_utf8 ( self . remaining_slice ( ) ) . map_err ( |_| io:: Error :: INVALID_UTF8 ) ?;
392
+ crate :: str:: from_utf8 ( Cursor :: split ( self ) . 1 ) . map_err ( |_| io:: Error :: INVALID_UTF8 ) ?;
388
393
let len = content. len ( ) ;
389
394
buf. try_reserve ( len) ?;
390
395
buf. push_str ( content) ;
@@ -400,7 +405,7 @@ where
400
405
T : AsRef < [ u8 ] > ,
401
406
{
402
407
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
403
- Ok ( self . remaining_slice ( ) )
408
+ Ok ( Cursor :: split ( self ) . 1 )
404
409
}
405
410
fn consume ( & mut self , amt : usize ) {
406
411
self . pos += amt as u64 ;
0 commit comments