reactive_signals/runtimes/
mod.rs

1//!
2//! Runtimes are the starting point of a reactive-signals based application. Internally, the runtimes presents a
3//! simple boolean constant to let the [Scope](crate::Scope)s and [Signal](crate::Signal)s know
4//! where they are running. Like that a signal marked with `server` or `client` knows if it should run.
5//!
6//! There are two types of runtimes:
7//!
8//! - Pooled runtimes: Allows for many runtimes in a thread.
9//! - Single runtimes: Limitied to one runtime per thread.
10//!
11//! A runtime presents a single function: `new_root_scope()` which returns a root [Scope](crate::Scope).
12//! When the root scope is discarded, using it's [discard()](crate::Scope::discard()) function, the
13//! runtime is discarded as well.
14//!
15//! Single runtimes have no memory overhead, whereas pooled runtimes have an overhead of 2 bytes
16//! which is the index in the pool. As a consequence a pool can have at most 65k runtimes.
17//!
18mod client;
19mod inner;
20mod server;
21mod test_client;
22// mod staticrt;
23
24use crate::Scope;
25pub use client::ClientRuntime;
26pub(crate) use inner::RuntimeInner;
27pub use server::ServerRuntime;
28pub use test_client::TestClientRuntime;
29// pub use staticrt::{StaticRuntime, StaticRuntimeId};
30
31#[doc(hidden)]
32pub trait Runtime: Default + Copy + 'static {
33    const IS_SERVER: bool;
34
35    fn with_ref<F, T>(&self, f: F) -> T
36    where
37        F: FnOnce(&RuntimeInner<Self>) -> T;
38
39    fn with_mut<F, T>(&self, f: F) -> T
40    where
41        F: FnOnce(&mut RuntimeInner<Self>) -> T;
42
43    fn discard(&self) {
44        self.with_mut(|rt| rt.discard());
45    }
46}