@@ -35,19 +35,44 @@ if_nightly! {
3535 use unsafe_pin:: UnsafePin ;
3636 }
3737
38+ /// A trait for `Future`s which can be pinned to a particular location in memory.
39+ ///
40+ /// These futures take `self` by `Pin<Self>`, rather than `&mut Self`.
41+ /// This allows types which are not [`Unpin`](::std::marker::Unpin) to guarantee
42+ /// that they won't be moved after being polled. Since they won't be moved, it's
43+ /// possible for them to safely contain references to themselves.
44+ ///
45+ /// The most common examples of such self-referential `StableFuture`s are `#[async]`
46+ /// functions and `async_block!`s.
47+ ///
48+ /// All types which implement `Future` also implement `StableFuture` automatically.
3849 pub trait StableFuture {
50+ /// A successful value
3951 type Item ;
52+
53+ /// An error
4054 type Error ;
4155
56+ /// Attempt to resolve the future to a final value, registering the current task
57+ /// for wakeup if the value is not yet available.
58+ ///
59+ /// This method takes `self` by `Pin`, and so calling it requires putting `Self`
60+ /// in a [`PinBox`](::std::boxed::PinBox) using the `pin` method, or otherwise
61+ /// guaranteeing that the location of `self` will not change after a call to `poll`.
4262 fn poll( self : Pin <Self >, ctx: & mut task:: Context ) -> Poll <Self :: Item , Self :: Error >;
4363
64+ /// Pin the future to a particular location by placing it on the heap.
4465 #[ cfg( feature = "std" ) ]
4566 fn pin<' a>( self ) -> PinBox <Future <Item = Self :: Item , Error = Self :: Error > + Send + ' a>
4667 where Self : Send + Sized + ' a
4768 {
4869 PinBox :: new( unsafe { UnsafePin :: new( self ) } )
4970 }
5071
72+ /// Pin the future to a particular location by placing it on the heap.
73+ ///
74+ /// This method is the same as `pin`, but doesn't require that `Self` can be
75+ /// safely sent across threads. `pin` should be preferred where possible.
5176 #[ cfg( feature = "std" ) ]
5277 fn pin_local<' a>( self ) -> PinBox <Future <Item = Self :: Item , Error = Self :: Error > + ' a>
5378 where Self : Sized + ' a
@@ -65,19 +90,43 @@ if_nightly! {
6590 }
6691 }
6792
93+ /// A trait for `Stream`s which can be pinned to a particular location in memory.
94+ ///
95+ /// These streams take `self` by `Pin<Self>`, rather than `&mut Self`.
96+ /// This allows types which are not [`Unpin`](::std::marker::Unpin) to guarantee
97+ /// that they won't be moved after being polled. Since they won't be moved, it's
98+ /// possible for them to safely contain references to themselves.
99+ ///
100+ /// The most common examples of such self-referential `StableStream`s are
101+ /// `#[async_stream(item = Foo)]` functions.
102+ ///
103+ /// All types which implement `Stream` also implement `StableStream` automatically.
68104 pub trait StableStream {
105+ /// A successful value
69106 type Item ;
107+ /// An error
70108 type Error ;
71109
110+ /// Attempt to resolve the stream to the next value, registering the current task
111+ /// for wakeup if the value is not yet available.
112+ ///
113+ /// This method takes `self` by `Pin`, and so calling it requires putting `Self`
114+ /// in a [`PinBox`](::std::boxed::PinBox) using the `pin` method, or otherwise
115+ /// guaranteeing that the location of `self` will not change after a call to `poll`.
72116 fn poll_next( self : Pin <Self >, ctx: & mut task:: Context ) -> Poll <Option <Self :: Item >, Self :: Error >;
73117
118+ /// Pin the stream to a particular location by placing it on the heap.
74119 #[ cfg( feature = "std" ) ]
75120 fn pin<' a>( self ) -> PinBox <Stream <Item = Self :: Item , Error = Self :: Error > + Send + ' a>
76121 where Self : Send + Sized + ' a
77122 {
78123 PinBox :: new( unsafe { UnsafePin :: new( self ) } )
79124 }
80125
126+ /// Pin the stream to a particular location by placing it on the heap.
127+ ///
128+ /// This method is the same as `pin`, but doesn't require that `Self` can be
129+ /// safely sent across threads. `pin` should be preferred where possible.
81130 #[ cfg( feature = "std" ) ]
82131 fn pin_local<' a>( self ) -> PinBox <Stream <Item = Self :: Item , Error = Self :: Error > + ' a>
83132 where Self : Sized + ' a
0 commit comments