diff options
| author | bors <bors@rust-lang.org> | 2019-01-28 14:12:15 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-01-28 14:12:15 +0000 |
| commit | d8a0dd7ae88023bd09fa4b86c9ca1f6ed8095b43 (patch) | |
| tree | c963f7629d086be931f8f95f9093f4167d258966 /src/libstd/future.rs | |
| parent | a21bd756889942cfed06dfd4ccd08838fc27ffdf (diff) | |
| parent | a21c95f08e37a2e609a0cb523eb9c132d8c341f3 (diff) | |
| download | rust-d8a0dd7ae88023bd09fa4b86c9ca1f6ed8095b43.tar.gz rust-d8a0dd7ae88023bd09fa4b86c9ca1f6ed8095b43.zip | |
Auto merge of #55704 - Nemo157:pinned-generators, r=Zoxc
Use pinning for generators to make trait safe I'm unsure whether there needs to be any changes to the actual generator transform. Tests are passing so the fact that `Pin<&mut T>` is fundamentally the same as `&mut T` seems to allow it to still work, but maybe there's something subtle here that could go wrong. This is specified in [RFC 2349 ยง Immovable generators](https://github.com/rust-lang/rfcs/blob/master/text/2349-pin.md#immovable-generators) (although, since that RFC it has become safe to create an immovable generator, and instead it's unsafe to resume any generator; with these changes both are now safe and instead the unsafety is moved to creating a `Pin<&mut [static generator]>` which there are safe APIs for). CC #43122
Diffstat (limited to 'src/libstd/future.rs')
| -rw-r--r-- | src/libstd/future.rs | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/src/libstd/future.rs b/src/libstd/future.rs index 22900c3067b..d1203be3cf0 100644 --- a/src/libstd/future.rs +++ b/src/libstd/future.rs @@ -33,7 +33,9 @@ impl<T: Generator<Yield = ()>> !Unpin for GenFuture<T> {} impl<T: Generator<Yield = ()>> Future for GenFuture<T> { type Output = T::Return; fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> { - set_task_waker(lw, || match unsafe { Pin::get_unchecked_mut(self).0.resume() } { + // Safe because we're !Unpin + !Drop mapping to a ?Unpin value + let gen = unsafe { Pin::map_unchecked_mut(self, |s| &mut s.0) }; + set_task_waker(lw, || match gen.resume() { GeneratorState::Yielded(()) => Poll::Pending, GeneratorState::Complete(x) => Poll::Ready(x), }) |
