Skip to main content

ntex_rt/
rt_default.rs

1use std::{fmt, future::Future, time::Instant};
2
3pub use crate::handle::{JoinHandle, RemoteJoinHandle};
4pub use crate::{rt::Handle, rt::Runtime};
5
6#[derive(Debug, Copy, Clone)]
7pub struct JoinError;
8
9impl fmt::Display for JoinError {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        write!(f, "JoinError")
12    }
13}
14
15impl std::error::Error for JoinError {}
16
17/// Returns the start of the current runtime turn.
18///
19/// Tasks polled in the same turn observe the same instant, see
20/// [`Runtime::now`]. Returns [`Instant::now()`] outside of a runtime.
21#[inline]
22pub fn now() -> Instant {
23    crate::rt::Runtime::current_now()
24}
25
26#[track_caller]
27pub fn spawn<F>(fut: F) -> JoinHandle<F::Output>
28where
29    F: Future + 'static,
30{
31    let location = std::panic::Location::caller();
32    let fut = crate::task::wrap(fut);
33    crate::rt::Runtime::with_current(|rt| unsafe { rt.spawn_at(fut, location) })
34}
35
36/// Spawns a future on the current thread without a join handle.
37///
38/// The output is dropped when the task completes. Unlike dropping the
39/// [`JoinHandle`] returned by [`spawn()`], this does not allocate a handle.
40///
41/// # Panics
42///
43/// This function panics if ntex system is not running.
44#[track_caller]
45pub fn spawn_detached<F>(fut: F)
46where
47    F: Future + 'static,
48{
49    let location = std::panic::Location::caller();
50    let fut = crate::task::wrap(fut);
51    crate::rt::Runtime::with_current(|rt| unsafe { rt.spawn_detached_at(fut, location) });
52}