1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
//! Asynchronous I/O //! //! This crate contains the `AsyncRead` and `AsyncWrite` traits, the //! asynchronous analogs to `std::io::{Read, Write}`. The primary difference is //! that these traits integrate with the asynchronous task system. #![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)] #![warn(clippy::all)] #![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.16/futures_io")] #[cfg(feature = "std")] mod if_std { use futures_core::task::{Context, Poll}; use std::boxed::Box; use std::cmp; use std::io as StdIo; use std::ops::DerefMut; use std::pin::Pin; use std::ptr; // Re-export some types from `std::io` so that users don't have to deal // with conflicts when `use`ing `futures::io` and `std::io`. pub use self::StdIo::Error as Error; pub use self::StdIo::ErrorKind as ErrorKind; pub use self::StdIo::Result as Result; pub use self::StdIo::IoSlice as IoSlice; pub use self::StdIo::IoSliceMut as IoSliceMut; pub use self::StdIo::SeekFrom as SeekFrom; /// A type used to conditionally initialize buffers passed to `AsyncRead` /// methods, modeled after `std`. #[derive(Debug)] pub struct Initializer(bool); impl Initializer { /// Returns a new `Initializer` which will zero out buffers. #[inline] pub fn zeroing() -> Initializer { Initializer(true) } /// Returns a new `Initializer` which will not zero out buffers. /// /// # Safety /// /// This method may only be called by `AsyncRead`ers which guarantee /// that they will not read from the buffers passed to `AsyncRead` /// methods, and that the return value of the method accurately reflects /// the number of bytes that have been written to the head of the buffer. #[inline] pub unsafe fn nop() -> Initializer { Initializer(false) } /// Indicates if a buffer should be initialized. #[inline] pub fn should_initialize(&self) -> bool { self.0 } /// Initializes a buffer if necessary. #[inline] pub fn initialize(&self, buf: &mut [u8]) { if self.should_initialize() { unsafe { ptr::write_bytes(buf.as_mut_ptr(), 0, buf.len()) } } } } /// Read bytes asynchronously. /// /// This trait is analogous to the `std::io::Read` trait, but integrates /// with the asynchronous task system. In particular, the `poll_read` /// method, unlike `Read::read`, will automatically queue the current task /// for wakeup and return if data is not yet available, rather than blocking /// the calling thread. pub trait AsyncRead { /// Determines if this `AsyncRead`er can work with buffers of /// uninitialized memory. /// /// The default implementation returns an initializer which will zero /// buffers. /// /// # Safety /// /// This method is `unsafe` because and `AsyncRead`er could otherwise /// return a non-zeroing `Initializer` from another `AsyncRead` type /// without an `unsafe` block. #[inline] unsafe fn initializer(&self) -> Initializer { Initializer::zeroing() } /// Attempt to read from the `AsyncRead` into `buf`. /// /// On success, returns `Poll::Ready(Ok(num_bytes_read))`. /// /// If no data is available for reading, the method returns /// `Poll::Pending` and arranges for the current task (via /// `cx.waker().wake_by_ref()`) to receive a notification when the object becomes /// readable or is closed. /// /// # Implementation /// /// This function may not return errors of kind `WouldBlock` or /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>>; /// Attempt to read from the `AsyncRead` into `bufs` using vectored /// IO operations. /// /// This method is similar to `poll_read`, but allows data to be read /// into multiple buffers using a single operation. /// /// On success, returns `Poll::Ready(Ok(num_bytes_read))`. /// /// If no data is available for reading, the method returns /// `Poll::Pending` and arranges for the current task (via /// `cx.waker().wake_by_ref()`) to receive a notification when the object becomes /// readable or is closed. /// By default, this method delegates to using `poll_read` on the first /// buffer in `bufs`. Objects which support vectored IO should override /// this method. /// /// # Implementation /// /// This function may not return errors of kind `WouldBlock` or /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) -> Poll<Result<usize>> { if let Some(ref mut first_iovec) = vec.get_mut(0) { self.poll_read(cx, first_iovec) } else { // `vec` is empty. Poll::Ready(Ok(0)) } } } /// Write bytes asynchronously. /// /// This trait is analogous to the `std::io::Write` trait, but integrates /// with the asynchronous task system. In particular, the `poll_write` /// method, unlike `Write::write`, will automatically queue the current task /// for wakeup and return if data is not yet available, rather than blocking /// the calling thread. pub trait AsyncWrite { /// Attempt to write bytes from `buf` into the object. /// /// On success, returns `Poll::Ready(Ok(num_bytes_written))`. /// /// If the object is not ready for writing, the method returns /// `Poll::Pending` and arranges for the current task (via /// `cx.waker().wake_by_ref()`) to receive a notification when the object becomes /// readable or is closed. /// /// # Implementation /// /// This function may not return errors of kind `WouldBlock` or /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>; /// Attempt to write bytes from `bufs` into the object using vectored /// IO operations. /// /// This method is similar to `poll_write`, but allows data from multiple buffers to be written /// using a single operation. /// /// On success, returns `Poll::Ready(Ok(num_bytes_written))`. /// /// If the object is not ready for writing, the method returns /// `Poll::Pending` and arranges for the current task (via /// `cx.waker().wake_by_ref()`) to receive a notification when the object becomes /// readable or is closed. /// /// By default, this method delegates to using `poll_write` on the first /// buffer in `bufs`. Objects which support vectored IO should override /// this method. /// /// # Implementation /// /// This function may not return errors of kind `WouldBlock` or /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>> { if let Some(ref first_iovec) = bufs.get(0) { self.poll_write(cx, &*first_iovec) } else { // `bufs` is empty. Poll::Ready(Ok(0)) } } /// Attempt to flush the object, ensuring that any buffered data reach /// their destination. /// /// On success, returns `Poll::Ready(Ok(()))`. /// /// If flushing cannot immediately complete, this method returns /// `Poll::Pending` and arranges for the current task (via /// `cx.waker().wake_by_ref()`) to receive a notification when the object can make /// progress towards flushing. /// /// # Implementation /// /// This function may not return errors of kind `WouldBlock` or /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>; /// Attempt to close the object. /// /// On success, returns `Poll::Ready(Ok(()))`. /// /// If closing cannot immediately complete, this function returns /// `Poll::Pending` and arranges for the current task (via /// `cx.waker().wake_by_ref()`) to receive a notification when the object can make /// progress towards closing. /// /// # Implementation /// /// This function may not return errors of kind `WouldBlock` or /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>; } /// Seek bytes asynchronously. /// /// This trait is analogous to the `std::io::Seek` trait, but integrates /// with the asynchronous task system. In particular, the `poll_seek` /// method, unlike `Seek::seek`, will automatically queue the current task /// for wakeup and return if data is not yet available, rather than blocking /// the calling thread. pub trait AsyncSeek { /// Attempt to seek to an offset, in bytes, in a stream. /// /// A seek beyond the end of a stream is allowed, but behavior is defined /// by the implementation. /// /// If the seek operation completed successfully, /// this method returns the new position from the start of the stream. /// That position can be used later with [`SeekFrom::Start`]. /// /// # Errors /// /// Seeking to a negative offset is considered an error. fn poll_seek(self: Pin<&mut Self>, cx: &mut Context<'_>, pos: SeekFrom) -> Poll<Result<u64>>; } /// Read bytes asynchronously. /// /// This trait is analogous to the `std::io::BufRead` trait, but integrates /// with the asynchronous task system. In particular, the `poll_fill_buf` /// method, unlike `BufRead::fill_buf`, will automatically queue the current task /// for wakeup and return if data is not yet available, rather than blocking /// the calling thread. pub trait AsyncBufRead: AsyncRead { /// Attempt to return the contents of the internal buffer, filling it with more data /// from the inner reader if it is empty. /// /// On success, returns `Poll::Ready(Ok(buf))`. /// /// If no data is available for reading, the method returns /// `Poll::Pending` and arranges for the current task (via /// `cx.waker().wake_by_ref()`) to receive a notification when the object becomes /// readable or is closed. /// /// This function is a lower-level call. It needs to be paired with the /// [`consume`] method to function properly. When calling this /// method, none of the contents will be "read" in the sense that later /// calling [`poll_read`] may return the same contents. As such, [`consume`] must /// be called with the number of bytes that are consumed from this buffer to /// ensure that the bytes are never returned twice. /// /// [`poll_read`]: AsyncRead::poll_read /// [`consume`]: AsyncBufRead::consume /// /// An empty buffer returned indicates that the stream has reached EOF. /// /// # Implementation /// /// This function may not return errors of kind `WouldBlock` or /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. fn poll_fill_buf<'a>(self: Pin<&'a mut Self>, cx: &mut Context<'_>) -> Poll<Result<&'a [u8]>>; /// Tells this buffer that `amt` bytes have been consumed from the buffer, /// so they should no longer be returned in calls to [`poll_read`]. /// /// This function is a lower-level call. It needs to be paired with the /// [`poll_fill_buf`] method to function properly. This function does /// not perform any I/O, it simply informs this object that some amount of /// its buffer, returned from [`poll_fill_buf`], has been consumed and should /// no longer be returned. As such, this function may do odd things if /// [`poll_fill_buf`] isn't called before calling it. /// /// The `amt` must be `<=` the number of bytes in the buffer returned by /// [`poll_fill_buf`]. /// /// [`poll_read`]: AsyncRead::poll_read /// [`poll_fill_buf`]: AsyncBufRead::poll_fill_buf fn consume(self: Pin<&mut Self>, amt: usize); } macro_rules! deref_async_read { () => { unsafe fn initializer(&self) -> Initializer { (**self).initializer() } fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>> { Pin::new(&mut **self).poll_read(cx, buf) } fn poll_read_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) -> Poll<Result<usize>> { Pin::new(&mut **self).poll_read_vectored(cx, vec) } } } impl<T: ?Sized + AsyncRead + Unpin> AsyncRead for Box<T> { deref_async_read!(); } impl<T: ?Sized + AsyncRead + Unpin> AsyncRead for &mut T { deref_async_read!(); } impl<P> AsyncRead for Pin<P> where P: DerefMut + Unpin, P::Target: AsyncRead, { unsafe fn initializer(&self) -> Initializer { (**self).initializer() } fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>> { Pin::get_mut(self).as_mut().poll_read(cx, buf) } fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) -> Poll<Result<usize>> { Pin::get_mut(self).as_mut().poll_read_vectored(cx, vec) } } /// `unsafe` because the `StdIo::Read` type must not access the buffer /// before reading data into it. macro_rules! unsafe_delegate_async_read_to_stdio { () => { unsafe fn initializer(&self) -> Initializer { Initializer::nop() } fn poll_read(mut self: Pin<&mut Self>, _: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>> { Poll::Ready(StdIo::Read::read(&mut *self, buf)) } fn poll_read_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) -> Poll<Result<usize>> { Poll::Ready(StdIo::Read::read_vectored(&mut *self, vec)) } } } impl AsyncRead for &[u8] { unsafe_delegate_async_read_to_stdio!(); } impl AsyncRead for StdIo::Repeat { unsafe_delegate_async_read_to_stdio!(); } impl AsyncRead for StdIo::Empty { unsafe_delegate_async_read_to_stdio!(); } impl<T: AsRef<[u8]> + Unpin> AsyncRead for StdIo::Cursor<T> { unsafe_delegate_async_read_to_stdio!(); } macro_rules! deref_async_write { () => { fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { Pin::new(&mut **self).poll_write(cx, buf) } fn poll_write_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>> { Pin::new(&mut **self).poll_write_vectored(cx, bufs) } fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { Pin::new(&mut **self).poll_flush(cx) } fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { Pin::new(&mut **self).poll_close(cx) } } } impl<T: ?Sized + AsyncWrite + Unpin> AsyncWrite for Box<T> { deref_async_write!(); } impl<T: ?Sized + AsyncWrite + Unpin> AsyncWrite for &mut T { deref_async_write!(); } impl<P> AsyncWrite for Pin<P> where P: DerefMut + Unpin, P::Target: AsyncWrite, { fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { Pin::get_mut(self).as_mut().poll_write(cx, buf) } fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>> { Pin::get_mut(self).as_mut().poll_write_vectored(cx, bufs) } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { Pin::get_mut(self).as_mut().poll_flush(cx) } fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { Pin::get_mut(self).as_mut().poll_close(cx) } } macro_rules! delegate_async_write_to_stdio { () => { fn poll_write(mut self: Pin<&mut Self>, _: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { Poll::Ready(StdIo::Write::write(&mut *self, buf)) } fn poll_write_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>> { Poll::Ready(StdIo::Write::write_vectored(&mut *self, bufs)) } fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>> { Poll::Ready(StdIo::Write::flush(&mut *self)) } fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { self.poll_flush(cx) } } } impl<T: AsMut<[u8]> + Unpin> AsyncWrite for StdIo::Cursor<T> { fn poll_write( mut self: Pin<&mut Self>, _: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>> { let position = self.position(); let result = { let out = (&mut *self).get_mut().as_mut(); let pos = cmp::min(out.len() as u64, position) as usize; StdIo::Write::write(&mut &mut out[pos..], buf) }; if let Ok(offset) = result { self.get_mut().set_position(position + offset as u64); } Poll::Ready(result) } fn poll_write_vectored(self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>> { Poll::Ready(StdIo::Write::write_vectored(&mut self.get_mut().get_mut().as_mut(), bufs)) } fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>> { Poll::Ready(StdIo::Write::flush(&mut self.get_mut().get_mut().as_mut())) } fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { self.poll_flush(cx) } } impl AsyncWrite for Vec<u8> { delegate_async_write_to_stdio!(); } impl AsyncWrite for StdIo::Sink { delegate_async_write_to_stdio!(); } macro_rules! deref_async_seek { () => { fn poll_seek(mut self: Pin<&mut Self>, cx: &mut Context<'_>, pos: SeekFrom) -> Poll<Result<u64>> { Pin::new(&mut **self).poll_seek(cx, pos) } } } impl<T: ?Sized + AsyncSeek + Unpin> AsyncSeek for Box<T> { deref_async_seek!(); } impl<T: ?Sized + AsyncSeek + Unpin> AsyncSeek for &mut T { deref_async_seek!(); } impl<P> AsyncSeek for Pin<P> where P: DerefMut + Unpin, P::Target: AsyncSeek, { fn poll_seek(self: Pin<&mut Self>, cx: &mut Context<'_>, pos: SeekFrom) -> Poll<Result<u64>> { self.get_mut().as_mut().poll_seek(cx, pos) } } macro_rules! delegate_async_seek_to_stdio { () => { fn poll_seek(mut self: Pin<&mut Self>, _: &mut Context<'_>, pos: SeekFrom) -> Poll<Result<u64>> { Poll::Ready(StdIo::Seek::seek(&mut *self, pos)) } } } impl<T: AsRef<[u8]> + Unpin> AsyncSeek for StdIo::Cursor<T> { delegate_async_seek_to_stdio!(); } macro_rules! deref_async_buf_read { () => { fn poll_fill_buf<'a>(self: Pin<&'a mut Self>, cx: &mut Context<'_>) -> Poll<Result<&'a [u8]>> { Pin::new(&mut **self.get_mut()).poll_fill_buf(cx) } fn consume(self: Pin<&mut Self>, amt: usize) { Pin::new(&mut **self.get_mut()).consume(amt) } } } impl<T: ?Sized + AsyncBufRead + Unpin> AsyncBufRead for Box<T> { deref_async_buf_read!(); } impl<T: ?Sized + AsyncBufRead + Unpin> AsyncBufRead for &mut T { deref_async_buf_read!(); } impl<P> AsyncBufRead for Pin<P> where P: DerefMut + Unpin, P::Target: AsyncBufRead, { fn poll_fill_buf<'a>(self: Pin<&'a mut Self>, cx: &mut Context<'_>) -> Poll<Result<&'a [u8]>> { self.get_mut().as_mut().poll_fill_buf(cx) } fn consume(self: Pin<&mut Self>, amt: usize) { self.get_mut().as_mut().consume(amt) } } macro_rules! delegate_async_buf_read_to_stdio { () => { fn poll_fill_buf<'a>(self: Pin<&'a mut Self>, _: &mut Context<'_>) -> Poll<Result<&'a [u8]>> { Poll::Ready(StdIo::BufRead::fill_buf(self.get_mut())) } fn consume(self: Pin<&mut Self>, amt: usize) { StdIo::BufRead::consume(self.get_mut(), amt) } } } impl AsyncBufRead for &[u8] { delegate_async_buf_read_to_stdio!(); } impl AsyncBufRead for StdIo::Empty { delegate_async_buf_read_to_stdio!(); } impl<T: AsRef<[u8]> + Unpin> AsyncBufRead for StdIo::Cursor<T> { delegate_async_buf_read_to_stdio!(); } } #[cfg(feature = "std")] pub use self::if_std::*;