(
&self,
memory: &mut GuestMemory<'_>,
host: &mut impl streams::HostOutputStream,
output_stream: Resource<streams::OutputStream>,
bytes: GuestPtr<[u8]>,
)
| 289 | } |
| 290 | } |
| 291 | async fn write( |
| 292 | &self, |
| 293 | memory: &mut GuestMemory<'_>, |
| 294 | host: &mut impl streams::HostOutputStream, |
| 295 | output_stream: Resource<streams::OutputStream>, |
| 296 | bytes: GuestPtr<[u8]>, |
| 297 | ) -> StreamResult<usize> { |
| 298 | use streams::HostOutputStream as Streams; |
| 299 | |
| 300 | let bytes = memory |
| 301 | .as_cow(bytes) |
| 302 | .map_err(|e| StreamError::Trap(e.into()))?; |
| 303 | let mut bytes = &bytes[..]; |
| 304 | |
| 305 | let total = bytes.len(); |
| 306 | while !bytes.is_empty() { |
| 307 | // NOTE: blocking_write_and_flush takes at most one 4k buffer. |
| 308 | let len = bytes.len().min(4096); |
| 309 | let (chunk, rest) = bytes.split_at(len); |
| 310 | bytes = rest; |
| 311 | |
| 312 | Streams::blocking_write_and_flush(host, output_stream.borrowed(), Vec::from(chunk)) |
| 313 | .await? |
| 314 | } |
| 315 | |
| 316 | Ok(total) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | #[derive(Debug)] |
no test coverage detected