try_drop/drop_strategies/
exit.rs

1use crate::TryDropStrategy;
2use std::process;
3
4/// A drop strategy which exits the program with a specific exit code if the drop fails.
5#[cfg_attr(
6    feature = "derives",
7    derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)
8)]
9pub struct ExitDropStrategy {
10    /// The exit code to use if the drop fails.
11    pub exit_code: i32,
12}
13
14impl ExitDropStrategy {
15    /// The default exit drop strategy.
16    pub const DEFAULT: Self = Self::new(1);
17
18    /// Create a new exit drop strategy.
19    pub const fn new(exit_code: i32) -> Self {
20        Self { exit_code }
21    }
22}
23
24impl Default for ExitDropStrategy {
25    fn default() -> Self {
26        Self::DEFAULT
27    }
28}
29
30impl TryDropStrategy for ExitDropStrategy {
31    fn handle_error(&self, _error: crate::Error) {
32        process::exit(self.exit_code)
33    }
34}
35
36// it is not possible to create tests for this strategy because this exits the program, which can't
37// be caught.