Skip to content

Commit 8f7ca98

Browse files
committed
Ensure 'static lifetime for future output
Protect against rust-lang/rust#84366
1 parent 5b3e076 commit 8f7ca98

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/backend/async_task.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ pub fn dispatch(msg: &MSG) -> bool {
2424
}
2525
}
2626

27-
pub fn spawn<F: Future + 'static>(future: F) -> JoinHandle<F> {
27+
pub fn spawn<F>(future: F) -> JoinHandle<F>
28+
where
29+
F: Future + 'static,
30+
F::Output: 'static,
31+
{
2832
// Its important to get the current thread id *outside* of the `schedule`
2933
// closure which can run from a different.
3034
let thread_id = unsafe { GetCurrentThreadId() };
@@ -48,10 +52,12 @@ pub fn spawn<F: Future + 'static>(future: F) -> JoinHandle<F> {
4852
}
4953
}
5054

51-
fn spawn_local<F: Future + 'static>(
52-
future: F,
53-
schedule: impl Schedule + Send + Sync + 'static,
54-
) -> (Runnable, async_task::Task<F::Output>) {
55+
fn spawn_local<F, S>(future: F, schedule: S) -> (Runnable, async_task::Task<F::Output>)
56+
where
57+
F: Future + 'static,
58+
F::Output: 'static,
59+
S: Schedule + Send + Sync + 'static,
60+
{
5561
// SAFETY: The `future` does not need to be `Send` because the thread that
5662
// receives the runnable is our own. All other safety properties are ensured
5763
// by the function signature.

src/backend/window.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ impl<F: Future> Future for JoinHandle<F> {
8383
}
8484
}
8585

86-
pub fn spawn<F: Future + 'static>(future: F) -> JoinHandle<F> {
86+
pub fn spawn<F>(future: F) -> JoinHandle<F>
87+
where
88+
F: Future + 'static,
89+
F::Output: 'static,
90+
{
8791
// Create a message only window to run the tasks.
8892
let window = Window::new_reentrant(true, (), |_, msg| {
8993
if msg.msg == MSG_ID_WAKE {

src/lib.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ use windows_sys::Win32::{
2929
///
3030
/// Panics when the message loops is running already. This happens when
3131
/// `block_on` or `run` is called from async tasks running on this executor.
32-
pub fn block_on<T: 'static>(future: impl Future<Output = T> + 'static) -> T {
32+
pub fn block_on<F>(future: F) -> F::Output
33+
where
34+
F: Future + 'static,
35+
F::Output: 'static,
36+
{
3337
// Wrap the future so it quits the message loop when finished.
3438
let task = backend::spawn(async move {
3539
let result = future.await;
@@ -116,6 +120,10 @@ pub type JoinHandle<F> = backend::JoinHandle<F>;
116120
/// This function may be used to spawn tasks when the message loop is not
117121
/// running. The provided future will start running once the message loop
118122
/// is entered with [`MessageLoop::block_on()`] or [`MessageLoop::run()`].
119-
pub fn spawn<F: Future + 'static>(future: F) -> JoinHandle<F> {
123+
pub fn spawn<F>(future: F) -> JoinHandle<F>
124+
where
125+
F: Future + 'static,
126+
F::Output: 'static,
127+
{
120128
backend::spawn(future)
121129
}

0 commit comments

Comments
 (0)