1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#![doc = include_str!("../README.md")]
#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "nightly", feature(allocator_api))]

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
macro_rules! with_global_default {
    ($(#[$meta:meta])* $v:vis struct $name:ident<$($lt:lifetime,)* $($generic:ident $(: $bound:path $(: $bounds:path )*)? $(= +$global:ty)? $(= $gtype:ty)?),+> { $($fvis:vis $fname:ident: $ftype:ty),* $(,)? }) => {
        $(#[$meta])*
        $v struct $name<$($lt,)* $($generic $(: $bound $(+ $bounds)*)? $(= $global)? $(= $gtype)?)+> {
            $($fvis $fname: $ftype,)*
        }
    };
}

#[cfg(not(feature = "alloc"))]
macro_rules! with_global_default {
    ($(#[$meta:meta])* $v:vis struct $name:ident<$($lt:lifetime,)* $($generic:ident $(: $bound:path $(: $bounds:path )*)? $(= +$global:ty)? $(= $gtype:ty)?),+> { $($fvis:vis $fname:ident: $ftype:ty),* $(,)? }) => {
        $(#[$meta])*
        $v struct $name<$($lt,)* $($generic $(: $bound $(+ $bounds)*)? $(= $gtype)?)+> {
            $($fvis $fname: $ftype,)*
        }
    };
}

mod api;
mod arena;
mod blink;
mod drop_list;
mod local;

#[cfg(feature = "sync")]
mod sync;

#[cfg(all(feature = "sync", feature = "alloc"))]
mod cache;

#[cfg(test)]
mod tests;

#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
mod oom;

pub use self::{
    api::BlinkAllocator,
    blink::{Blink, Emplace, SendBlink},
    local::BlinkAlloc,
};

#[cfg(feature = "sync")]
pub use self::sync::{LocalBlinkAlloc, SyncBlinkAlloc};

#[cfg(all(feature = "sync", feature = "alloc"))]
pub use self::cache::BlinkAllocCache;

pub(crate) trait ResultExt<T> {
    fn safe_ok(self) -> T;
}

impl<T> ResultExt<T> for Result<T, core::convert::Infallible> {
    #[inline]
    fn safe_ok(self) -> T {
        match self {
            Ok(value) => value,
            Err(never) => match never {},
        }
    }
}

#[inline]
unsafe fn in_place<'a, T, I>(ptr: *mut T, init: I, f: impl FnOnce(I) -> T) -> &'a mut T {
    // Ask compiler very nicely to store return directly into memory.
    core::ptr::write(ptr, f(init));
    &mut *ptr
}