Skip to main content

spatialrust_core/
runtime.rs

1use crate::{DeviceKind, ExecutionPolicy};
2
3/// Runtime boundary for executing spatial algorithms on one backend.
4///
5/// The trait intentionally exposes only backend identity and policy
6/// compatibility. Backend-specific queues, buffers, and transfer operations
7/// belong in the crate that implements the runtime.
8pub trait SpatialRuntime {
9    /// Returns the device kind owned by this runtime.
10    fn device_kind(&self) -> DeviceKind;
11
12    /// Returns whether this runtime can satisfy the requested policy.
13    #[must_use]
14    fn supports_policy(&self, policy: ExecutionPolicy) -> bool {
15        match policy.device_kind() {
16            Some(kind) => kind == self.device_kind(),
17            None => true,
18        }
19    }
20}
21
22/// Host runtime for single-threaded and parallel CPU algorithms.
23#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
24pub struct CpuRuntime;
25
26impl SpatialRuntime for CpuRuntime {
27    fn device_kind(&self) -> DeviceKind {
28        DeviceKind::Cpu
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::{CpuRuntime, SpatialRuntime};
35    use crate::{DeviceKind, ExecutionPolicy};
36
37    #[test]
38    fn cpu_runtime_accepts_cpu_and_auto_policies_only() {
39        let runtime = CpuRuntime;
40        assert_eq!(runtime.device_kind(), DeviceKind::Cpu);
41        assert!(runtime.supports_policy(ExecutionPolicy::CpuSingle));
42        assert!(runtime.supports_policy(ExecutionPolicy::CpuParallel));
43        assert!(runtime.supports_policy(ExecutionPolicy::Auto));
44        assert!(!runtime.supports_policy(ExecutionPolicy::Gpu(DeviceKind::Wgpu)));
45    }
46}