Skip to content

Commit ad4fe67

Browse files
committed
refactor: merge some methods and remove OpFuture struct
merge pop and poll_task
1 parent 4eb4263 commit ad4fe67

File tree

5 files changed

+18
-79
lines changed

5 files changed

+18
-79
lines changed

compio-driver/src/lib.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ impl Proactor {
273273
}
274274

275275
/// Poll the driver and get completed entries.
276-
/// You need to call [`Proactor::pop`] to get the pushed operations.
276+
/// You need to call [`Proactor::pop`] to get the pushed
277+
/// operations.
277278
pub fn poll(
278279
&mut self,
279280
timeout: Option<Duration>,
@@ -290,25 +291,7 @@ impl Proactor {
290291
/// # Panics
291292
/// This function will panic if the requested operation has not been
292293
/// completed.
293-
pub fn pop<T>(&mut self, op: Key<T>) -> PushEntry<Key<T>, BufResult<usize, T>> {
294-
instrument!(compio_log::Level::DEBUG, "pop", ?op);
295-
if op.has_result() {
296-
// SAFETY: completed.
297-
PushEntry::Ready(unsafe { op.into_inner() })
298-
} else {
299-
PushEntry::Pending(op)
300-
}
301-
}
302-
303-
/// Get the pushed operations from the completion entries.
304-
///
305-
/// # Panics
306-
/// This function will panic if the requested operation has not been
307-
/// completed.
308-
pub fn pop_with_flags<T>(
309-
&mut self,
310-
op: Key<T>,
311-
) -> PushEntry<Key<T>, (BufResult<usize, T>, u32)> {
294+
pub fn pop<T>(&mut self, op: Key<T>) -> PushEntry<Key<T>, (BufResult<usize, T>, u32)> {
312295
instrument!(compio_log::Level::DEBUG, "pop_flags", ?op);
313296
if op.has_result() {
314297
let flags = op.flags();

compio-driver/tests/file.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ fn push_and_wait<O: OpCode + 'static>(driver: &mut Proactor, op: O) -> BufResult
5353
driver.poll(None, &mut entries).unwrap();
5454
}
5555
assert_eq!(entries[0], user_data.user_data());
56-
driver.pop(user_data).take_ready().unwrap()
56+
driver
57+
.pop(user_data)
58+
.map_ready(|(res, _)| res)
59+
.take_ready()
60+
.unwrap()
5761
}
5862
}
5963
}

compio-runtime/src/runtime/mod.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ use send_wrapper::SendWrapper;
3131

3232
#[cfg(feature = "time")]
3333
use crate::runtime::time::{TimerFuture, TimerRuntime};
34-
use crate::{
35-
runtime::op::{OpFlagsFuture, OpFuture},
36-
BufResult,
37-
};
34+
use crate::{runtime::op::OpFlagsFuture, BufResult};
3835

3936
scoped_tls::scoped_thread_local!(static CURRENT_RUNTIME: Runtime);
4037

@@ -238,10 +235,7 @@ impl Runtime {
238235
///
239236
/// You only need this when authoring your own [`OpCode`].
240237
pub fn submit<T: OpCode + 'static>(&self, op: T) -> impl Future<Output = BufResult<usize, T>> {
241-
match self.submit_raw(op) {
242-
PushEntry::Pending(user_data) => Either::Left(OpFuture::new(user_data)),
243-
PushEntry::Ready(res) => Either::Right(ready(res)),
244-
}
238+
self.submit_with_flags(op).map(|(res, _)| res)
245239
}
246240

247241
/// Submit an operation to the runtime.
@@ -287,7 +281,7 @@ impl Runtime {
287281
&self,
288282
cx: &mut Context,
289283
op: Key<T>,
290-
) -> PushEntry<Key<T>, BufResult<usize, T>> {
284+
) -> PushEntry<Key<T>, (BufResult<usize, T>, u32)> {
291285
instrument!(compio_log::Level::DEBUG, "poll_task", ?op);
292286
let mut driver = self.driver.borrow_mut();
293287
driver.pop(op).map_pending(|mut k| {
@@ -296,19 +290,6 @@ impl Runtime {
296290
})
297291
}
298292

299-
pub(crate) fn poll_task_with_flags<T: OpCode>(
300-
&self,
301-
cx: &mut Context,
302-
op: Key<T>,
303-
) -> PushEntry<Key<T>, (BufResult<usize, T>, u32)> {
304-
instrument!(compio_log::Level::DEBUG, "poll_task_flags", ?op);
305-
let mut driver = self.driver.borrow_mut();
306-
driver.pop_with_flags(op).map_pending(|mut k| {
307-
driver.update_waker(&mut k, cx.waker().clone());
308-
k
309-
})
310-
}
311-
312293
#[cfg(feature = "time")]
313294
pub(crate) fn poll_timer(&self, cx: &mut Context, key: usize) -> Poll<()> {
314295
instrument!(compio_log::Level::DEBUG, "poll_timer", ?cx, ?key);

compio-runtime/src/runtime/op.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,6 @@ use compio_driver::{Key, OpCode, PushEntry};
99

1010
use crate::runtime::Runtime;
1111

12-
#[derive(Debug)]
13-
pub struct OpFuture<T: OpCode> {
14-
key: Option<Key<T>>,
15-
}
16-
17-
impl<T: OpCode> OpFuture<T> {
18-
pub fn new(key: Key<T>) -> Self {
19-
Self { key: Some(key) }
20-
}
21-
}
22-
23-
impl<T: OpCode> Future for OpFuture<T> {
24-
type Output = BufResult<usize, T>;
25-
26-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
27-
let res = Runtime::with_current(|r| r.poll_task(cx, self.key.take().unwrap()));
28-
match res {
29-
PushEntry::Pending(key) => {
30-
self.key = Some(key);
31-
Poll::Pending
32-
}
33-
PushEntry::Ready(res) => Poll::Ready(res),
34-
}
35-
}
36-
}
37-
38-
impl<T: OpCode> Drop for OpFuture<T> {
39-
fn drop(&mut self) {
40-
if let Some(key) = self.key.take() {
41-
Runtime::with_current(|r| r.cancel_op(key))
42-
}
43-
}
44-
}
45-
4612
#[derive(Debug)]
4713
pub struct OpFlagsFuture<T: OpCode> {
4814
key: Option<Key<T>>,
@@ -58,7 +24,7 @@ impl<T: OpCode> Future for OpFlagsFuture<T> {
5824
type Output = (BufResult<usize, T>, u32);
5925

6026
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
61-
let res = Runtime::with_current(|r| r.poll_task_with_flags(cx, self.key.take().unwrap()));
27+
let res = Runtime::with_current(|r| r.poll_task(cx, self.key.take().unwrap()));
6228
match res {
6329
PushEntry::Pending(key) => {
6430
self.key = Some(key);

compio/examples/driver.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ fn push_and_wait<O: OpCode + 'static>(driver: &mut Proactor, op: O) -> (usize, O
5454
driver.poll(None, &mut entries).unwrap();
5555
}
5656
assert_eq!(entries[0], user_data.user_data());
57-
driver.pop(user_data).take_ready().unwrap().unwrap()
57+
driver
58+
.pop(user_data)
59+
.map_ready(|(res, _)| res)
60+
.take_ready()
61+
.unwrap()
62+
.unwrap()
5863
}
5964
}
6065
}

0 commit comments

Comments
 (0)