Skip to main content

stynx_code_services/prevent_sleep/
mod.rs

1use stynx_code_errors::AppResult;
2
3pub trait SleepInhibitor: Send + Sync {
4    fn inhibit(&self) -> AppResult<SleepGuard>;
5}
6
7pub struct SleepGuard {
8    _private: (),
9}
10
11impl SleepGuard {
12    fn new() -> Self {
13        tracing::info!("sleep inhibited");
14        Self { _private: () }
15    }
16}
17
18impl Drop for SleepGuard {
19    fn drop(&mut self) {
20        tracing::info!("sleep re-enabled");
21    }
22}
23
24pub struct NoopInhibitor;
25
26impl NoopInhibitor {
27    pub fn new() -> Self {
28        Self
29    }
30}
31
32impl SleepInhibitor for NoopInhibitor {
33    fn inhibit(&self) -> AppResult<SleepGuard> {
34        tracing::warn!("using noop sleep inhibitor; system may still sleep");
35        Ok(SleepGuard::new())
36    }
37}