pal/lib.rs
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! This crate provides a set of types that abstract over OS-specific platform
5//! primitives. It is focused on IO- and wait-related functionality: events,
6//! timers, and polling.
7//!
8//! As a convenience, it also exports some OS-specific functionality and some
9//! general library functionality.
10
11#![expect(missing_docs)]
12
13pub mod process;
14pub mod unix;
15pub mod windows;
16
17// Re-export windows_sys and windows_result so that the delayload! macro works
18// when used in other crates
19#[cfg(windows)]
20#[doc(hidden)]
21pub use windows_result;
22#[cfg(windows)]
23#[doc(hidden)]
24pub use windows_sys;
25
26pub use sys::close_stdout;
27pub use sys::pipe::pair as pipe_pair;
28
29#[cfg(unix)]
30use unix as sys;
31#[cfg(windows)]
32use windows as sys;
33
34/// Runs a closure when the instance goes out of scope.
35pub struct ScopeExit<F: FnOnce()> {
36 func: Option<F>,
37}
38
39impl<F: FnOnce()> ScopeExit<F> {
40 /// Creates a new `ScopeExit`.
41 pub fn new(func: F) -> Self {
42 Self { func: Some(func) }
43 }
44
45 /// Prevents the closure from running when the instance goes out of scope.
46 ///
47 /// This function takes ownership so that any variables borrowed by the closure will be
48 /// usable again after calling cancel.
49 pub fn dismiss(mut self) {
50 self.func = None;
51 }
52}
53
54impl<F: FnOnce()> Drop for ScopeExit<F> {
55 fn drop(&mut self) {
56 if let Some(func) = self.func.take() {
57 func();
58 }
59 }
60}