1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! This crate provides a set of types that abstract over OS-specific platform
//! primitives. It is focused on IO- and wait-related functionality: events,
//! timers, and polling.
//!
//! As a convenience, it also exports some OS-specific functionality and some
//! general library functionality.

mod headervec;
pub mod process;
pub mod unix;
pub mod windows;

pub use headervec::HeaderVec;
pub use sys::close_stdout;
pub use sys::pipe::pair as pipe_pair;

#[cfg(unix)]
use unix as sys;
#[cfg(windows)]
use windows as sys;

/// Runs a closure when the instance goes out of scope.
pub struct ScopeExit<F: FnOnce()> {
    func: Option<F>,
}

impl<F: FnOnce()> ScopeExit<F> {
    /// Creates a new `ScopeExit`.
    pub fn new(func: F) -> Self {
        Self { func: Some(func) }
    }

    /// Prevents the closure from running when the instance goes out of scope.
    ///
    /// This function takes ownership so that any variables borrowed by the closure will be
    /// usable again after calling cancel.
    pub fn dismiss(mut self) {
        self.func = None;
    }
}

impl<F: FnOnce()> Drop for ScopeExit<F> {
    fn drop(&mut self) {
        if let Some(func) = self.func.take() {
            func();
        }
    }
}