use std::sync::Arc;
use cordis::Context;
use std::sync::atomic::AtomicBool;
use cordis::Service;
pub struct EmergencyStop {
flag: AtomicBool,
}
impl EmergencyStop {
pub fn new(active: bool) -> Self {
Self {
flag: AtomicBool::new(active),
}
}
pub fn is_active(&self) -> bool {
self.flag.load(std::sync::atomic::Ordering::Relaxed)
}
pub fn set_active(&self, active: bool) {
self.flag
.store(active, std::sync::atomic::Ordering::Relaxed)
}
}
impl Service for EmergencyStop {
fn name(&self) -> &'static str {
"emergency_stop"
}
fn init(&self, _ctx: &Arc<Context>) -> cordis::ServiceInitFuture<'_> {
Box::pin(async { Ok(None) })
}
fn check(&self) -> bool {
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use cordis::Context;
#[test]
fn emergency_stop_readable_via_cordis() {
let ctx = Context::new_root();
ctx.provide(EmergencyStop::new(false));
let got = ctx.get::<EmergencyStop>().expect("provided");
assert!(!got.is_active());
got.set_active(true);
assert!(got.is_active());
}
}