use std::io::{self, IoSlice};
pub fn write_all_vectored<W: io::Write>(
writer: &mut W,
mut slices: &mut [IoSlice<'_>],
) -> io::Result<()> {
IoSlice::advance_slices(&mut slices, 0);
while !slices.is_empty() {
match writer.write_vectored(slices) {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to write whole buffer",
));
}
Ok(n) => IoSlice::advance_slices(&mut slices, n),
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(())
}
#[cfg(feature = "async")]
pub async fn write_all_vectored_async<W: tokio::io::AsyncWriteExt + Unpin>(
writer: &mut W,
mut slices: &mut [IoSlice<'_>],
) -> io::Result<()> {
IoSlice::advance_slices(&mut slices, 0);
while !slices.is_empty() {
match writer.write_vectored(slices).await {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to write whole buffer",
));
}
Ok(n) => IoSlice::advance_slices(&mut slices, n),
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(())
}