#[cfg_attr(macos, path = "mac.rs")] #[cfg_attr(windows, path = "windows.rs")] #[cfg_attr(noop, path = "noop.rs")] mod imp; use std::{ pin::Pin, task::{Context, Poll}, }; use pin_project::pin_project; pub use imp::Guard; /// Returns a guard that prevents the system from going to sleep while the guard is held. /// /// Callers should provide a description of the reason for preventing sleep. Depending on /// platform, this may appear in logs, so write it as though it may be user-visible, e.g.: /// "Agent Mode request in-progress". pub fn prevent_sleep(reason: &'static str) -> Guard { imp::prevent_sleep(reason) } /// A simple wrapper around a stream that optionally prevents the system from going to sleep /// while the stream is being polled. #[pin_project] pub struct Stream { #[pin] inner: S, guard: Option, } impl Stream { /// Wraps the provided stream, maintaining the provided sleep guard as long as the stream is /// being polled. pub fn wrap(inner: S, guard: Option) -> Self { Self { inner, guard } } } impl futures::stream::Stream for Stream { type Item = S::Item; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { self.project().inner.poll_next(cx) } }