Skip to content

Commit a118130

Browse files
committed
Add a &LoopPin to add_loop_data closures
This way they can use that proof that they're running on the event loop
1 parent 234ca28 commit a118130

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

futures-mio/src/event_loop/loop_data.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl LoopHandle {
9595
/// closure `f`, generate a handle, and then the future will yield it back.
9696
// TODO: more with examples
9797
pub fn add_loop_data<F, A>(&self, f: F) -> AddLoopData<F, A>
98-
where F: FnOnce() -> A + Send + 'static,
98+
where F: FnOnce(&LoopPin) -> A + Send + 'static,
9999
A: 'static,
100100
{
101101
AddLoopData {
@@ -109,18 +109,19 @@ impl LoopHandle {
109109
}
110110

111111
impl<F, A> Future for AddLoopData<F, A>
112-
where F: FnOnce() -> A + Send + 'static,
112+
where F: FnOnce(&LoopPin) -> A + Send + 'static,
113113
A: 'static,
114114
{
115115
type Item = LoopData<A>;
116116
type Error = io::Error;
117117

118118
fn poll(&mut self) -> Poll<LoopData<A>, io::Error> {
119-
let ret = self.inner.poll(|_lp, f| {
120-
Ok(DropBox::new(f()))
119+
let ret = self.inner.poll(|lp, f| {
120+
Ok(DropBox::new(f(&lp.pin())))
121121
}, |f, slot| {
122122
Message::Run(Box::new(move || {
123-
slot.try_produce(Ok(DropBox::new(f()))).ok()
123+
let pin = super::CURRENT_LOOP.with(|lp| lp.pin());
124+
slot.try_produce(Ok(DropBox::new(f(&pin)))).ok()
124125
.expect("add loop data try_produce intereference");
125126
}))
126127
});

futures-socks5/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct GlobalBuffer {
147147

148148
impl GlobalBuffer {
149149
fn new(lp: &Loop, size: usize) -> IoFuture<GlobalBuffer> {
150-
lp.handle().add_loop_data(move || {
150+
lp.handle().add_loop_data(move |_| {
151151
RefCell::new(vec![0u8; size])
152152
}).map(|data| {
153153
GlobalBuffer { inner: Arc::new(data) }

0 commit comments

Comments
 (0)