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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]

extern crate alloc;

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

#[cfg(feature = "std")]
pub use sync::{LazyMap, OnceMap};

pub mod unsync;

mod map;

#[cfg(test)]
mod tests;

use core::{
    borrow::Borrow,
    hash::{BuildHasher, Hash, Hasher},
};

/// Generalization of `Borrow` that works with more types.
pub trait Equivalent<K: ?Sized> {
    fn equivalent(&self, key: &K) -> bool;
}

impl<Q, K> Equivalent<K> for Q
where
    Q: Eq + ?Sized,
    K: Borrow<Q> + ?Sized,
{
    fn equivalent(&self, key: &K) -> bool {
        self == key.borrow()
    }
}

/// Generalization of `ToOwned` that works with more types.
pub trait ToOwnedEquivalent<K>: Equivalent<K> {
    fn to_owned_equivalent(&self) -> K;
}

impl<Q> ToOwnedEquivalent<Q::Owned> for Q
where
    Q: alloc::borrow::ToOwned + Eq + ?Sized,
{
    fn to_owned_equivalent(&self) -> Q::Owned {
        self.to_owned()
    }
}

fn hash_one<S: BuildHasher, Q: Hash + ?Sized>(hash_builder: &S, key: &Q) -> u64 {
    let mut hasher = hash_builder.build_hasher();
    key.hash(&mut hasher);
    hasher.finish()
}

trait InfallibleResult {
    type Ok;

    fn unwrap_infallible(self) -> Self::Ok;
}

impl<T> InfallibleResult for Result<T, core::convert::Infallible> {
    type Ok = T;

    #[inline]
    fn unwrap_infallible(self) -> T {
        match self {
            Ok(v) => v,
            Err(void) => match void {},
        }
    }
}

#[cfg(feature = "ahash")]
use ahash::{AHasher as HasherInner, RandomState as RandomStateInner};

#[cfg(all(not(feature = "ahash"), feature = "std"))]
use std::collections::hash_map::{DefaultHasher as HasherInner, RandomState as RandomStateInner};

#[cfg(all(not(feature = "ahash"), not(feature = "std")))]
compile_error!("Either feature `ahash` or `std` must be enabled");

/// The default hasher used by this crate.
#[derive(Debug, Clone)]
pub struct RandomState(RandomStateInner);

#[derive(Debug, Clone, Default)]
pub struct DefaultHasher(HasherInner);

impl RandomState {
    #[inline]
    pub fn new() -> Self {
        Self(RandomStateInner::new())
    }
}

impl Default for RandomState {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl core::hash::BuildHasher for RandomState {
    type Hasher = DefaultHasher;

    #[inline]
    fn build_hasher(&self) -> Self::Hasher {
        DefaultHasher(self.0.build_hasher())
    }
}

impl core::hash::Hasher for DefaultHasher {
    #[inline]
    fn finish(&self) -> u64 {
        self.0.finish()
    }

    #[inline]
    fn write(&mut self, bytes: &[u8]) {
        self.0.write(bytes)
    }

    #[inline]
    fn write_u8(&mut self, i: u8) {
        self.0.write_u8(i)
    }

    #[inline]
    fn write_u16(&mut self, i: u16) {
        self.0.write_u16(i)
    }

    #[inline]
    fn write_u32(&mut self, i: u32) {
        self.0.write_u32(i)
    }

    #[inline]
    fn write_u64(&mut self, i: u64) {
        self.0.write_u64(i)
    }

    #[inline]
    fn write_u128(&mut self, i: u128) {
        self.0.write_u128(i)
    }

    #[inline]
    fn write_usize(&mut self, i: usize) {
        self.0.write_usize(i)
    }
}

/// ```compile_fail
/// fn assert_send<T: Send>() {}
/// assert_send::<once_map::sync::ReadOnlyView<(), (), once_map::RandomState>>();
/// ```
struct PhantomUnsend(core::marker::PhantomData<*const ()>);
unsafe impl Sync for PhantomUnsend {}