Skip to main content

plato_engine_block/
sensor.rs

1//! Sensor — callback-based readers that return f64.
2
3/// A sensor callback: takes nothing, returns an f64 reading.
4pub trait Sensor {
5    fn read(&self) -> f64;
6}
7
8/// Type-erased sensor callback.
9pub type SensorFn = Box<dyn Fn() -> f64 + Send + Sync>;
10
11/// A named sensor with its callback.
12pub struct SensorSpec {
13    pub name: String,
14    pub callback: SensorFn,
15}
16
17impl SensorSpec {
18    pub fn new(name: impl Into<String>, callback: SensorFn) -> Self {
19        SensorSpec {
20            name: name.into(),
21            callback,
22        }
23    }
24}
25
26impl core::fmt::Debug for SensorSpec {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        f.debug_struct("SensorSpec")
29            .field("name", &self.name)
30            .finish()
31    }
32}
33
34/// Blanket impl for closures.
35impl<F: Fn() -> f64 + Send + Sync + 'static> Sensor for F {
36    fn read(&self) -> f64 {
37        self()
38    }
39}