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
17pub use sys::close_stdout;
18pub use sys::pipe::pair as pipe_pair;
19
20#[cfg(unix)]
21use unix as sys;
22#[cfg(windows)]
23use windows as sys;
24
25/// Runs a closure when the instance goes out of scope.
26pub struct ScopeExit<F: FnOnce()> {
27    func: Option<F>,
28}
29
30impl<F: FnOnce()> ScopeExit<F> {
31    /// Creates a new `ScopeExit`.
32    pub fn new(func: F) -> Self {
33        Self { func: Some(func) }
34    }
35
36    /// Prevents the closure from running when the instance goes out of scope.
37    ///
38    /// This function takes ownership so that any variables borrowed by the closure will be
39    /// usable again after calling cancel.
40    pub fn dismiss(mut self) {
41        self.func = None;
42    }
43}
44
45impl<F: FnOnce()> Drop for ScopeExit<F> {
46    fn drop(&mut self) {
47        if let Some(func) = self.func.take() {
48            func();
49        }
50    }
51}