conquer_once/
noblock.rs

1//! Synchronized one-time and lazy initialization primitives that permit only
2//! non-blocking synchronized initialization operations.
3
4use crate::{cell::Unblock, state::BlockedState};
5
6use self::internal::NoBlock;
7
8/// A type for lazy initialization of e.g. global static variables, which
9/// provides the same functionality as the `lazy_static!` macro.
10///
11/// This type does not permit any (potentially) blocking operations, only their
12/// respective non-blocking counterparts and is thus `#[no_std]` compatible.
13///
14/// For the API of this type alias, see the API of the generic
15/// [`Lazy`](crate::lazy::Lazy) type.
16pub type Lazy<T, F = fn() -> T> = crate::lazy::Lazy<T, NoBlock, F>;
17
18/// An interior mutability cell type which allows synchronized one-time
19/// initialization and read-only access exclusively after initialization.
20///
21/// This type does not permit any (potentially) blocking operations, only their
22/// respective non-blocking counterparts and is thus `#[no_std]` compatible.
23///
24/// For the API of this type alias, see the generic
25/// [`OnceCell`](crate::doc::OnceCell) type.
26pub type OnceCell<T> = crate::cell::OnceCell<T, NoBlock>;
27
28/// A synchronization primitive which can be used to run a one-time global
29/// initialization.
30///
31/// This type does not permit any (potentially) blocking operations, only their
32/// respective non-blocking counterparts and is thus `#[no_std]` compatible.
33///
34/// For the API of this type alias, see the generic
35/// [`OnceCell`](crate::doc::OnceCell) type.
36/// This is a specialization with `T = ()`.
37pub type Once = crate::cell::OnceCell<(), NoBlock>;
38
39mod internal {
40    /// "Blocking" strategy which does not actually allow blocking.
41    #[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
42    pub struct NoBlock;
43}
44
45impl Unblock for NoBlock {
46    #[inline(always)]
47    unsafe fn on_unblock(_: BlockedState) {}
48}
49
50#[cfg(test)]
51mod tests {
52    generate_tests_non_blocking!();
53}