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/libcore | |
| 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/libcore')
| -rw-r--r-- | src/libcore/marker.rs | 1 | ||||
| -rw-r--r-- | src/libcore/ops/generator.rs | 37 | ||||
| -rw-r--r-- | src/libcore/pin.rs | 1 |
3 files changed, 25 insertions, 14 deletions
diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index 65752ba0321..457d556e4fa 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -627,6 +627,7 @@ unsafe impl<T: ?Sized> Freeze for &mut T {} /// [`Pin`]: ../pin/struct.Pin.html /// [`pin module`]: ../../std/pin/index.html #[stable(feature = "pin", since = "1.33.0")] +#[cfg_attr(not(stage0), lang = "unpin")] pub auto trait Unpin {} /// A marker type which does not implement `Unpin`. diff --git a/src/libcore/ops/generator.rs b/src/libcore/ops/generator.rs index 1542cd6397e..5401fff860e 100644 --- a/src/libcore/ops/generator.rs +++ b/src/libcore/ops/generator.rs @@ -1,3 +1,6 @@ +use crate::marker::Unpin; +use crate::pin::Pin; + /// The result of a generator resumption. /// /// This enum is returned from the `Generator::resume` method and indicates the @@ -39,6 +42,7 @@ pub enum GeneratorState<Y, R> { /// #![feature(generators, generator_trait)] /// /// use std::ops::{Generator, GeneratorState}; +/// use std::pin::Pin; /// /// fn main() { /// let mut generator = || { @@ -46,11 +50,11 @@ pub enum GeneratorState<Y, R> { /// return "foo" /// }; /// -/// match unsafe { generator.resume() } { +/// match Pin::new(&mut generator).resume() { /// GeneratorState::Yielded(1) => {} /// _ => panic!("unexpected return from resume"), /// } -/// match unsafe { generator.resume() } { +/// match Pin::new(&mut generator).resume() { /// GeneratorState::Complete("foo") => {} /// _ => panic!("unexpected return from resume"), /// } @@ -88,10 +92,6 @@ pub trait Generator { /// generator will continue executing until it either yields or returns, at /// which point this function will return. /// - /// The function is unsafe because it can be used on an immovable generator. - /// After such a call, the immovable generator must not move again, but - /// this is not enforced by the compiler. - /// /// # Return value /// /// The `GeneratorState` enum returned from this function indicates what @@ -110,16 +110,25 @@ pub trait Generator { /// been returned previously. While generator literals in the language are /// guaranteed to panic on resuming after `Complete`, this is not guaranteed /// for all implementations of the `Generator` trait. - unsafe fn resume(&mut self) -> GeneratorState<Self::Yield, Self::Return>; + fn resume(self: Pin<&mut Self>) -> GeneratorState<Self::Yield, Self::Return>; +} + +#[unstable(feature = "generator_trait", issue = "43122")] +impl<G: ?Sized + Generator> Generator for Pin<&mut G> { + type Yield = G::Yield; + type Return = G::Return; + + fn resume(mut self: Pin<&mut Self>) -> GeneratorState<Self::Yield, Self::Return> { + G::resume((*self).as_mut()) + } } #[unstable(feature = "generator_trait", issue = "43122")] -impl<T> Generator for &mut T - where T: Generator + ?Sized -{ - type Yield = T::Yield; - type Return = T::Return; - unsafe fn resume(&mut self) -> GeneratorState<Self::Yield, Self::Return> { - (**self).resume() +impl<G: ?Sized + Generator + Unpin> Generator for &mut G { + type Yield = G::Yield; + type Return = G::Return; + + fn resume(mut self: Pin<&mut Self>) -> GeneratorState<Self::Yield, Self::Return> { + G::resume(Pin::new(&mut *self)) } } diff --git a/src/libcore/pin.rs b/src/libcore/pin.rs index 7c09a36d898..56a32c928fb 100644 --- a/src/libcore/pin.rs +++ b/src/libcore/pin.rs @@ -117,6 +117,7 @@ use ops::{Deref, DerefMut, Receiver, CoerceUnsized, DispatchFromDyn}; // implementations, are allowed because they all only use `&P`, so they cannot move // the value behind `pointer`. #[stable(feature = "pin", since = "1.33.0")] +#[cfg_attr(not(stage0), lang = "pin")] #[fundamental] #[repr(transparent)] #[derive(Copy, Clone, Hash, Eq, Ord)] |
