try_drop/handlers/fallback/
global.rs1use 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
22pub type GlobalFallbackHandler<OU = DefaultOnUninit> = CommonHandler<OU, GlobalScope, Fallback>;
24
25pub 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_dyn;
66
67 install;
69
70 try_read;
75
76 read;
81
82 try_write;
87
88 write;
93
94 uninstall;
96
97 read_or_default;
102
103 write_or_default;
108}