From b42bcb9823a08990dbf1cfe9064a67143e818798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20B=C3=A4chler?= Date: Sat, 22 May 2021 13:06:34 +0200 Subject: [PATCH 1/3] io: add get_{ref,mut} methods to AsyncFdReadyGuard and AsyncFdReadyMutGuard. The only way to use AsyncFdReadyMutGuard currently is through try_io, since it is impossible to access the underlying object. In some situations, it is desirable to use clear_ready and retain_ready directly instead of try_io. Enable this by adding get_ref and get_mut methods. --- tokio/src/io/async_fd.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index 5a68d307665..5638212bb27 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -480,6 +480,11 @@ impl Drop for AsyncFd { } impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> { + /// Returns a shared reference to the inner [`AsyncFd`]. + pub fn get_ref(&self) -> &AsyncFd { + &self.async_fd + } + /// Indicates to tokio that the file descriptor is no longer ready. The /// internal readiness flag will be cleared, and tokio will wait for the /// next edge-triggered readiness notification from the OS. @@ -543,6 +548,16 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> { } impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> { + /// Returns a shared reference to the inner [`AsyncFd`]. + pub fn get_ref(&self) -> &AsyncFd { + &self.async_fd + } + + /// Returns a mutable reference to the inner [`AsyncFd`]. + pub fn get_mut(&mut self) -> &mut AsyncFd { + &mut self.async_fd + } + /// Indicates to tokio that the file descriptor is no longer ready. The /// internal readiness flag will be cleared, and tokio will wait for the /// next edge-triggered readiness notification from the OS. From 9465943964896f9173cb3ebebeb681de7a19c62e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20B=C3=A4chler?= Date: Wed, 26 May 2021 15:05:49 +0200 Subject: [PATCH 2/3] io: add get_inner{,_mut} methods to AsyncFdReadyGuard and AsyncFdReadyMutGuard. --- tokio/src/io/async_fd.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index 5638212bb27..6f37178160d 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -482,7 +482,12 @@ impl Drop for AsyncFd { impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> { /// Returns a shared reference to the inner [`AsyncFd`]. pub fn get_ref(&self) -> &AsyncFd { - &self.async_fd + self.async_fd + } + + /// Returns a shared reference to the backing object of the inner [`AsyncFd`]. + pub fn get_inner(&self) -> &Inner { + self.get_ref().get_ref() } /// Indicates to tokio that the file descriptor is no longer ready. The @@ -550,12 +555,22 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> { impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> { /// Returns a shared reference to the inner [`AsyncFd`]. pub fn get_ref(&self) -> &AsyncFd { - &self.async_fd + self.async_fd } /// Returns a mutable reference to the inner [`AsyncFd`]. pub fn get_mut(&mut self) -> &mut AsyncFd { - &mut self.async_fd + self.async_fd + } + + /// Returns a shared reference to the backing object of the inner [`AsyncFd`]. + pub fn get_inner(&self) -> &Inner { + self.get_ref().get_ref() + } + + /// Returns a mutable reference to the backing object of the inner [`AsyncFd`]. + pub fn get_inner_mut(&mut self) -> &mut Inner { + self.get_mut().get_mut() } /// Indicates to tokio that the file descriptor is no longer ready. The From 641149d76c8baf150852e75946d7a55282471248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20B=C3=A4chler?= Date: Wed, 26 May 2021 23:33:21 +0200 Subject: [PATCH 3/3] io: move {AsyncFdReadyGuard,AsyncFdReadyMutGuard}::get_* to the bottom of the impl block. --- tokio/src/io/async_fd.rs | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index 6f37178160d..fa5bec530fa 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -480,16 +480,6 @@ impl Drop for AsyncFd { } impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> { - /// Returns a shared reference to the inner [`AsyncFd`]. - pub fn get_ref(&self) -> &AsyncFd { - self.async_fd - } - - /// Returns a shared reference to the backing object of the inner [`AsyncFd`]. - pub fn get_inner(&self) -> &Inner { - self.get_ref().get_ref() - } - /// Indicates to tokio that the file descriptor is no longer ready. The /// internal readiness flag will be cleared, and tokio will wait for the /// next edge-triggered readiness notification from the OS. @@ -550,29 +540,19 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> { result => Ok(result), } } -} -impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> { /// Returns a shared reference to the inner [`AsyncFd`]. pub fn get_ref(&self) -> &AsyncFd { self.async_fd } - /// Returns a mutable reference to the inner [`AsyncFd`]. - pub fn get_mut(&mut self) -> &mut AsyncFd { - self.async_fd - } - /// Returns a shared reference to the backing object of the inner [`AsyncFd`]. pub fn get_inner(&self) -> &Inner { self.get_ref().get_ref() } +} - /// Returns a mutable reference to the backing object of the inner [`AsyncFd`]. - pub fn get_inner_mut(&mut self) -> &mut Inner { - self.get_mut().get_mut() - } - +impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> { /// Indicates to tokio that the file descriptor is no longer ready. The /// internal readiness flag will be cleared, and tokio will wait for the /// next edge-triggered readiness notification from the OS. @@ -631,6 +611,26 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> { result => Ok(result), } } + + /// Returns a shared reference to the inner [`AsyncFd`]. + pub fn get_ref(&self) -> &AsyncFd { + self.async_fd + } + + /// Returns a mutable reference to the inner [`AsyncFd`]. + pub fn get_mut(&mut self) -> &mut AsyncFd { + self.async_fd + } + + /// Returns a shared reference to the backing object of the inner [`AsyncFd`]. + pub fn get_inner(&self) -> &Inner { + self.get_ref().get_ref() + } + + /// Returns a mutable reference to the backing object of the inner [`AsyncFd`]. + pub fn get_inner_mut(&mut self) -> &mut Inner { + self.get_mut().get_mut() + } } impl<'a, T: std::fmt::Debug + AsRawFd> std::fmt::Debug for AsyncFdReadyGuard<'a, T> {