Repository: Thomasdezeeuw/a10 (rust)

Issue:
Move SubmissionQueue::{oneshot,multishot}_poll to stand alone functions
Removing them from SubmissionQueue and moving them into the poll module.

Pull request description:
Move poll functions
Closes #99

Patch:
diff --git a/src/lib.rs b/src/lib.rs
index e97fb74..1081110 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -535,13 +535,12 @@ impl SubmissionQueue {
 
     /// Wait for an event specified in `mask` on the file descriptor `fd`.
     ///
-    /// Ths is similar to calling `poll(2)` the file descriptor.
-    #[doc(alias = "poll")]
-    #[doc(alias = "epoll")]
-    #[doc(alias = "select")]
-    #[allow(clippy::cast_sign_loss)]
-    pub fn oneshot_poll<'a>(&'a self, fd: BorrowedFd, mask: libc::c_int) -> OneshotPoll<'a> {
-        OneshotPoll::new(self, fd.as_raw_fd(), mask as u32)
+    /// Ths is similar to calling `poll(2)` on the file descriptor.
+    ///
+    /// This is deprecated, use [`poll::oneshot_poll`] instead.
+    #[deprecated(note = "use a10::poll::oneshot_poll instead")]
+    pub fn oneshot_poll<'sq>(&'sq self, fd: BorrowedFd, mask: libc::c_int) -> OneshotPoll<'sq> {
+        poll::oneshot_poll(self, fd, mask)
     }
 
     /// Returns an [`AsyncIterator`] that returns multiple events as specified
@@ -551,10 +550,12 @@ impl SubmissionQueue {
     /// loop as this uses a multishot operation, which means only a single
     /// operation is created kernel side, making this more efficient.
     ///
+    /// This is deprecated, use [`poll::multishot_poll`] instead.
+    ///
     /// [`AsyncIterator`]: std::async_iter::AsyncIterator
-    #[allow(clippy::cast_sign_loss)]
+    #[deprecated(note = "use a10::poll::multishot_poll instead")]
     pub fn multishot_poll<'a>(&'a self, fd: BorrowedFd, mask: libc::c_int) -> MultishotPoll<'a> {
-        MultishotPoll::new(self, fd.as_raw_fd(), mask as u32)
+        poll::multishot_poll(self, fd, mask)
     }
 
     /// Add a submission to the queue.
diff --git a/src/poll.rs b/src/poll.rs
index ac0bb13..861e0d7 100644
--- a/src/poll.rs
+++ b/src/poll.rs
@@ -9,7 +9,7 @@
 //! [`AsyncIterator`]: std::async_iter::AsyncIterator
 
 use std::future::Future;
-use std::os::fd::RawFd;
+use std::os::fd::{AsRawFd, BorrowedFd, RawFd};
 use std::pin::Pin;
 use std::task::{self, Poll};
 use std::{fmt, io};
@@ -18,28 +18,37 @@ use crate::cancel::{Cancel, CancelOp, CancelResult};
 use crate::op::{poll_state, OpState};
 use crate::{QueueFull, SubmissionQueue};
 
+/// Wait for an event specified in `mask` on the file descriptor `fd`.
+///
+/// Ths is similar to calling `poll(2)` on the file descriptor.
+#[doc(alias = "poll")]
+#[doc(alias = "epoll")]
+#[doc(alias = "select")]
+#[allow(clippy::module_name_repetitions)]
+pub fn oneshot_poll<'sq>(
+    sq: &'sq SubmissionQueue,
+    fd: BorrowedFd,
+    mask: libc::c_int,
+) -> OneshotPoll<'sq> {
+    OneshotPoll {
+        sq,
+        state: OpState::NotStarted((fd.as_raw_fd(), mask)),
+    }
+}
+
 /// [`Future`] behind [`SubmissionQueue::oneshot_poll`].
 #[derive(Debug)]
 #[must_use = "`Future`s do nothing unless polled"]
 #[allow(clippy::module_name_repetitions)]
-pub struct OneshotPoll<'a> {
-    sq: &'a SubmissionQueue,
-    state: OpState<(RawFd, u32)>,
+pub struct OneshotPoll<'sq> {
+    sq: &'sq SubmissionQueue,
+    state: OpState<(RawFd, libc::c_int)>,
 }
 
-impl<'a> OneshotPoll<'a> {
-    /// Create a new `OneshotPoll`.
-    pub(crate) const fn new(sq: &'a SubmissionQueue, fd: RawFd, mask: u32) -> OneshotPoll {
-        OneshotPoll {
-            sq,
-            state: OpState::NotStarted((fd, mask)),
-        }
-    }
-}
-
-impl<'a> Future for OneshotPoll<'a> {
+impl<'sq> Future for OneshotPoll<'sq> {
     type Output = io::Result<PollEvent>;
 
+    #[allow(clippy::cast_sign_loss)]
     fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> Poll<Self::Output> {
         let op_index = poll_state!(
             OneshotPoll,
@@ -47,7 +56,7 @@ impl<'a> Future for OneshotPoll<'a> {
             self.sq,
             ctx,
             |submission, (fd, mask)| unsafe {
-                submission.poll(fd, mask);
+                submission.poll(fd, mask as u32);
             }
         );
 
@@ -64,7 +73,7 @@ impl<'a> Future for OneshotPoll<'a> {
     }
 }
 
-impl<'a> Cancel for OneshotPoll<'a> {
+impl<'sq> Cancel for OneshotPoll<'sq> {
     fn try_cancel(&mut self) -> CancelResult {
         self.state.try_cancel(self.sq)
     }
@@ -74,7 +83,7 @@ impl<'a> Cancel for OneshotPoll<'a> {
     }
 }
 
-impl<'a> Drop for OneshotPoll<'a> {
+impl<'sq> Drop for OneshotPoll<'sq> {
     fn drop(&mut self) {
         if let OpState::Running(op_index) = self.state {
             let result = self.sq.cancel_op(op_index, (), |submission| unsafe {
@@ -92,28 +101,41 @@ impl<'a> Drop for OneshotPoll<'a> {
     }
 }
 
+/// Returns an [`AsyncIterator`] that returns multiple events as specified
+/// in `mask` on the file descriptor `fd`.
+///
+/// This is not the same as calling [`SubmissionQueue::oneshot_poll`] in a
+/// loop as this uses a multishot operation, which means only a single
+/// operation is created kernel side, making this more efficient.
+///
+/// [`AsyncIterator`]: std::async_iter::AsyncIterator
+#[allow(clippy::module_name_repetitions)]
+pub fn multishot_poll<'sq>(
+    sq: &'sq SubmissionQueue,
+    fd: BorrowedFd,
+    mask: libc::c_int,
+) -> MultishotPoll<'sq> {
+    MultishotPoll {
+        sq,
+        state: OpState::NotStarted((fd.as_raw_fd(), mask)),
+    }
+}
+
 /// [`AsyncIterator`] behind [`SubmissionQueue::multishot_poll`].
 ///
 /// [`AsyncIterator`]: std::async_iter::AsyncIterator
 #[derive(Debug)]
 #[must_use = "`Future`s do nothing unless polled"]
 #[allow(clippy::module_name_repetitions)]
-pub struct MultishotPoll<'a> {
-    sq: &'a SubmissionQueue,
-    state: OpState<(RawFd, u32)>,
+pub struct MultishotPoll<'sq> {
+    sq: &'sq SubmissionQueue,
+    state: OpState<(RawFd, libc::c_int)>,
 }
 
-impl<'a> MultishotPoll<'a> {
-    /// Create a new `MultishotPoll`.
-    pub(crate) const fn new(sq: &'a SubmissionQueue, fd: RawFd, mask: u32) -> MultishotPoll {
-        MultishotPoll {
-            sq,
-            state: OpState::NotStarted((fd, mask)),
-        }
-    }
-
+impl<'sq> MultishotPoll<'sq> {
     /// This is the same as the `AsyncIterator::poll_next` function, but then
     /// available on stable Rust.
+    #[allow(clippy::cast_sign_loss)]
     pub fn poll_next(
         mut self: Pin<&mut Self>,
         ctx: &mut task::Context<'_>,
@@ -122,7 +144,7 @@ impl<'a> MultishotPoll<'a> {
             OpState::Running(op_index) => op_index,
             OpState::NotStarted((fd, mask)) => {
                 let result = self.sq.add_multishot(|submission| unsafe {
-                    submission.multishot_poll(fd, mask);
+                    submission.multishot_poll(fd, mask as u32);
                 });
                 match result {
                     Ok(op_index) => {
@@ -163,7 +185,7 @@ impl<'a> MultishotPoll<'a> {
 }
 
 #[cfg(feature = "nightly")]
-impl<'a> std::async_iter::AsyncIterator for MultishotPoll<'a> {
+impl<'sq> std::async_iter::AsyncIterator for MultishotPoll<'sq> {
     type Item = io::Result<PollEvent>;
 
     fn poll_next(self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
@@ -171,7 +193,7 @@ impl<'a> std::async_iter::AsyncIterator for MultishotPoll<'a> {
     }
 }
 
-impl<'a> Cancel for MultishotPoll<'a> {
+impl<'sq> Cancel for MultishotPoll<'sq> {
     fn try_cancel(&mut self) -> CancelResult {
         self.state.try_cancel(self.sq)
     }
@@ -181,7 +203,7 @@ impl<'a> Cancel for MultishotPoll<'a> {
     }
 }
 
-impl<'a> Drop for MultishotPoll<'a> {
+impl<'sq> Drop for MultishotPoll<'sq> {
     fn drop(&mut self) {
         if let OpState::Running(op_index) = self.state {
             let result = self.sq.cancel_op(op_index, (), |submission| unsafe {


Test patch:
diff --git a/tests/ring.rs b/tests/ring.rs
index f901ddb..d8aac82 100644
--- a/tests/ring.rs
+++ b/tests/ring.rs
@@ -19,7 +19,7 @@ use a10::cancel::Cancel;
 use a10::fs::OpenOptions;
 use a10::io::ReadBufPool;
 use a10::msg::{MsgListener, MsgToken, SendMsg};
-use a10::poll::{MultishotPoll, OneshotPoll};
+use a10::poll::{multishot_poll, oneshot_poll, MultishotPoll, OneshotPoll};
 use a10::{mem, process, AsyncFd, Config, Ring, SubmissionQueue};
 
 mod util;
@@ -294,7 +294,7 @@ fn message_sending() {
 }
 
 #[test]
-fn oneshot_poll() {
+fn test_oneshot_poll() {
     let sq = test_queue();
     let waker = Waker::new();
 
@@ -303,8 +303,8 @@ fn oneshot_poll() {
 
     let (mut receiver, mut sender) = pipe2().unwrap();
 
-    let sender_write = pin!(sq.oneshot_poll(sender.as_fd(), libc::POLLOUT as _));
-    let receiver_read = pin!(sq.oneshot_poll(receiver.as_fd(), libc::POLLIN as _));
+    let sender_write = pin!(oneshot_poll(&sq, sender.as_fd(), libc::POLLOUT as _));
+    let receiver_read = pin!(oneshot_poll(&sq, receiver.as_fd(), libc::POLLIN as _));
 
     let event = waker.block_on(sender_write).unwrap();
     assert!(event.is_writable());
@@ -324,7 +324,7 @@ fn drop_oneshot_poll() {
 
     let (receiver, sender) = pipe2().unwrap();
 
-    let mut receiver_read = sq.oneshot_poll(receiver.as_fd(), libc::POLLIN as _);
+    let mut receiver_read = oneshot_poll(&sq, receiver.as_fd(), libc::POLLIN as _);
 
     start_op(&mut receiver_read);
 
@@ -340,7 +340,7 @@ fn cancel_oneshot_poll() {
 
     let (receiver, sender) = pipe2().unwrap();
 
-    let mut receiver_read = pin!(sq.oneshot_poll(receiver.as_fd(), libc::POLLIN as _));
+    let mut receiver_read = pin!(oneshot_poll(&sq, receiver.as_fd(), libc::POLLIN as _));
     start_op(&mut receiver_read);
 
     waker.block_on(receiver_read.cancel()).unwrap();
@@ -349,7 +349,7 @@ fn cancel_oneshot_poll() {
 }
 
 #[test]
-fn multishot_poll() {
+fn test_multishot_poll() {
     let sq = test_queue();
     let waker = Waker::new();
 
@@ -358,7 +358,7 @@ fn multishot_poll() {
 
     let (mut receiver, mut sender) = pipe2().unwrap();
 
-    let mut receiver_read = pin!(sq.multishot_poll(receiver.as_fd(), libc::POLLIN as _));
+    let mut receiver_read = pin!(multishot_poll(&sq, receiver.as_fd(), libc::POLLIN as _));
     start_mulitshot_op(Pin::new(&mut receiver_read));
 
     let mut buf = vec![0; DATA.len() + 1];
@@ -384,7 +384,7 @@ fn cancel_multishot_poll() {
 
     let (receiver, sender) = pipe2().unwrap();
 
-    let mut receiver_read = pin!(sq.multishot_poll(receiver.as_fd(), libc::POLLIN as _));
+    let mut receiver_read = pin!(multishot_poll(&sq, receiver.as_fd(), libc::POLLIN as _));
     start_mulitshot_op(receiver_read.as_mut());
 
     waker.block_on(receiver_read.cancel()).unwrap();
@@ -398,7 +398,7 @@ fn drop_multishot_poll() {
 
     let (receiver, sender) = pipe2().unwrap();
 
-    let mut receiver_read = sq.multishot_poll(receiver.as_fd(), libc::POLLIN as _);
+    let mut receiver_read = multishot_poll(&sq, receiver.as_fd(), libc::POLLIN as _);
 
     start_mulitshot_op(Pin::new(&mut receiver_read));