Skip to content

Commit a69ad08

Browse files
committed
fix(dispatch2): Add 'static bound to exec_async
Closes #689.
1 parent 171a694 commit a69ad08

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

crates/dispatch2/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
5959
### Fixed
6060
- **BREAKING**: Use `extern "C-unwind"` instead of `extern "C"` in certain functions that required that.
6161
- **BREAKING**: Use `isize` instead of `usize` in certain functions where that is more correct.
62+
- **BREAKING**: An `F: 'static` bound was added to the following methods to make
63+
sure any referenced values passed to them are borrowed for long
64+
enough since they perform their `work` asynchronously (#689):
65+
- `Group::exec_async`
66+
- `Queue::exec_async`
67+
- `Queue::barrier_async`
68+
- `Queue::barrier_async_and_wait`
6269

6370

6471
## 0.1.0 - 2022-10-02

crates/dispatch2/src/group.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ impl Group {
3838
/// Submit a function to a [Queue] and associates it with the [Group].
3939
pub fn exec_async<F>(&self, queue: &Queue, work: F)
4040
where
41-
F: Send + FnOnce(),
41+
// We need `'static` to make sure any referenced values are borrowed for
42+
// long enough since `work` will be performed asynchronously.
43+
F: Send + FnOnce() + 'static,
4244
{
4345
let work_boxed = Box::into_raw(Box::new(work)).cast::<c_void>();
4446

crates/dispatch2/src/queue.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ impl Queue {
226226
/// Submit a function for asynchronous execution on the [Queue].
227227
pub fn exec_async<F>(&self, work: F)
228228
where
229-
F: Send + FnOnce(),
229+
// We need `'static` to make sure any referenced values are borrowed for
230+
// long enough since `work` will be performed asynchronously.
231+
F: Send + FnOnce() + 'static,
230232
{
231233
let work_boxed = Box::into_raw(Box::new(work)).cast();
232234

@@ -254,7 +256,9 @@ impl Queue {
254256
/// Enqueue a barrier function for asynchronous execution on the [Queue] and return immediately.
255257
pub fn barrier_async<F>(&self, work: F)
256258
where
257-
F: Send + FnOnce(),
259+
// We need `'static` to make sure any referenced values are borrowed for
260+
// long enough since `work` will be performed asynchronously.
261+
F: Send + FnOnce() + 'static,
258262
{
259263
let work_boxed = Box::into_raw(Box::new(work)).cast();
260264

@@ -276,7 +280,9 @@ impl Queue {
276280
/// Submit a function for synchronous execution and mark the function as a barrier for subsequent concurrent tasks.
277281
pub fn barrier_async_and_wait<F>(&self, work: F)
278282
where
279-
F: Send + FnOnce(),
283+
// We need `'static` to make sure any referenced values are borrowed for
284+
// long enough since `work` will be performed asynchronously.
285+
F: Send + FnOnce() + 'static,
280286
{
281287
let work_boxed = Box::into_raw(Box::new(work)).cast();
282288

0 commit comments

Comments
 (0)