Skip to content

Commit 015aa8d

Browse files
committed
Update for latest generators changes
As of rust-lang/rust#49194 it's now unsafe to resume a generator, but safe to create an immovable generator. Fixes alexcrichton#74
1 parent b6ea9af commit 015aa8d

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

futures-await-async-macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ where F: FnOnce(&Type, &[&Lifetime]) -> proc_macro2::TokenStream
206206
let gen_function = respan(gen_function.into(), &output_span);
207207
let body_inner = if pinned {
208208
quote_cs! {
209-
#gen_function (#[allow(unused_unsafe)] unsafe { static move || -> #output #gen_body })
209+
#gen_function (static move || -> #output #gen_body)
210210
}
211211
} else {
212212
quote_cs! {

src/__rt/future.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ impl<T> Future for GenFuture<T>
3636
fn poll(&mut self, ctx: &mut task::Context) -> Poll<Self::Item, Self::Error> {
3737
CTX.with(|cell| {
3838
let _r = Reset::new(ctx, cell);
39-
match self.0.resume() {
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() } {
4043
GeneratorState::Yielded(Async::Pending)
4144
=> Ok(Async::Pending),
4245
GeneratorState::Yielded(Async::Ready(mu))

src/__rt/pinned_future.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ impl<T> StableFuture for GenStableFuture<T>
3131
CTX.with(|cell| {
3232
let _r = Reset::new(ctx, cell);
3333
let this: &mut Self = unsafe { Pin::get_mut(&mut self) };
34-
match this.0.resume() {
34+
// This is an immovable generator, but since we're only accessing
35+
// it via a Pin this is safe.
36+
match unsafe { this.0.resume() } {
3537
GeneratorState::Yielded(Async::Pending)
3638
=> Ok(Async::Pending),
3739
GeneratorState::Yielded(Async::Ready(mu))

src/__rt/pinned_stream.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ impl<U, T> StableStream for GenStableStream<U, T>
4343
let _r = Reset::new(ctx, cell);
4444
let this: &mut Self = unsafe { Pin::get_mut(&mut self) };
4545
if this.done { return Ok(Async::Ready(None)) }
46-
match this.gen.resume() {
46+
// This is an immovable generator, but since we're only accessing
47+
// it via a Pin this is safe.
48+
match unsafe { this.gen.resume() } {
4749
GeneratorState::Yielded(Async::Ready(e)) => {
4850
Ok(Async::Ready(Some(e)))
4951
}

src/__rt/stream.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ impl<U, T> Stream for GenStream<U, T>
3838
CTX.with(|cell| {
3939
let _r = Reset::new(ctx, cell);
4040
if self.done { return Ok(Async::Ready(None)) }
41-
match self.gen.resume() {
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() } {
4245
GeneratorState::Yielded(Async::Ready(e)) => {
4346
Ok(Async::Ready(Some(e)))
4447
}

0 commit comments

Comments
 (0)