Skip to content

Commit 70420e7

Browse files
committed
Add generic poll_fn_notify to Spawn
1 parent 957f47b commit 70420e7

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/task_impl/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ impl<T: ?Sized> Spawn<T> {
275275
self.obj
276276
}
277277

278+
/// Calls the provided closure, scheduling notifications to be sent to the
279+
/// `notify` argument.
280+
pub fn poll_fn_notify<N, F, R>(&mut self,
281+
notify: &N,
282+
id: usize,
283+
f: F) -> R
284+
where F: FnOnce(&mut T) -> R,
285+
N: Clone + Into<NotifyHandle>,
286+
{
287+
let mk = || notify.clone().into();
288+
self.enter(BorrowedUnpark::new(&mk, id), f)
289+
}
290+
278291
/// Polls the internal future, scheduling notifications to be sent to the
279292
/// `notify` argument.
280293
///
@@ -310,8 +323,7 @@ impl<T: ?Sized> Spawn<T> {
310323
where N: Clone + Into<NotifyHandle>,
311324
T: Future,
312325
{
313-
let mk = || notify.clone().into();
314-
self.enter(BorrowedUnpark::new(&mk, id), |f| f.poll())
326+
self.poll_fn_notify(notify, id, |f| f.poll())
315327
}
316328

317329
/// Like `poll_future_notify`, except polls the underlying stream.
@@ -322,8 +334,7 @@ impl<T: ?Sized> Spawn<T> {
322334
where N: Clone + Into<NotifyHandle>,
323335
T: Stream,
324336
{
325-
let mk = || notify.clone().into();
326-
self.enter(BorrowedUnpark::new(&mk, id), |s| s.poll())
337+
self.poll_fn_notify(notify, id, |s| s.poll())
327338
}
328339

329340
/// Invokes the underlying `start_send` method with this task in place.
@@ -339,8 +350,7 @@ impl<T: ?Sized> Spawn<T> {
339350
where N: Clone + Into<NotifyHandle>,
340351
T: Sink,
341352
{
342-
let mk = || notify.clone().into();
343-
self.enter(BorrowedUnpark::new(&mk, id), |s| s.start_send(value))
353+
self.poll_fn_notify(notify, id, |s| s.start_send(value))
344354
}
345355

346356
/// Invokes the underlying `poll_complete` method with this task in place.
@@ -355,8 +365,7 @@ impl<T: ?Sized> Spawn<T> {
355365
where N: Clone + Into<NotifyHandle>,
356366
T: Sink,
357367
{
358-
let mk = || notify.clone().into();
359-
self.enter(BorrowedUnpark::new(&mk, id), |s| s.poll_complete())
368+
self.poll_fn_notify(notify, id, |s| s.poll_complete())
360369
}
361370

362371
/// Invokes the underlying `close` method with this task in place.
@@ -371,8 +380,7 @@ impl<T: ?Sized> Spawn<T> {
371380
where N: Clone + Into<NotifyHandle>,
372381
T: Sink,
373382
{
374-
let mk = || notify.clone().into();
375-
self.enter(BorrowedUnpark::new(&mk, id), |s| s.close())
383+
self.poll_fn_notify(notify, id, |s| s.close())
376384
}
377385

378386
fn enter<F, R>(&mut self, unpark: BorrowedUnpark, f: F) -> R

0 commit comments

Comments
 (0)