@@ -210,55 +210,60 @@ impl<T> Cursor<T>
210210where
211211 T : AsRef < [ u8 ] > ,
212212{
213- /// Returns the remaining slice.
213+ /// Splits the underlying slice at the cursor position and returns them .
214214 ///
215215 /// # Examples
216216 ///
217217 /// ```
218- /// #![feature(cursor_remaining )]
218+ /// #![feature(cursor_split )]
219219 /// use std::io::Cursor;
220220 ///
221221 /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
222222 ///
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()) );
224224 ///
225225 /// 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()));
230227 ///
231228 /// buff.set_position(6);
232- /// assert_eq!(buff.remaining_slice (), &[] );
229+ /// assert_eq!(buff.split (), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()) );
233230 /// ```
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 )
238236 }
237+ }
239238
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.
241245 ///
242246 /// # Examples
243247 ///
244248 /// ```
245- /// #![feature(cursor_remaining )]
249+ /// #![feature(cursor_split )]
246250 /// use std::io::Cursor;
247251 ///
248252 /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
249253 ///
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()));
252255 ///
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() ));
255258 ///
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() ));
258261 /// ```
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 )
262267 }
263268}
264269
@@ -320,15 +325,15 @@ where
320325 T : AsRef < [ u8 ] > ,
321326{
322327 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) ?;
324329 self . pos += n as u64 ;
325330 Ok ( n)
326331 }
327332
328333 fn read_buf ( & mut self , mut cursor : BorrowedCursor < ' _ > ) -> io:: Result < ( ) > {
329334 let prev_written = cursor. written ( ) ;
330335
331- Read :: read_buf ( & mut self . remaining_slice ( ) , cursor. reborrow ( ) ) ?;
336+ Read :: read_buf ( & mut Cursor :: split ( self ) . 1 , cursor. reborrow ( ) ) ?;
332337
333338 self . pos += ( cursor. written ( ) - prev_written) as u64 ;
334339
@@ -352,7 +357,7 @@ where
352357 }
353358
354359 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) ;
356361
357362 match result {
358363 Ok ( _) => self . pos += buf. len ( ) as u64 ,
@@ -366,14 +371,14 @@ where
366371 fn read_buf_exact ( & mut self , mut cursor : BorrowedCursor < ' _ > ) -> io:: Result < ( ) > {
367372 let prev_written = cursor. written ( ) ;
368373
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 ( ) ) ;
370375 self . pos += ( cursor. written ( ) - prev_written) as u64 ;
371376
372377 result
373378 }
374379
375380 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 ;
377382 let len = content. len ( ) ;
378383 buf. try_reserve ( len) ?;
379384 buf. extend_from_slice ( content) ;
@@ -384,7 +389,7 @@ where
384389
385390 fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
386391 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 ) ?;
388393 let len = content. len ( ) ;
389394 buf. try_reserve ( len) ?;
390395 buf. push_str ( content) ;
@@ -400,7 +405,7 @@ where
400405 T : AsRef < [ u8 ] > ,
401406{
402407 fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
403- Ok ( self . remaining_slice ( ) )
408+ Ok ( Cursor :: split ( self ) . 1 )
404409 }
405410 fn consume ( & mut self , amt : usize ) {
406411 self . pos += amt as u64 ;
0 commit comments