-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsubmission_handler.rs
105 lines (90 loc) · 2.62 KB
/
submission_handler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use super::handle::HandleOpRegisterer;
use std::marker::PhantomData as marker;
use std::{
future::Future,
io,
pin::Pin,
task::{Context, Poll},
};
pub struct SubmissionHandler<T>(marker<T>)
where
T: Unpin;
impl<T> SubmissionHandler<T>
where
T: Unpin + HandleOpRegisterer,
{
pub fn handle_read(
handle: Pin<&mut T>,
cx: &mut Context,
completion_dispatcher: impl Future<Output = io::Result<usize>> + 'static,
) -> Poll<io::Result<usize>> {
let handle = handle.get_mut();
let read_result = handle.read_registerer();
let mut result = match read_result.try_lock() {
Some(result) => result,
None => return Poll::Pending,
};
if result.is_none() {
*result = Some(Box::pin(completion_dispatcher));
}
let poll = {
let result = result.as_mut().unwrap();
result.as_mut().poll(cx).map_ok(|s| s)
};
if poll.is_ready() {
*result = None;
}
poll
}
pub fn handle_write(
handle: Pin<&mut T>,
cx: &mut Context,
completion_dispatcher: impl Future<Output = io::Result<usize>> + 'static,
) -> Poll<io::Result<usize>> {
let handle = handle.get_mut();
let write_result = handle.write_registerer();
let mut result = match write_result.try_lock() {
Some(result) => result,
None => return Poll::Pending,
};
if result.is_none() {
*result = Some(Box::pin(completion_dispatcher));
}
let poll = {
let result = result.as_mut().unwrap();
result.as_mut().poll(cx).map_ok(|s| s)
};
if poll.is_ready() {
*result = None;
}
poll
}
// pub fn handle_seek(
// handle: Pin<&mut T>,
// cx: &mut Context,
// completion_dispatcher: impl Future<Output = io::Result<u64>> + 'static,
// ) -> Poll<Result<u64, Error>> {
// let handle = handle.get_mut();
// let read_result = handle.read_registerer();
//
// let mut result = match read_result.try_lock() {
// Some(result) => result,
// None => return Poll::Pending,
// };
//
// if result.is_none() {
// *result = Some(Box::pin(completion_dispatcher));
// }
//
// let poll = {
// let result = result.as_mut().unwrap();
// result.as_mut().poll(cx).map_ok(|s| s as u64)
// };
//
// if poll.is_ready() {
// *result = None;
// }
//
// poll
// }
}