try_drop/handlers/primary/
global.rs

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