Skip to main content

tilo_rt/
caps.rs

1//! What this crate provides on each target, as data an app can query.
2
3pub use tilo_platform_caps::{Availability, availability, available};
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5#[non_exhaustive]
6pub enum Feature {
7    Threads,
8    BlockOn,
9    DaemonRuntime,
10    Io,
11}
12
13const WEB_THREADS: &str = "A browser page has no threads of its own.";
14const WEB_BLOCK_ON: &str = "A browser page's one thread cannot wait on work.";
15const WEB_DAEMON: &str = "A browser page cannot run a multi-threaded runtime.";
16const WEB_IO: &str = "A browser page has no file-I/O threads.";
17
18impl tilo_platform_caps::Feature for Feature {
19    /// The feature's availability on Linux, macOS and the web, in that
20    /// order.
21    fn targets(self) -> [Availability; 3] {
22        use Availability::{Available, Unsupported};
23        match self {
24            Self::Threads => [Available, Available, Unsupported(WEB_THREADS)],
25            Self::BlockOn => [Available, Available, Unsupported(WEB_BLOCK_ON)],
26            Self::DaemonRuntime => [Available, Available, Unsupported(WEB_DAEMON)],
27            Self::Io => [Available, Available, Unsupported(WEB_IO)],
28        }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    tilo_test_env::scenario! {
37        fn threads_and_block_on_agree_with_their_availability;
38
39        phase spawn_thread_agrees_with_threads_availability;
40        {
41            let spawned = crate::spawn_thread("tilo-rt-caps-test", || 5);
42            match availability(Feature::Threads) {
43                Availability::Available | Availability::Prompt => {
44                    assert_eq!(spawned.unwrap().join().unwrap(), 5);
45                }
46                Availability::Unsupported(_) => assert!(spawned.is_err()),
47            }
48        }
49
50        phase block_on_agrees_with_block_on_availability;
51        {
52            let result = crate::block_on(async { 3 });
53            match availability(Feature::BlockOn) {
54                Availability::Available | Availability::Prompt => assert_eq!(result.unwrap(), 3),
55                Availability::Unsupported(_) => assert!(result.is_err()),
56            }
57        }
58    }
59}