|
251 | 251 |
|
252 | 252 | use crate::cmp; |
253 | 253 | use crate::fmt; |
254 | | -use crate::mem; |
255 | 254 | use crate::memchr; |
256 | 255 | use crate::ops::{Deref, DerefMut}; |
257 | 256 | use crate::ptr; |
@@ -1435,12 +1434,15 @@ pub trait Write { |
1435 | 1434 | /// ``` |
1436 | 1435 | #[unstable(feature = "write_all_vectored", issue = "70436")] |
1437 | 1436 | fn write_all_vectored(&mut self, mut bufs: &mut [IoSlice<'_>]) -> Result<()> { |
| 1437 | + // Guarantee that bufs is empty if it contains no data, |
| 1438 | + // to avoid calling write_vectored if there is no data to be written. |
| 1439 | + bufs = IoSlice::advance(bufs, 0); |
1438 | 1440 | while !bufs.is_empty() { |
1439 | 1441 | match self.write_vectored(bufs) { |
1440 | 1442 | Ok(0) => { |
1441 | 1443 | return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer")); |
1442 | 1444 | } |
1443 | | - Ok(n) => bufs = IoSlice::advance(mem::take(&mut bufs), n), |
| 1445 | + Ok(n) => bufs = IoSlice::advance(bufs, n), |
1444 | 1446 | Err(ref e) if e.kind() == ErrorKind::Interrupted => {} |
1445 | 1447 | Err(e) => return Err(e), |
1446 | 1448 | } |
@@ -2958,6 +2960,7 @@ mod tests { |
2958 | 2960 | #[rustfmt::skip] // Becomes unreadable otherwise. |
2959 | 2961 | let tests: Vec<(_, &'static [u8])> = vec![ |
2960 | 2962 | (vec![], &[]), |
| 2963 | + (vec![IoSlice::new(&[]), IoSlice::new(&[])], &[]), |
2961 | 2964 | (vec![IoSlice::new(&[1])], &[1]), |
2962 | 2965 | (vec![IoSlice::new(&[1, 2])], &[1, 2]), |
2963 | 2966 | (vec![IoSlice::new(&[1, 2, 3])], &[1, 2, 3]), |
|
0 commit comments