Skip to content

Commit

Permalink
Ensure 'static lifetime for future output
Browse files Browse the repository at this point in the history
Protect against rust-lang/rust#84366
  • Loading branch information
timokroeger committed Jun 8, 2024
1 parent 5b3e076 commit 8f7ca98
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/backend/async_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ pub fn dispatch(msg: &MSG) -> bool {
}
}

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

fn spawn_local<F: Future + 'static>(
future: F,
schedule: impl Schedule + Send + Sync + 'static,
) -> (Runnable, async_task::Task<F::Output>) {
fn spawn_local<F, S>(future: F, schedule: S) -> (Runnable, async_task::Task<F::Output>)
where
F: Future + 'static,
F::Output: 'static,
S: Schedule + Send + Sync + 'static,
{
// SAFETY: The `future` does not need to be `Send` because the thread that
// receives the runnable is our own. All other safety properties are ensured
// by the function signature.
Expand Down
6 changes: 5 additions & 1 deletion src/backend/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ impl<F: Future> Future for JoinHandle<F> {
}
}

pub fn spawn<F: Future + 'static>(future: F) -> JoinHandle<F> {
pub fn spawn<F>(future: F) -> JoinHandle<F>
where
F: Future + 'static,
F::Output: 'static,
{
// Create a message only window to run the tasks.
let window = Window::new_reentrant(true, (), |_, msg| {
if msg.msg == MSG_ID_WAKE {
Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ use windows_sys::Win32::{
///
/// Panics when the message loops is running already. This happens when
/// `block_on` or `run` is called from async tasks running on this executor.
pub fn block_on<T: 'static>(future: impl Future<Output = T> + 'static) -> T {
pub fn block_on<F>(future: F) -> F::Output
where
F: Future + 'static,
F::Output: 'static,
{
// Wrap the future so it quits the message loop when finished.
let task = backend::spawn(async move {
let result = future.await;
Expand Down Expand Up @@ -116,6 +120,10 @@ pub type JoinHandle<F> = backend::JoinHandle<F>;
/// This function may be used to spawn tasks when the message loop is not
/// running. The provided future will start running once the message loop
/// is entered with [`MessageLoop::block_on()`] or [`MessageLoop::run()`].
pub fn spawn<F: Future + 'static>(future: F) -> JoinHandle<F> {
pub fn spawn<F>(future: F) -> JoinHandle<F>
where
F: Future + 'static,
F::Output: 'static,
{
backend::spawn(future)
}

0 comments on commit 8f7ca98

Please sign in to comment.