Skip to content

Commit ae36d12

Browse files
Nemo157boats
authored andcommitted
Cleanup non-Stable stuff from futures::__rt
1 parent e0aa065 commit ae36d12

File tree

6 files changed

+47
-161
lines changed

6 files changed

+47
-161
lines changed

futures-async-runtime/src/future.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
1+
use std::marker::Unpin;
2+
use std::mem::Pin;
13
use std::ops::{Generator, GeneratorState};
24

35
use super::{IsResult, Reset, CTX};
46

5-
use futures_core::Never;
6-
use futures_core::task;
7-
use futures_core::{Poll, Async, Future};
7+
use futures_core::{Async, Poll, Never, task};
8+
use futures_stable::StableFuture;
89

9-
pub trait MyFuture<T: IsResult>: Future<Item=T::Ok, Error = T::Err> {}
10+
pub trait MyStableFuture<T: IsResult>: StableFuture<Item=T::Ok, Error = T::Err> {}
1011

11-
impl<F, T> MyFuture<T> for F
12-
where F: Future<Item = T::Ok, Error = T::Err > + ?Sized,
13-
T: IsResult
12+
impl<F, T> MyStableFuture<T> for F
13+
where F: StableFuture<Item = T::Ok, Error = T::Err> + ?Sized,
14+
T: IsResult,
1415
{}
1516

16-
/// Small shim to translate from a generator to a future.
17-
///
18-
/// This is the translation layer from the generator/coroutine protocol to
19-
/// the futures protocol.
20-
struct GenFuture<T>(T);
17+
struct GenStableFuture<T>(T);
2118

22-
pub fn gen_move<T>(gen: T) -> impl MyFuture<T::Return>
23-
where T: Generator<Yield = Async<Never>>,
24-
T::Return: IsResult,
25-
{
26-
GenFuture(gen)
27-
}
19+
impl<T> !Unpin for GenStableFuture<T> { }
2820

29-
impl<T> Future for GenFuture<T>
21+
impl<T> StableFuture for GenStableFuture<T>
3022
where T: Generator<Yield = Async<Never>>,
3123
T::Return: IsResult,
3224
{
3325
type Item = <T::Return as IsResult>::Ok;
3426
type Error = <T::Return as IsResult>::Err;
3527

36-
fn poll(&mut self, ctx: &mut task::Context) -> Poll<Self::Item, Self::Error> {
28+
fn poll(mut self: Pin<Self>, ctx: &mut task::Context) -> Poll<Self::Item, Self::Error> {
3729
CTX.with(|cell| {
3830
let _r = Reset::new(ctx, cell);
39-
// Because we are controlling the creation of our underlying
40-
// generator, we know that this is definitely a movable generator
41-
// so calling resume is always safe.
42-
match unsafe { self.0.resume() } {
31+
let this: &mut Self = unsafe { Pin::get_mut(&mut self) };
32+
// This is an immovable generator, but since we're only accessing
33+
// it via a Pin this is safe.
34+
match unsafe { this.0.resume() } {
4335
GeneratorState::Yielded(Async::Pending)
4436
=> Ok(Async::Pending),
4537
GeneratorState::Yielded(Async::Ready(mu))
@@ -50,3 +42,10 @@ impl<T> Future for GenFuture<T>
5042
})
5143
}
5244
}
45+
46+
pub fn gen_future<'a, T>(gen: T) -> impl MyStableFuture<T::Return> + 'a
47+
where T: Generator<Yield = Async<Never>> + 'a,
48+
T::Return: IsResult,
49+
{
50+
GenStableFuture(gen)
51+
}

futures-async-runtime/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ if_nightly_and_std! {
2222

2323
mod future;
2424
mod stream;
25-
mod pinned_future;
26-
mod pinned_stream;
2725

2826
use std::cell::Cell;
2927
use std::mem;
@@ -32,8 +30,6 @@ if_nightly_and_std! {
3230

3331
pub use self::future::*;
3432
pub use self::stream::*;
35-
pub use self::pinned_future::*;
36-
pub use self::pinned_stream::*;
3733

3834
pub use futures_core::{Async, Future, Stream};
3935
pub use futures_stable::{StableFuture, StableStream};

futures-async-runtime/src/pinned_future.rs

Lines changed: 0 additions & 51 deletions
This file was deleted.

futures-async-runtime/src/pinned_stream.rs

Lines changed: 0 additions & 61 deletions
This file was deleted.

futures-async-runtime/src/stream.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,58 @@
1+
use std::mem::Pin;
12
use std::ops::{Generator, GeneratorState};
2-
use std::marker::PhantomData;
3+
use std::marker::{PhantomData, Unpin};
34

4-
use futures_core::task;
5-
use futures_core::{Poll, Async, Stream};
5+
use futures_core::{Poll, Async, task};
6+
use futures_stable::StableStream;
67

78
use super::{CTX, Reset, IsResult};
89

9-
pub trait MyStream<T, U: IsResult<Ok=()>>: Stream<Item=T, Error=U::Err> {}
10+
pub trait MyStableStream<T, U: IsResult<Ok=()>>: StableStream<Item=T, Error=U::Err> {}
1011

11-
impl<F, T, U> MyStream<T, U> for F
12-
where F: Stream<Item = T, Error = U::Err> + ?Sized,
12+
impl<F, T, U> MyStableStream<T, U> for F
13+
where F: StableStream<Item = T, Error = U::Err> + ?Sized,
1314
U: IsResult<Ok=()>
1415
{}
1516

1617
/// Small shim to translate from a generator to a stream.
17-
struct GenStream<U, T> {
18+
struct GenStableStream<U, T> {
1819
gen: T,
1920
done: bool,
2021
phantom: PhantomData<U>,
2122
}
2223

23-
pub fn gen_stream<T, U>(gen: T) -> impl MyStream<U, T::Return>
24+
impl<U, T> !Unpin for GenStableStream<U, T> { }
25+
26+
pub fn gen_stream<T, U>(gen: T) -> impl MyStableStream<U, T::Return>
2427
where T: Generator<Yield = Async<U>>,
2528
T::Return: IsResult<Ok = ()>,
2629
{
27-
GenStream { gen, done: false, phantom: PhantomData }
30+
GenStableStream { gen, done: false, phantom: PhantomData }
2831
}
2932

30-
impl<U, T> Stream for GenStream<U, T>
33+
impl<U, T> StableStream for GenStableStream<U, T>
3134
where T: Generator<Yield = Async<U>>,
3235
T::Return: IsResult<Ok = ()>,
3336
{
3437
type Item = U;
3538
type Error = <T::Return as IsResult>::Err;
3639

37-
fn poll_next(&mut self, ctx: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error> {
40+
fn poll_next(mut self: Pin<Self>, ctx: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error> {
3841
CTX.with(|cell| {
3942
let _r = Reset::new(ctx, cell);
40-
if self.done { return Ok(Async::Ready(None)) }
41-
// Because we are controlling the creation of our underlying
42-
// generator, we know that this is definitely a movable generator
43-
// so calling resume is always safe.
44-
match unsafe { self.gen.resume() } {
43+
let this: &mut Self = unsafe { Pin::get_mut(&mut self) };
44+
if this.done { return Ok(Async::Ready(None)) }
45+
// This is an immovable generator, but since we're only accessing
46+
// it via a Pin this is safe.
47+
match unsafe { this.gen.resume() } {
4548
GeneratorState::Yielded(Async::Ready(e)) => {
4649
Ok(Async::Ready(Some(e)))
4750
}
4851
GeneratorState::Yielded(Async::Pending) => {
4952
Ok(Async::Pending)
5053
}
5154
GeneratorState::Complete(e) => {
52-
self.done = true;
55+
this.done = true;
5356
e.into_result().map(|()| Async::Ready(None))
5457
}
5558
}

futures-macro-async/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ if_nightly! {
255255

256256
let attribute = Attribute::from(args.0.into_iter().map(|arg| arg.0));
257257

258-
async_inner(attribute, function, quote_cs! { ::futures::__rt::gen_pinned }, |output, lifetimes| {
258+
async_inner(attribute, function, quote_cs! { ::futures::__rt::gen_future }, |output, lifetimes| {
259259
// TODO: can we lift the restriction that `futures` must be at the root of
260260
// the crate?
261261
let output_span = first_last(&output);
@@ -313,7 +313,7 @@ if_nightly! {
313313
let item_ty = item_ty.expect("#[async_stream] requires item type to be specified");
314314
let attribute = Attribute::from(args);
315315

316-
async_inner(attribute, function, quote_cs! { ::futures::__rt::gen_stream_pinned }, |output, lifetimes| {
316+
async_inner(attribute, function, quote_cs! { ::futures::__rt::gen_stream }, |output, lifetimes| {
317317
let return_ty = match attribute {
318318
Attribute::NONE => quote_cs! {
319319
impl ::futures::__rt::MyStableStream<!, !> + #(#lifetimes +)*
@@ -348,7 +348,7 @@ if_nightly! {
348348
let expr = ExpandAsyncFor.fold_expr(expr);
349349

350350
let mut tokens = quote_cs! {
351-
::futures::__rt::gen_pinned
351+
::futures::__rt::gen_future
352352
};
353353

354354
// Use some manual token construction here instead of `quote_cs!` to ensure
@@ -377,7 +377,7 @@ if_nightly! {
377377
let expr = ExpandAsyncFor.fold_expr(expr);
378378

379379
let mut tokens = quote_cs! {
380-
::futures::__rt::gen_stream_pinned
380+
::futures::__rt::gen_stream
381381
};
382382

383383
// Use some manual token construction here instead of `quote_cs!` to ensure

0 commit comments

Comments
 (0)