tokise/
runtime.rs

1use std::future::Future;
2use std::io::Result;
3use std::marker::PhantomData;
4
5/// Spawns a task on current thread.
6///
7/// # Panics
8///
9/// This function will panic when not being executed from within the [Runtime].
10#[inline(always)]
11pub fn spawn_local<F>(f: F)
12where
13    F: Future<Output = ()> + 'static,
14{
15    crate::imp::spawn_local(f);
16}
17
18/// A Runtime Builder.
19#[derive(Debug)]
20pub struct RuntimeBuilder {
21    worker_threads: usize,
22}
23
24impl Default for RuntimeBuilder {
25    fn default() -> Self {
26        Self {
27            worker_threads: crate::imp::get_default_runtime_size(),
28        }
29    }
30}
31
32impl RuntimeBuilder {
33    /// Creates a new Runtime Builder.
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    /// Sets the number of worker threads the Runtime will use.
39    ///
40    /// # Default
41    ///
42    /// The default number of worker threads is the number of available logical CPU cores.
43    ///
44    /// # Note
45    ///
46    /// This setting has no effect if current platform has no thread support (e.g.: WebAssembly).
47    pub fn worker_threads(&mut self, val: usize) -> &mut Self {
48        self.worker_threads = val;
49
50        self
51    }
52
53    /// Creates a Runtime.
54    pub fn build(&mut self) -> Result<Runtime> {
55        Ok(Runtime {
56            inner: crate::imp::Runtime::new(self.worker_threads)?,
57        })
58    }
59}
60
61/// An asynchronous Runtime.
62#[derive(Debug, Clone, Default)]
63pub struct Runtime {
64    inner: crate::imp::Runtime,
65}
66
67impl Runtime {
68    /// Creates a runtime Builder.
69    pub fn builder() -> RuntimeBuilder {
70        RuntimeBuilder::new()
71    }
72
73    /// Spawns a task with it pinned to a worker thread.
74    ///
75    /// This can be used to execute non-Send futures without blocking the current thread.
76    ///
77    /// [`spawn_local`] is available with tasks executed with `spawn_pinned`.
78    #[inline(always)]
79    pub fn spawn_pinned<F, Fut>(&self, create_task: F)
80    where
81        F: FnOnce() -> Fut,
82        F: Send + 'static,
83        Fut: Future<Output = ()> + 'static,
84    {
85        self.inner.spawn_pinned(create_task);
86    }
87}
88
89/// A Local Runtime Handle.
90///
91/// This type can be used to acquire a runtime handle to spawn local tasks.
92#[derive(Debug, Clone)]
93pub struct LocalHandle {
94    inner: crate::imp::LocalHandle,
95    // This type is not send or sync.
96    _marker: PhantomData<*const ()>,
97}
98
99impl LocalHandle {
100    /// Creates a Handle to current Runtime worker.
101    ///
102    /// # Panics
103    ///
104    /// This method will panic if not called from within the [Runtime].
105    pub fn current() -> Self {
106        let inner = crate::imp::LocalHandle::current();
107
108        Self {
109            inner,
110            _marker: PhantomData,
111        }
112    }
113
114    /// Creates a Handle to current Runtime worker.
115    ///
116    /// This methods will return `None` if called from outside the [Runtime].
117    pub fn try_current() -> Option<Self> {
118        let inner = crate::imp::LocalHandle::try_current()?;
119
120        Some(Self {
121            inner,
122            _marker: PhantomData,
123        })
124    }
125
126    /// Spawns a Future with current [Runtime] worker.
127    #[inline(always)]
128    pub fn spawn_local<F>(&self, f: F)
129    where
130        F: Future<Output = ()> + 'static,
131    {
132        self.inner.spawn_local(f);
133    }
134}