datex_core/runtime/
global_context.rs1use crate::stdlib::sync::Arc;
2use crate::{crypto::crypto::CryptoTrait, utils::time::TimeTrait};
3use core::prelude::rust_2024::*;
4#[cfg(feature = "debug")]
5#[derive(Clone, Debug)]
6pub struct DebugFlags {
7 pub allow_unsigned_blocks: bool,
8 pub enable_deterministic_behavior: bool,
9}
10#[cfg(feature = "debug")]
11impl Default for DebugFlags {
12 fn default() -> Self {
13 DebugFlags {
14 allow_unsigned_blocks: true,
15 enable_deterministic_behavior: true,
16 }
17 }
18}
19
20#[derive(Clone)]
21pub struct GlobalContext {
22 pub crypto: Arc<dyn CryptoTrait>,
23 pub time: Arc<dyn TimeTrait>,
24
25 #[cfg(feature = "debug")]
26 pub debug_flags: DebugFlags,
27}
28
29impl GlobalContext {
30 pub fn new(
31 crypto: Arc<dyn CryptoTrait>,
32 time: Arc<dyn TimeTrait>,
33 ) -> GlobalContext {
34 GlobalContext {
35 crypto,
36 time,
37 #[cfg(feature = "debug")]
38 debug_flags: DebugFlags::default(),
39 }
40 }
41
42 #[cfg(all(
43 feature = "native_crypto",
44 feature = "std",
45 feature = "native_time"
46 ))]
47 pub fn native() -> GlobalContext {
48 use crate::{
49 crypto::crypto_native::CryptoNative, utils::time_native::TimeNative,
50 };
51 GlobalContext {
52 crypto: Arc::new(CryptoNative),
53 time: Arc::new(TimeNative),
54 #[cfg(feature = "debug")]
55 debug_flags: DebugFlags::default(),
56 }
57 }
58}
59
60#[cfg_attr(not(feature = "embassy_runtime"), thread_local)]
61pub static mut GLOBAL_CONTEXT: Option<GlobalContext> = None;
62
63pub fn set_global_context(c: GlobalContext) {
64 unsafe {
65 GLOBAL_CONTEXT.replace(c);
66 }
67}
68pub(crate) fn get_global_context() -> GlobalContext {
69 unsafe {
70 GLOBAL_CONTEXT.clone().expect(
71 "Global context not initialized - call set_global_context first!",
72 )
73 }
74}