summaryrefslogtreecommitdiffstats
path: root/xpcom/rust/moz_task/src/executor/mod.rs
blob: 5e7d1ca81f2cc83206fa170bc33c2b2aa41d7d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::get_current_thread;
use std::{fmt::Debug, future::Future};
use thiserror::Error;

mod future_task;

pub fn spawn_current_thread<Fut>(future: Fut) -> Result<(), Shutdown>
where
    Fut: Future<Output = ()> + 'static,
{
    let current_thread = get_current_thread().map_err(|_| Shutdown { _priv: () })?;
    // # Safety
    //
    // It's safe to use `local_task` since `future` is dispatched `current_thread`.
    unsafe {
        future_task::local_task(future, current_thread.coerce());
    }
    Ok(())
}

/// An error that occurred during spawning on a shutdown event queue
#[derive(Error, Debug)]
#[error("Event target is shutdown")]
pub struct Shutdown {
    _priv: (),
}