TcpStream: add send_slice()

sync
Astro 2020-06-18 01:56:49 +02:00
parent a80a2c67ef
commit b4bcc6cf5c
1 changed files with 18 additions and 0 deletions

View File

@ -200,6 +200,24 @@ impl TcpStream {
Ok(())
}
/// Yields to wait for more buffer space
pub async fn send_slice(&self, mut data: &'_ [u8]) -> Result<()> {
while data.len() > 0 {
self.wait_can_send().await?;
data = self.with_socket(|mut socket| {
socket.send(|buf| {
let len = buf.len().min(data.len());
buf[..len].copy_from_slice(&data[..len]);
data = &data[len..];
(len, data)
})
})?;
}
Ok(())
}
/// Wait for all queued data to be sent and ACKed
///
/// **Warning:** this may not work as immediately as expected! The