@@ -253,6 +253,7 @@ mod tests;
253253
254254use crate :: cmp;
255255use crate :: fmt;
256+ use crate :: mem:: replace;
256257use crate :: ops:: { Deref , DerefMut } ;
257258use crate :: ptr;
258259use crate :: slice;
@@ -1044,6 +1045,32 @@ impl<'a> IoSliceMut<'a> {
10441045
10451046 /// Advance the internal cursor of the slice.
10461047 ///
1048+ /// Also see [`IoSliceMut::advance_slices`] to advance the cursors of
1049+ /// multiple buffers.
1050+ ///
1051+ /// # Examples
1052+ ///
1053+ /// ```
1054+ /// #![feature(io_slice_advance)]
1055+ ///
1056+ /// use std::io::IoSliceMut;
1057+ /// use std::ops::Deref;
1058+ ///
1059+ /// let mut data = [1; 8];
1060+ /// let mut buf = IoSliceMut::new(&mut data);
1061+ ///
1062+ /// // Mark 3 bytes as read.
1063+ /// buf.advance(3);
1064+ /// assert_eq!(buf.deref(), [1; 5].as_ref());
1065+ /// ```
1066+ #[ unstable( feature = "io_slice_advance" , issue = "62726" ) ]
1067+ #[ inline]
1068+ pub fn advance ( & mut self , n : usize ) {
1069+ self . 0 . advance ( n)
1070+ }
1071+
1072+ /// Advance the internal cursor of the slices.
1073+ ///
10471074 /// # Notes
10481075 ///
10491076 /// Elements in the slice may be modified if the cursor is not advanced to
@@ -1070,13 +1097,13 @@ impl<'a> IoSliceMut<'a> {
10701097 /// ][..];
10711098 ///
10721099 /// // Mark 10 bytes as read.
1073- /// bufs = IoSliceMut::advance( bufs, 10);
1100+ /// IoSliceMut::advance_slices(&mut bufs, 10);
10741101 /// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
10751102 /// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
10761103 /// ```
10771104 #[ unstable( feature = "io_slice_advance" , issue = "62726" ) ]
10781105 #[ inline]
1079- pub fn advance < ' b > ( bufs : & ' b mut [ IoSliceMut < ' a > ] , n : usize ) -> & ' b mut [ IoSliceMut < ' a > ] {
1106+ pub fn advance_slices ( bufs : & mut & mut [ IoSliceMut < ' a > ] , n : usize ) {
10801107 // Number of buffers to remove.
10811108 let mut remove = 0 ;
10821109 // Total length of all the to be removed buffers.
@@ -1090,11 +1117,10 @@ impl<'a> IoSliceMut<'a> {
10901117 }
10911118 }
10921119
1093- let bufs = & mut bufs[ remove..] ;
1120+ * bufs = & mut replace ( bufs, & mut [ ] ) [ remove..] ;
10941121 if !bufs. is_empty ( ) {
1095- bufs[ 0 ] . 0 . advance ( n - accumulated_len)
1122+ bufs[ 0 ] . advance ( n - accumulated_len)
10961123 }
1097- bufs
10981124 }
10991125}
11001126
@@ -1153,6 +1179,32 @@ impl<'a> IoSlice<'a> {
11531179
11541180 /// Advance the internal cursor of the slice.
11551181 ///
1182+ /// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple
1183+ /// buffers.
1184+ ///
1185+ /// # Examples
1186+ ///
1187+ /// ```
1188+ /// #![feature(io_slice_advance)]
1189+ ///
1190+ /// use std::io::IoSlice;
1191+ /// use std::ops::Deref;
1192+ ///
1193+ /// let mut data = [1; 8];
1194+ /// let mut buf = IoSlice::new(&mut data);
1195+ ///
1196+ /// // Mark 3 bytes as read.
1197+ /// buf.advance(3);
1198+ /// assert_eq!(buf.deref(), [1; 5].as_ref());
1199+ /// ```
1200+ #[ unstable( feature = "io_slice_advance" , issue = "62726" ) ]
1201+ #[ inline]
1202+ pub fn advance ( & mut self , n : usize ) {
1203+ self . 0 . advance ( n)
1204+ }
1205+
1206+ /// Advance the internal cursor of the slices.
1207+ ///
11561208 /// # Notes
11571209 ///
11581210 /// Elements in the slice may be modified if the cursor is not advanced to
@@ -1179,12 +1231,12 @@ impl<'a> IoSlice<'a> {
11791231 /// ][..];
11801232 ///
11811233 /// // Mark 10 bytes as written.
1182- /// bufs = IoSlice::advance( bufs, 10);
1234+ /// IoSlice::advance_slices(&mut bufs, 10);
11831235 /// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
11841236 /// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
11851237 #[ unstable( feature = "io_slice_advance" , issue = "62726" ) ]
11861238 #[ inline]
1187- pub fn advance < ' b > ( bufs : & ' b mut [ IoSlice < ' a > ] , n : usize ) -> & ' b mut [ IoSlice < ' a > ] {
1239+ pub fn advance_slices ( bufs : & mut & mut [ IoSlice < ' a > ] , n : usize ) {
11881240 // Number of buffers to remove.
11891241 let mut remove = 0 ;
11901242 // Total length of all the to be removed buffers.
@@ -1198,11 +1250,10 @@ impl<'a> IoSlice<'a> {
11981250 }
11991251 }
12001252
1201- let bufs = & mut bufs[ remove..] ;
1253+ * bufs = & mut replace ( bufs, & mut [ ] ) [ remove..] ;
12021254 if !bufs. is_empty ( ) {
1203- bufs[ 0 ] . 0 . advance ( n - accumulated_len)
1255+ bufs[ 0 ] . advance ( n - accumulated_len)
12041256 }
1205- bufs
12061257 }
12071258}
12081259
@@ -1511,7 +1562,7 @@ pub trait Write {
15111562 fn write_all_vectored ( & mut self , mut bufs : & mut [ IoSlice < ' _ > ] ) -> Result < ( ) > {
15121563 // Guarantee that bufs is empty if it contains no data,
15131564 // to avoid calling write_vectored if there is no data to be written.
1514- bufs = IoSlice :: advance ( bufs, 0 ) ;
1565+ IoSlice :: advance_slices ( & mut bufs, 0 ) ;
15151566 while !bufs. is_empty ( ) {
15161567 match self . write_vectored ( bufs) {
15171568 Ok ( 0 ) => {
@@ -1520,7 +1571,7 @@ pub trait Write {
15201571 & "failed to write whole buffer" ,
15211572 ) ) ;
15221573 }
1523- Ok ( n) => bufs = IoSlice :: advance ( bufs, n) ,
1574+ Ok ( n) => IoSlice :: advance_slices ( & mut bufs, n) ,
15241575 Err ( ref e) if e. kind ( ) == ErrorKind :: Interrupted => { }
15251576 Err ( e) => return Err ( e) ,
15261577 }
0 commit comments