@@ -253,6 +253,7 @@ mod tests;
253
253
254
254
use crate :: cmp;
255
255
use crate :: fmt;
256
+ use crate :: mem:: replace;
256
257
use crate :: ops:: { Deref , DerefMut } ;
257
258
use crate :: ptr;
258
259
use crate :: slice;
@@ -1044,6 +1045,32 @@ impl<'a> IoSliceMut<'a> {
1044
1045
1045
1046
/// Advance the internal cursor of the slice.
1046
1047
///
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
+ ///
1047
1074
/// # Notes
1048
1075
///
1049
1076
/// Elements in the slice may be modified if the cursor is not advanced to
@@ -1070,13 +1097,13 @@ impl<'a> IoSliceMut<'a> {
1070
1097
/// ][..];
1071
1098
///
1072
1099
/// // Mark 10 bytes as read.
1073
- /// bufs = IoSliceMut::advance( bufs, 10);
1100
+ /// IoSliceMut::advance_slices(&mut bufs, 10);
1074
1101
/// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
1075
1102
/// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
1076
1103
/// ```
1077
1104
#[ unstable( feature = "io_slice_advance" , issue = "62726" ) ]
1078
1105
#[ 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 ) {
1080
1107
// Number of buffers to remove.
1081
1108
let mut remove = 0 ;
1082
1109
// Total length of all the to be removed buffers.
@@ -1090,11 +1117,10 @@ impl<'a> IoSliceMut<'a> {
1090
1117
}
1091
1118
}
1092
1119
1093
- let bufs = & mut bufs[ remove..] ;
1120
+ * bufs = & mut replace ( bufs, & mut [ ] ) [ remove..] ;
1094
1121
if !bufs. is_empty ( ) {
1095
- bufs[ 0 ] . 0 . advance ( n - accumulated_len)
1122
+ bufs[ 0 ] . advance ( n - accumulated_len)
1096
1123
}
1097
- bufs
1098
1124
}
1099
1125
}
1100
1126
@@ -1153,6 +1179,32 @@ impl<'a> IoSlice<'a> {
1153
1179
1154
1180
/// Advance the internal cursor of the slice.
1155
1181
///
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
+ ///
1156
1208
/// # Notes
1157
1209
///
1158
1210
/// Elements in the slice may be modified if the cursor is not advanced to
@@ -1179,12 +1231,12 @@ impl<'a> IoSlice<'a> {
1179
1231
/// ][..];
1180
1232
///
1181
1233
/// // Mark 10 bytes as written.
1182
- /// bufs = IoSlice::advance( bufs, 10);
1234
+ /// IoSlice::advance_slices(&mut bufs, 10);
1183
1235
/// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
1184
1236
/// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
1185
1237
#[ unstable( feature = "io_slice_advance" , issue = "62726" ) ]
1186
1238
#[ 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 ) {
1188
1240
// Number of buffers to remove.
1189
1241
let mut remove = 0 ;
1190
1242
// Total length of all the to be removed buffers.
@@ -1198,11 +1250,10 @@ impl<'a> IoSlice<'a> {
1198
1250
}
1199
1251
}
1200
1252
1201
- let bufs = & mut bufs[ remove..] ;
1253
+ * bufs = & mut replace ( bufs, & mut [ ] ) [ remove..] ;
1202
1254
if !bufs. is_empty ( ) {
1203
- bufs[ 0 ] . 0 . advance ( n - accumulated_len)
1255
+ bufs[ 0 ] . advance ( n - accumulated_len)
1204
1256
}
1205
- bufs
1206
1257
}
1207
1258
}
1208
1259
@@ -1511,7 +1562,7 @@ pub trait Write {
1511
1562
fn write_all_vectored ( & mut self , mut bufs : & mut [ IoSlice < ' _ > ] ) -> Result < ( ) > {
1512
1563
// Guarantee that bufs is empty if it contains no data,
1513
1564
// 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 ) ;
1515
1566
while !bufs. is_empty ( ) {
1516
1567
match self . write_vectored ( bufs) {
1517
1568
Ok ( 0 ) => {
@@ -1520,7 +1571,7 @@ pub trait Write {
1520
1571
& "failed to write whole buffer" ,
1521
1572
) ) ;
1522
1573
}
1523
- Ok ( n) => bufs = IoSlice :: advance ( bufs, n) ,
1574
+ Ok ( n) => IoSlice :: advance_slices ( & mut bufs, n) ,
1524
1575
Err ( ref e) if e. kind ( ) == ErrorKind :: Interrupted => { }
1525
1576
Err ( e) => return Err ( e) ,
1526
1577
}
0 commit comments