Skip to content

Commit

Permalink
add AsyncFd::async_io_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Apr 8, 2023
1 parent 57f8151 commit 58452b5
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions tokio/src/io/async_fd.rs
Expand Up @@ -559,7 +559,7 @@ impl<T: AsRawFd> AsyncFd<T> {
/// 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");
Expand Down Expand Up @@ -596,9 +596,27 @@ impl<T: AsRawFd> AsyncFd<T> {
pub async fn async_io<R>(
&self,
interest: Interest,
f: impl FnMut() -> io::Result<R>,
mut f: impl FnMut(&T) -> io::Result<R>,
) -> io::Result<R> {
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<R>(
&mut self,
interest: Interest,
mut f: impl FnMut(&mut T) -> io::Result<R>,
) -> io::Result<R> {
self.registration
.async_io(interest, || f(self.inner.as_mut().unwrap()))
.await
}
}

Expand Down

0 comments on commit 58452b5

Please sign in to comment.