1 use super::{Compat, Future01CompatExt}
;
3 future
::{FutureExt, TryFutureExt, UnitError}
,
6 use futures_01
::future
::{ExecuteError as ExecuteError01, Executor as Executor01}
;
7 use futures_01
::Future
as Future01
;
8 use futures_task
::{FutureObj, Spawn as Spawn03, SpawnError as SpawnError03}
;
10 /// A future that can run on a futures 0.1
11 /// [`Executor`](futures_01::future::Executor).
12 pub type Executor01Future
= Compat
<UnitError
<FutureObj
<'
static, ()>>>;
14 /// Extension trait for futures 0.1 [`Executor`](futures_01::future::Executor).
15 pub trait Executor01CompatExt
: Executor01
<Executor01Future
> + Clone
+ Send
+ '
static {
16 /// Converts a futures 0.1 [`Executor`](futures_01::future::Executor) into a
17 /// futures 0.3 [`Spawn`](futures_task::Spawn).
20 /// use futures::task::SpawnExt;
21 /// use futures::future::{FutureExt, TryFutureExt};
22 /// use futures_util::compat::Executor01CompatExt;
23 /// use tokio::executor::DefaultExecutor;
25 /// # let (tx, rx) = futures::channel::oneshot::channel();
27 /// let spawner = DefaultExecutor::current().compat();
28 /// let future03 = async move {
29 /// println!("Running on the pool");
30 /// spawner.spawn(async {
31 /// println!("Spawned!");
32 /// # tx.send(42).unwrap();
36 /// let future01 = future03.unit_error().boxed().compat();
38 /// tokio::run(future01);
39 /// # futures::executor::block_on(rx).unwrap();
41 fn compat(self) -> Executor01As03
<Self>
46 impl<Ex
> Executor01CompatExt
for Ex
48 Ex
: Executor01
<Executor01Future
> + Clone
+ Send
+ '
static,
50 fn compat(self) -> Executor01As03
<Self> {
51 Executor01As03 { executor01: self }
55 /// Converts a futures 0.1 [`Executor`](futures_01::future::Executor) into a
56 /// futures 0.3 [`Spawn`](futures_task::Spawn).
57 #[derive(Debug, Clone)]
58 pub struct Executor01As03
<Ex
> {
62 impl<Ex
> Spawn03
for Executor01As03
<Ex
>
64 Ex
: Executor01
<Executor01Future
> + Clone
+ Send
+ '
static,
66 fn spawn_obj(&self, future
: FutureObj
<'
static, ()>) -> Result
<(), SpawnError03
> {
67 let future
= future
.unit_error().compat();
69 self.executor01
.execute(future
).map_err(|_
| SpawnError03
::shutdown())
73 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058
74 impl<Sp
, Fut
> Executor01
<Fut
> for Compat
<Sp
>
76 for<'a
> &'a Sp
: Spawn03
,
77 Fut
: Future01
<Item
= (), Error
= ()> + Send
+ '
static,
79 fn execute(&self, future
: Fut
) -> Result
<(), ExecuteError01
<Fut
>> {
81 .spawn(future
.compat().map(|_
| ()))
82 .expect("unable to spawn future from Compat executor");