diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index 65b02959571..1023aae6d56 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -559,7 +559,7 @@ impl AsyncFd { /// let async_fd = AsyncFd::new(socket)?; /// /// let written = async_fd - /// .async_io(Interest::WRITABLE, || async_fd.get_ref().send(&[1, 2])) + /// .async_io(Interest::WRITABLE, |inner| inner.send(&[1, 2])) /// .await?; /// /// println!("wrote {written} bytes"); @@ -596,9 +596,27 @@ impl AsyncFd { pub async fn async_io( &self, interest: Interest, - f: impl FnMut() -> io::Result, + mut f: impl FnMut(&T) -> io::Result, ) -> io::Result { - self.registration.async_io(interest, f).await + self.registration + .async_io(interest, || f(self.get_ref())) + .await + } + + /// Reads or writes from the file descriptor using a user-provided IO operation. + /// + /// The behavior is the same as [`async_io`], except that the closure can mutate the inner + /// value of the [`AsyncFd`]. + /// + /// [`async_io`]: AsyncFd::async_io + pub async fn async_io_mut( + &mut self, + interest: Interest, + mut f: impl FnMut(&mut T) -> io::Result, + ) -> io::Result { + self.registration + .async_io(interest, || f(self.inner.as_mut().unwrap())) + .await } }