@@ -95,14 +95,53 @@ impl ThreadPool {
95
95
pub fn run < F : Future > ( & mut self , f : F ) -> F :: Output {
96
96
crate :: LocalPool :: new ( ) . run_until ( f)
97
97
}
98
+
99
+ /// Spawns a future that will be run to completion.
100
+ ///
101
+ /// > **Note**: This method is similar to `Spawn::spawn_obj`, except that
102
+ /// > it is guaranteed to always succeed.
103
+ pub fn spawn_obj_ok ( & self , future : FutureObj < ' static , ( ) > ) {
104
+ let task = Task {
105
+ future,
106
+ wake_handle : Arc :: new ( WakeHandle {
107
+ exec : self . clone ( ) ,
108
+ mutex : UnparkMutex :: new ( ) ,
109
+ } ) ,
110
+ exec : self . clone ( ) ,
111
+ } ;
112
+ self . state . send ( Message :: Run ( task) ) ;
113
+ }
114
+
115
+ /// Spawns a task that polls the given future with output `()` to
116
+ /// completion.
117
+ ///
118
+ /// ```
119
+ /// #![feature(async_await)]
120
+ /// use futures::executor::ThreadPool;
121
+ ///
122
+ /// let pool = ThreadPool::new().unwrap();
123
+ ///
124
+ /// let future = async { /* ... */ };
125
+ /// pool.spawn_ok(future);
126
+ /// ```
127
+ ///
128
+ /// > **Note**: This method is similar to `SpawnExt::spawn`, except that
129
+ /// > it is guaranteed to always succeed.
130
+ pub fn spawn_ok < Fut > ( & self , future : Fut )
131
+ where
132
+ Fut : Future < Output = ( ) > + Send + ' static ,
133
+ {
134
+ self . spawn_obj_ok ( FutureObj :: new ( Box :: new ( future) ) )
135
+ }
98
136
}
99
137
100
138
impl Spawn for ThreadPool {
101
139
fn spawn_obj (
102
140
& mut self ,
103
141
future : FutureObj < ' static , ( ) > ,
104
142
) -> Result < ( ) , SpawnError > {
105
- ( & * self ) . spawn_obj ( future)
143
+ self . spawn_obj_ok ( future) ;
144
+ Ok ( ( ) )
106
145
}
107
146
}
108
147
@@ -111,15 +150,7 @@ impl Spawn for &ThreadPool {
111
150
& mut self ,
112
151
future : FutureObj < ' static , ( ) > ,
113
152
) -> Result < ( ) , SpawnError > {
114
- let task = Task {
115
- future,
116
- wake_handle : Arc :: new ( WakeHandle {
117
- exec : self . clone ( ) ,
118
- mutex : UnparkMutex :: new ( ) ,
119
- } ) ,
120
- exec : self . clone ( ) ,
121
- } ;
122
- self . state . send ( Message :: Run ( task) ) ;
153
+ self . spawn_obj_ok ( future) ;
123
154
Ok ( ( ) )
124
155
}
125
156
}
0 commit comments