-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
87 lines (68 loc) · 2.1 KB
/
lib.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
#![no_std]
#![feature(arbitrary_self_types)]
extern crate pin_api;
extern crate futures_core;
extern crate futures_executor;
macro_rules! if_std {
($($i:item)*) => ($(
#[cfg(feature = "std")]
$i
)*)
}
use pin_api::{PinMut, Unpin};
use futures_core::{Future, Stream, Poll, task};
if_std! {
mod executor;
mod unsafe_pin;
use pin_api::PinBox;
pub use executor::{StableExecutor, block_on_stable};
use unsafe_pin::UnsafePin;
}
pub trait StableFuture {
type Item;
type Error;
fn poll(self: PinMut<Self>, ctx: &mut task::Context) -> Poll<Self::Item, Self::Error>;
#[cfg(feature = "std")]
fn pin<'a>(self) -> PinBox<Future<Item = Self::Item, Error = Self::Error> + Send + 'a>
where Self: Send + Sized + 'a
{
PinBox::new(unsafe { UnsafePin::new(self) })
}
#[cfg(feature = "std")]
fn pin_local<'a>(self) -> PinBox<Future<Item = Self::Item, Error = Self::Error> + 'a>
where Self: Sized + 'a
{
PinBox::new(unsafe { UnsafePin::new(self) })
}
}
impl<F: Future + Unpin> StableFuture for F {
type Item = F::Item;
type Error = F::Error;
fn poll(mut self: PinMut<Self>, ctx: &mut task::Context) -> Poll<Self::Item, Self::Error> {
F::poll(&mut *self, ctx)
}
}
pub trait StableStream {
type Item;
type Error;
fn poll_next(self: PinMut<Self>, ctx: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error>;
#[cfg(feature = "std")]
fn pin<'a>(self) -> PinBox<Stream<Item = Self::Item, Error = Self::Error> + Send + 'a>
where Self: Send + Sized + 'a
{
PinBox::new(unsafe { UnsafePin::new(self) })
}
#[cfg(feature = "std")]
fn pin_local<'a>(self) -> PinBox<Stream<Item = Self::Item, Error = Self::Error> + 'a>
where Self: Sized + 'a
{
PinBox::new(unsafe { UnsafePin::new(self) })
}
}
impl<S: Stream + Unpin> StableStream for S {
type Item = S::Item;
type Error = S::Error;
fn poll_next(mut self: PinMut<Self>, ctx: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error> {
S::poll_next(&mut *self, ctx)
}
}