pub use tilo_platform_caps::{Availability, availability, available};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Feature {
Threads,
BlockOn,
DaemonRuntime,
Io,
}
const WEB_THREADS: &str = "A browser page has no threads of its own.";
const WEB_BLOCK_ON: &str = "A browser page's one thread cannot wait on work.";
const WEB_DAEMON: &str = "A browser page cannot run a multi-threaded runtime.";
const WEB_IO: &str = "A browser page has no file-I/O threads.";
impl tilo_platform_caps::Feature for Feature {
fn targets(self) -> [Availability; 3] {
use Availability::{Available, Unsupported};
match self {
Self::Threads => [Available, Available, Unsupported(WEB_THREADS)],
Self::BlockOn => [Available, Available, Unsupported(WEB_BLOCK_ON)],
Self::DaemonRuntime => [Available, Available, Unsupported(WEB_DAEMON)],
Self::Io => [Available, Available, Unsupported(WEB_IO)],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
tilo_test_env::scenario! {
fn threads_and_block_on_agree_with_their_availability;
phase spawn_thread_agrees_with_threads_availability;
{
let spawned = crate::spawn_thread("tilo-rt-caps-test", || 5);
match availability(Feature::Threads) {
Availability::Available | Availability::Prompt => {
assert_eq!(spawned.unwrap().join().unwrap(), 5);
}
Availability::Unsupported(_) => assert!(spawned.is_err()),
}
}
phase block_on_agrees_with_block_on_availability;
{
let result = crate::block_on(async { 3 });
match availability(Feature::BlockOn) {
Availability::Available | Availability::Prompt => assert_eq!(result.unwrap(), 3),
Availability::Unsupported(_) => assert!(result.is_err()),
}
}
}
}