Asynchronously write to the output stream. This method is the same as [`AsyncWrite::write`], but doesn't require a `&mut self`. Awaits for write readiness, and then performs at most one write to the output stream. Returns how much of the argument `buf` was written, or a `std::io::Error` indicating either an error returned by the stream write using the debug string provided by the WASI error, or e
(&self, buf: &[u8])
| 250 | /// using the debug string provided by the WASI error, or else that the, |
| 251 | /// indicated by `std::io::ErrorKind::ConnectionReset`. |
| 252 | pub async fn write(&self, buf: &[u8]) -> std::io::Result<usize> { |
| 253 | // Loops at most twice. |
| 254 | loop { |
| 255 | match self.stream.check_write() { |
| 256 | Ok(0) => { |
| 257 | self.ready().await; |
| 258 | // Next loop guaranteed to have nonzero check_write, or error. |
| 259 | continue; |
| 260 | } |
| 261 | Ok(some) => { |
| 262 | let writable = some.try_into().unwrap_or(usize::MAX).min(buf.len()); |
| 263 | match self.stream.write(&buf[0..writable]) { |
| 264 | Ok(()) => return Ok(writable), |
| 265 | Err(StreamError::Closed) => { |
| 266 | return Err(std::io::Error::from(std::io::ErrorKind::ConnectionReset)); |
| 267 | } |
| 268 | Err(StreamError::LastOperationFailed(err)) => { |
| 269 | return Err(std::io::Error::other(err.to_debug_string())); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | Err(StreamError::Closed) => { |
| 274 | return Err(std::io::Error::from(std::io::ErrorKind::ConnectionReset)); |
| 275 | } |
| 276 | Err(StreamError::LastOperationFailed(err)) => { |
| 277 | return Err(std::io::Error::other(err.to_debug_string())); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /// Asynchronously write to the output stream. This method is the same as |
| 284 | /// [`AsyncWrite::write_all`], but doesn't require a `&mut self`. |