@@ -35,19 +35,44 @@ if_nightly! {
35
35
use unsafe_pin:: UnsafePin ;
36
36
}
37
37
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.
38
49
pub trait StableFuture {
50
+ /// A successful value
39
51
type Item ;
52
+
53
+ /// An error
40
54
type Error ;
41
55
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`.
42
62
fn poll( self : Pin <Self >, ctx: & mut task:: Context ) -> Poll <Self :: Item , Self :: Error >;
43
63
64
+ /// Pin the future to a particular location by placing it on the heap.
44
65
#[ cfg( feature = "std" ) ]
45
66
fn pin<' a>( self ) -> PinBox <Future <Item = Self :: Item , Error = Self :: Error > + Send + ' a>
46
67
where Self : Send + Sized + ' a
47
68
{
48
69
PinBox :: new( unsafe { UnsafePin :: new( self ) } )
49
70
}
50
71
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.
51
76
#[ cfg( feature = "std" ) ]
52
77
fn pin_local<' a>( self ) -> PinBox <Future <Item = Self :: Item , Error = Self :: Error > + ' a>
53
78
where Self : Sized + ' a
@@ -65,19 +90,43 @@ if_nightly! {
65
90
}
66
91
}
67
92
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.
68
104
pub trait StableStream {
105
+ /// A successful value
69
106
type Item ;
107
+ /// An error
70
108
type Error ;
71
109
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`.
72
116
fn poll_next( self : Pin <Self >, ctx: & mut task:: Context ) -> Poll <Option <Self :: Item >, Self :: Error >;
73
117
118
+ /// Pin the stream to a particular location by placing it on the heap.
74
119
#[ cfg( feature = "std" ) ]
75
120
fn pin<' a>( self ) -> PinBox <Stream <Item = Self :: Item , Error = Self :: Error > + Send + ' a>
76
121
where Self : Send + Sized + ' a
77
122
{
78
123
PinBox :: new( unsafe { UnsafePin :: new( self ) } )
79
124
}
80
125
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.
81
130
#[ cfg( feature = "std" ) ]
82
131
fn pin_local<' a>( self ) -> PinBox <Stream <Item = Self :: Item , Error = Self :: Error > + ' a>
83
132
where Self : Sized + ' a
0 commit comments