Skip to content

Commit 94dc00b

Browse files
tomakacramertj
authored andcommitted
Add ThreadPool::spawn_ok and spawn_obj_ok
1 parent fb75154 commit 94dc00b

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

futures-executor/src/thread_pool.rs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,53 @@ impl ThreadPool {
9595
pub fn run<F: Future>(&mut self, f: F) -> F::Output {
9696
crate::LocalPool::new().run_until(f)
9797
}
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+
}
98136
}
99137

100138
impl Spawn for ThreadPool {
101139
fn spawn_obj(
102140
&mut self,
103141
future: FutureObj<'static, ()>,
104142
) -> Result<(), SpawnError> {
105-
(&*self).spawn_obj(future)
143+
self.spawn_obj_ok(future);
144+
Ok(())
106145
}
107146
}
108147

@@ -111,15 +150,7 @@ impl Spawn for &ThreadPool {
111150
&mut self,
112151
future: FutureObj<'static, ()>,
113152
) -> 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);
123154
Ok(())
124155
}
125156
}

0 commit comments

Comments
 (0)