try_drop/handlers/fallback/
global.rs

1//! Manage the global fallback handler.
2
3use super::DefaultOnUninit;
4use crate::handlers::common::global::{Global as GenericGlobal, GlobalDefinition};
5use crate::handlers::common::handler::CommonHandler;
6use crate::handlers::common::Fallback;
7use crate::handlers::common::Global as GlobalScope;
8use crate::handlers::fallback::Abstracter;
9use crate::handlers::on_uninit::{FlagOnUninit, PanicOnUninit};
10use crate::handlers::uninit_error::UninitializedError;
11use crate::{GlobalTryDropStrategy, TryDropStrategy};
12use anyhow::Error;
13use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock};
14use std::boxed::Box;
15
16#[cfg(feature = "ds-panic")]
17use crate::handlers::common::global::DefaultGlobalDefinition;
18
19#[cfg(feature = "ds-panic")]
20use crate::handlers::on_uninit::UseDefaultOnUninit;
21
22/// A fallback handler which uses the global scope.
23pub type GlobalFallbackHandler<OU = DefaultOnUninit> = CommonHandler<OU, GlobalScope, Fallback>;
24
25/// The default global fallback handler.
26pub static DEFAULT_GLOBAL_FALLBACK_HANDLER: GlobalFallbackHandler = GlobalFallbackHandler::DEFAULT;
27
28static FALLBACK_HANDLER: RwLock<Option<Box<dyn GlobalTryDropStrategy>>> =
29    parking_lot::const_rwlock(None);
30
31impl_try_drop_strategy_for!(GlobalFallbackHandler where Scope: GlobalScope);
32
33impl GlobalDefinition for Fallback {
34    const UNINITIALIZED_ERROR: &'static str = "the global fallback handler is not initialized yet";
35    type Global = Box<dyn GlobalTryDropStrategy>;
36
37    fn global() -> &'static RwLock<Option<Self::Global>> {
38        &FALLBACK_HANDLER
39    }
40}
41
42#[cfg(feature = "ds-panic")]
43impl DefaultGlobalDefinition for Fallback {
44    fn default() -> Self::Global {
45        Box::new(crate::drop_strategies::PanicDropStrategy::DEFAULT)
46    }
47}
48
49impl<T: GlobalTryDropStrategy> From<T> for Box<dyn GlobalTryDropStrategy> {
50    fn from(t: T) -> Self {
51        Box::new(t)
52    }
53}
54
55type Global = GenericGlobal<Fallback>;
56type BoxDynGlobalTryDropStrategy = Box<dyn GlobalTryDropStrategy>;
57
58global_methods! {
59    Global = Global;
60    GenericStrategy = GlobalTryDropStrategy;
61    DynStrategy = BoxDynGlobalTryDropStrategy;
62    feature = "ds-panic";
63
64    /// Install a new global fallback handler. Must be a dynamic trait object.
65    install_dyn;
66
67    /// Install a new global fallback handler.
68    install;
69
70    /// Try and get a reference to the global fallback handler.
71    ///
72    /// # Errors
73    /// If the global fallback handler is not initialized yet, an error is returned.
74    try_read;
75
76    /// Get a reference to the global fallback handler.
77    ///
78    /// # Panics
79    /// If the global fallback handler is not initialized yet, a panic is raised.
80    read;
81
82    /// Try and get a mutable reference to the global fallback handler.
83    ///
84    /// # Errors
85    /// If the global fallback handler is not initialized yet, an error is returned.
86    try_write;
87
88    /// Get a mutable reference to the global fallback handler.
89    ///
90    /// # Panics
91    /// If the global fallback handler is not initialized yet, a panic is raised.
92    write;
93
94    /// Uninstall the current global fallback handler.
95    uninstall;
96
97    /// Get a reference to the global fallback handler.
98    ///
99    /// If the global fallback handler is not initialized yet, it is initialized with the default
100    /// one.
101    read_or_default;
102
103    /// Get a mutable reference to the global fallback handler.
104    ///
105    /// If the global fallback handler is not initialized yet, it is initialized with the default
106    /// one.
107    write_or_default;
108}