Skip to main content

datex_core/
lib.rs

1//! The `datex-core` crate is the core library for the DATEX system.
2#![cfg_attr(not(feature = "std"), no_std)]
3// Features required for the crate.
4#![feature(gen_blocks)]
5#![feature(async_iterator)]
6#![feature(deref_patterns)]
7#![feature(negative_impls, with_negative_coherence)]
8#![feature(core_float_math)]
9#![feature(variant_count)]
10#![feature(const_cmp)]
11#![feature(const_trait_impl)]
12#![feature(specialization)]
13#![feature(custom_inner_attributes)]
14#![feature(decl_macro)]
15#![feature(try_blocks)]
16#![rustfmt::skip::macros(assert_regular_instructions_equal)]
17#![feature(arbitrary_self_types)]
18// Allowances for various compiler features and warnings.
19#![allow(incomplete_features)]
20#![allow(static_mut_refs)]
21#![allow(async_fn_in_trait)]
22
23extern crate alloc;
24extern crate num_integer;
25
26#[doc(hidden)]
27#[macro_use]
28mod preludes;
29
30#[doc = include_str!("../README.md")]
31#[cfg(doctest)]
32pub struct ReadmeDoctests;
33
34#[cfg(feature = "std")]
35extern crate std;
36
37pub mod channel;
38pub mod prelude;
39
40#[cfg(feature = "ast")]
41pub mod ast;
42#[cfg(feature = "compiler")]
43pub mod compiler;
44pub mod decompiler;
45#[cfg(feature = "compiler")]
46pub mod fmt;
47pub mod global;
48pub mod instruction;
49pub mod libs;
50#[cfg(all(feature = "lsp", feature = "std"))]
51pub mod lsp;
52pub mod network;
53#[cfg(feature = "parser")]
54pub mod parser;
55pub mod runtime;
56pub mod shared_values;
57#[cfg(feature = "compiler")]
58pub mod type_inference;
59#[cfg(feature = "compiler")]
60pub mod visitor;
61
62pub mod core_compiler;
63pub mod dif;
64pub mod disassembler;
65pub mod dxb_parser;
66mod stub;
67pub mod task;
68pub mod traits;
69pub mod types;
70pub mod utils;
71pub mod value_updates;
72pub mod values;
73
74// reexport macros
75pub use datex_macros_internal as macros;
76extern crate core;
77
78pub mod datex_registry;
79pub mod inspector;
80pub mod serde_compat;
81pub mod shared_wrappers;
82
83pub use inventory;
84pub use linkme;
85
86pub use serde;
87
88#[cfg(feature = "debug_dhat")]
89#[global_allocator]
90static ALLOC: dhat::Alloc = dhat::Alloc;
91
92#[cfg(feature = "debug_dhat")]
93pub use dhat;
94
95/// HashMap and HashSet that work in both std and no_std environments.
96pub mod collections {
97    cfg_if::cfg_if! {
98        if #[cfg(feature = "std")] {
99            pub use std::collections::{HashMap, HashSet, hash_map, hash_set};
100            pub use std::collections::hash_map::DefaultHasher;
101            pub fn default_hasher() -> DefaultHasher {
102                DefaultHasher::new()
103            }
104        } else {
105            use core::hash::BuildHasher;
106            pub use hashbrown::{HashMap, HashSet, hash_map, hash_set, DefaultHashBuilder, DefaultHasher};
107            pub fn default_hasher() -> DefaultHasher {
108                DefaultHashBuilder::default().build_hasher()
109            }
110        }
111    }
112}
113
114/// Reexport of Mutex that works in both std and no_std environments.
115pub mod std_sync {
116    cfg_if::cfg_if! {
117        if #[cfg(feature = "std")] {
118            pub use std::sync::Mutex;
119        } else {
120            pub use spin::Mutex;
121        }
122    }
123}
124
125/// Crypto implementations selection based on target architecture and features.
126pub mod crypto {
127    cfg_if::cfg_if! {
128        if #[cfg(any(feature = "target_native", test))] {
129            pub use datex_crypto_native::CryptoNative as CryptoImpl;
130        } else if #[cfg(feature = "target_esp_shared")] {
131            pub use datex_crypto_esp32::CryptoEsp32 as CryptoImpl;
132        } else if #[cfg(feature = "target_wasm")] {
133            pub use datex_crypto_web::CryptoWeb as CryptoImpl;
134        } else {
135            pub use crate::stub::crypto::CryptoStub as CryptoImpl;
136        }
137    }
138}
139
140pub mod time {
141    mod system_time {
142        cfg_if::cfg_if! {
143            if #[cfg(feature = "target_wasm")] {
144                pub use web_time::{SystemTime, UNIX_EPOCH};
145            } else if #[cfg(feature = "target_native")] {
146                pub use std::time::{SystemTime, UNIX_EPOCH};
147            }
148        }
149    }
150
151    cfg_if::cfg_if! {
152        if #[cfg(feature = "target_wasm")] {
153            pub use web_time::{Instant};
154        } else if #[cfg(feature = "std")] {
155            pub use std::time::{Instant};
156        } else if #[cfg(feature = "embassy_runtime")] {
157            pub use embassy_time::{Instant};
158        } else {
159            pub use crate::stub::time::{Instant};
160        }
161    }
162
163    // current unix timestamp in milliseconds
164    pub fn now_ms() -> u64 {
165        cfg_if::cfg_if! {
166            if #[cfg(any(feature = "target_wasm", feature = "target_native"))] {
167                use system_time::{SystemTime, UNIX_EPOCH};
168                SystemTime::now()
169                    .duration_since(UNIX_EPOCH)
170                    .expect("System time is before UNIX_EPOCH")
171                    .as_millis() as u64
172            } else if #[cfg(feature = "target_esp_shared")] {
173                datex_crypto_esp32::now_ms()
174            } else {
175                Instant::now().elapsed().as_millis() as u64
176            }
177        }
178    }
179}
180
181pub mod random {
182    cfg_if::cfg_if! {
183        if #[cfg(feature = "std")] {
184            pub use std::hash::RandomState;
185        } else {
186            pub use foldhash::fast::RandomState;
187        }
188    }
189}