Skip to content
This repository was archived by the owner on Oct 30, 2019. It is now read-only.

Commit 5b7224b

Browse files
Nemo157yoshuawuyts
authored andcommitted
Add handle to runtime's thread pool (#58)
1 parent ea01d9c commit 5b7224b

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/task.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,48 @@
22
33
use std::pin::Pin;
44

5+
use futures::future::FutureObj;
56
use futures::prelude::*;
6-
use futures::task::{Context, Poll};
7+
use futures::task::{Context, Poll, Spawn, SpawnError};
8+
9+
/// A [`Spawn`] handle to runtime's thread pool for spawning futures.
10+
///
11+
/// This allows integrating runtime with libraries based on explicitly passed spawners.
12+
#[derive(Debug)]
13+
pub struct Spawner {
14+
_reserved: (),
15+
}
16+
17+
impl Spawner {
18+
/// Construct a new [`Spawn`] handle.
19+
pub fn new() -> Self {
20+
Self { _reserved: () }
21+
}
22+
}
23+
24+
impl Default for Spawner {
25+
fn default() -> Self {
26+
Self::new()
27+
}
28+
}
29+
30+
impl Clone for Spawner {
31+
fn clone(&self) -> Self {
32+
Self::new()
33+
}
34+
}
35+
36+
impl Spawn for Spawner {
37+
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
38+
(&*self).spawn_obj(future)
39+
}
40+
}
41+
42+
impl<'a> Spawn for &'a Spawner {
43+
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
44+
runtime_raw::current_runtime().spawn_boxed(future.boxed())
45+
}
46+
}
747

848
/// Spawn a future on the runtime's thread pool.
949
///

0 commit comments

Comments
 (0)