nosleep/lib.rs
1//! Cross-platform library to block the
2//! power save functionality in the OS.
3//!
4//! ```rust
5//! # use std::error::Error;
6//! # use std::{time::Duration, thread};
7//! # use nosleep::*;
8//! # fn main() -> Result<(), Box<dyn Error>> {
9//! let nosleep = NoSleep::new()?;
10//! nosleep.start(NoSleepType::PreventUserIdleDisplaySleep)?;
11//! // Depending on the platform, the block will hold
12//! // until either nosleep will be dropped (Linux)
13//! // or the process exits (macOS) or you manually
14//! // call `nosleep.stop()`
15//! # Ok(())
16//! # }
17//! ```
18
19pub use nosleep_types::NoSleepType;
20
21#[cfg(target_os = "macos")]
22pub use nosleep_mac_sys::*;
23
24#[cfg(target_os = "linux")]
25pub use nosleep_nix::*;
26
27#[cfg(target_os = "windows")]
28pub use nosleep_windows::*;
29
30#[cfg(test)]
31mod tests {
32 use crate::*;
33
34 #[test]
35 fn test_block_platform_agnostic() {
36 let mut nosleep = NoSleep::new().unwrap();
37 nosleep
38 .start(NoSleepType::PreventUserIdleDisplaySleep)
39 .unwrap();
40 std::thread::sleep(std::time::Duration::from_millis(2000));
41 nosleep.stop().unwrap();
42 }
43}