interner 0.2.1

A string, path, and buffer internment crate with no dependencies.
Documentation
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::borrow::Cow;
use std::collections::hash_map::RandomState;
use std::fmt::Debug;
use std::hash::{BuildHasher, Hash};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};

use crate::pool::{Pool, PoolKindSealed, Poolable};
use crate::{PoolKind, Pooled};

/// A pooled string that is stored in a [`GlobalPool`].
///
/// This type implements `From<String>` and `From<&str>`.
pub type GlobalString<S = RandomState> = Pooled<&'static GlobalPool<String, S>, S>;
/// A pooled path that is stored in a [`GlobalPool`].
///
/// This type implements `From<PathBuf>` and `From<&Path>`.
pub type GlobalPath<S = RandomState> = Pooled<&'static GlobalPool<PathBuf, S>, S>;
/// A pooled buffer (`Vec<u8>`) that is stored in a [`GlobalPool`].
///
/// This type implements `From<Vec<u8>>` and `From<&[u8]>`.
pub type GlobalBuffer<S = RandomState> = Pooled<&'static GlobalPool<Vec<u8>, S>, S>;

/// A global string interning pool that manages [`GlobalString`]s.
pub type StringPool<S = RandomState> = GlobalPool<String, S>;
/// A global path interning pool that manages [`GlobalPath`]s.
///
/// Each [`PathPool`] has its own storage. When comparing [`GlobalPath`]s
/// from separate pools, the full string comparison function must be used.
pub type PathPool<S = RandomState> = GlobalPool<PathBuf, S>;
/// A global byte buffer interning pool that manages [`GlobalBuffer`]s.
///
/// Each [`BufferPool`] has its own storage. When comparing [`GlobalBuffer`]s
/// from separate pools, the full string comparison function must be used.
pub type BufferPool<S = RandomState> = GlobalPool<Vec<u8>, S>;

/// A global interned pool.
///
/// This type is used to create globally allocated pools.
///
/// ```rust
/// use interner::global::{GlobalPool, GlobalString};
///
/// static GLOBAL_STRINGS: GlobalPool<String> = GlobalPool::new();
///
/// let interned = GLOBAL_STRINGS.get(String::from("hello"));
/// let second = GLOBAL_STRINGS.get("hello");
///
/// assert!(GlobalString::ptr_eq(&interned, &second));
/// ```
#[derive(Debug)]
pub struct GlobalPool<T, S = RandomState>(Mutex<GlobalPoolState<T, S>>)
where
    T: Poolable + Debug + Clone + Eq + PartialEq + Hash + Ord + PartialOrd + 'static,
    S: BuildHasher + 'static;

#[derive(Debug)]
enum GlobalPoolState<T, S>
where
    &'static GlobalPool<T, S>: PoolKind<S>,
    T: Poolable + Debug + Clone + Eq + PartialEq + Hash + Ord + PartialOrd + 'static,
    S: BuildHasher + 'static,
{
    Initializing,
    LazyInitialize { capacity: usize, hasher: fn() -> S },
    StaticInitialize { capacity: usize, hasher: S },
    Initialized(Pool<&'static GlobalPool<T, S>, S>),
}

impl<T> GlobalPool<T>
where
    T: Poolable + Debug + Clone + Eq + PartialEq + Hash + Ord + PartialOrd,
{
    /// Returns a new instance using [`RandomState`] for the internal hashing.
    #[must_use]
    pub const fn new() -> Self {
        Self::with_capacity_and_hasher_init(0, RandomState::new)
    }
}
impl<T, S> GlobalPool<T, S>
where
    T: Poolable + Debug + Clone + Eq + PartialEq + Hash + Ord + PartialOrd,
    S: BuildHasher,
{
    /// Returns a collection of the currently pooled items.
    #[must_use]
    pub fn pooled<C>(&'static self) -> C
    where
        C: FromIterator<Pooled<&'static Self, S>>,
    {
        self.with_active_symbols(|pool| {
            pool.active
                .iter()
                .map(|data| Pooled(data.clone()))
                .collect()
        })
    }
}
impl<T, S, S2> PartialEq<GlobalPool<T, S2>> for GlobalPool<T, S>
where
    T: Poolable + Poolable + Debug + Clone + Eq + PartialEq + Hash + Ord + PartialOrd,
    S: BuildHasher,
    S2: BuildHasher,
{
    fn eq(&self, other: &GlobalPool<T, S2>) -> bool {
        (self as *const Self).cast::<()>() == (other as *const GlobalPool<T, S2>).cast::<()>()
    }
}

impl<T, S> PoolKindSealed<S> for &'static GlobalPool<T, S>
where
    T: Poolable + Debug + Clone + Eq + PartialEq + Hash + Ord + PartialOrd,
    S: BuildHasher,
{
    type Owned = T;
    type Pooled = T::Boxed;

    fn with_active_symbols<R>(&self, logic: impl FnOnce(&mut Pool<Self, S>) -> R) -> R {
        let mut symbols = self.0.lock().expect("poisoned");
        if !matches!(*symbols, GlobalPoolState::Initialized(_)) {
            let pool = match std::mem::replace(&mut *symbols, GlobalPoolState::Initializing) {
                GlobalPoolState::LazyInitialize { capacity, hasher } => {
                    Pool::with_capacity_and_hasher(capacity, hasher())
                }
                GlobalPoolState::StaticInitialize { capacity, hasher } => {
                    Pool::with_capacity_and_hasher(capacity, hasher)
                }

                _ => unreachable!("invalid state"),
            };
            *symbols = GlobalPoolState::Initialized(pool);
        }

        let GlobalPoolState::Initialized(pool) = &mut *symbols else { unreachable!("always initialized above") };
        logic(pool)
    }

    fn address_of(&self) -> *const () {
        std::ptr::addr_of!(*self).cast()
    }
}

impl<T, S> PoolKind<S> for &'static GlobalPool<T, S>
where
    T: Poolable + Debug + Clone + Eq + PartialEq + Hash + Ord + PartialOrd,
    S: BuildHasher,
{
}

impl<T, S> GlobalPool<T, S>
where
    T: Poolable + Debug + Clone + Eq + PartialEq + Hash + Ord + PartialOrd,
    S: BuildHasher,
{
    /// Returns a new instance using the provided hasher.
    pub const fn with_hasher(hasher: S) -> Self {
        Self::with_capacity_and_hasher(0, hasher)
    }

    /// Returns a new instance using the function to load the hasher when the
    /// pool is initialized on first use.
    pub const fn with_hasher_init(init: fn() -> S) -> Self {
        Self::with_capacity_and_hasher_init(0, init)
    }

    /// Returns a new instance using the provided hasher with enough capacity to
    /// hold the requested number of items without reallocating.
    pub const fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
        Self(Mutex::new(GlobalPoolState::StaticInitialize {
            capacity,
            hasher,
        }))
    }

    /// Returns a new instance using the function to load the hasher when the
    /// pool is initialized on first use. The returned instance has enough
    /// capacity to hold the requested number of items without reallocating.
    pub const fn with_capacity_and_hasher_init(capacity: usize, init: fn() -> S) -> Self {
        Self(Mutex::new(GlobalPoolState::LazyInitialize {
            capacity,
            hasher: init,
        }))
    }
}

impl<S> GlobalPool<String, S>
where
    S: BuildHasher,
{
    /// Returns a copy of an existing [`GlobalString`] if one is found.
    /// Otherwise, a new [`GlobalString`] is created and returned.
    ///
    /// While any copies of the returned [`GlobalString`] are still allocated,
    /// calling this function is guaranteed to return a copy of the same string.
    pub fn get<'a, V>(&'static self, value: V) -> GlobalString<S>
    where
        V: Into<Cow<'a, str>>,
    {
        let value = value.into();
        self.with_active_symbols(|symbols| symbols.get(value, &self))
    }

    /// Returns a static pooled string, which keeps the pooled string allocated
    /// for the duration of the process.
    ///
    /// The string is not initialized until it is retrieved for the first time.
    pub const fn get_static(&'static self, value: &'static str) -> StaticPooledString<S> {
        StaticPooledString::new(self, value)
    }

    /// Returns a static pooled string, which keeps the pooled string allocated
    /// for the duration of the process. The string is initialized using the
    /// function provided when it is retrieved for the first time.
    pub const fn get_static_with(
        &'static self,
        function: fn() -> Cow<'static, str>,
    ) -> StaticPooledString<S> {
        StaticPooledString::new_fn(self, function)
    }
}

impl<S> GlobalPool<PathBuf, S>
where
    S: BuildHasher,
{
    /// Returns a copy of an existing [`GlobalPath`] if one is found.
    /// Otherwise, a new [`GlobalPath`] is created and returned.
    ///
    /// While any copies of the returned [`GlobalPath`] are still allocated,
    /// calling this function is guaranteed to return a copy of the same path.
    pub fn get<'a, V>(&'static self, value: V) -> GlobalPath<S>
    where
        V: Into<Cow<'a, Path>>,
    {
        let value = value.into();
        self.with_active_symbols(|symbols| symbols.get(value, &self))
    }

    // This function serves no purpose, currently, as there's no way to get a
    // static path in a const context -- Path::new() isn't const.
    // /// Returns a static pooled path, which keeps the pooled path allocated for
    // /// the duration of the process.
    // ///
    // /// The path is not initialized until it is retrieved for the first time.
    // pub const fn get_static(&'static self, value: &'static Path) -> StaticPooledPath<S> {
    //     StaticPooledPath::new(self, value)
    // }

    /// Returns a static pooled path, which keeps the pooled path allocated for
    /// the duration of the process. The path is initialized using the function
    /// provided when it is retrieved for the first time.
    pub const fn get_static_with(
        &'static self,
        function: fn() -> Cow<'static, Path>,
    ) -> StaticPooledPath<S> {
        StaticPooledPath::new_fn(self, function)
    }
}

impl<S> GlobalPool<Vec<u8>, S>
where
    S: BuildHasher,
{
    /// Returns a copy of an existing [`GlobalBuffer`] if one is found.
    /// Otherwise, a new [`GlobalBuffer`] is created and returned.
    ///
    /// While any copies of the returned [`GlobalBuffer`] are still allocated,
    /// calling this function is guaranteed to return a copy of the same byte
    /// buffer.
    pub fn get<'a, V>(&'static self, value: V) -> GlobalBuffer<S>
    where
        V: Into<Cow<'a, [u8]>>,
    {
        let value = value.into();
        self.with_active_symbols(|symbols| symbols.get(value, &self))
    }

    /// Returns a static pooled buffer, which keeps the pooled buffer allocated for
    /// the duration of the process.
    ///
    /// The buffer is not initialized until it is retrieved for the first time.
    pub const fn get_static(&'static self, value: &'static [u8]) -> StaticPooledBuffer<S> {
        StaticPooledBuffer::new(self, value)
    }

    /// Returns a static pooled buffer, which keeps the pooled buffer allocated
    /// for the duration of the process. The buffer is initialized using the
    /// function provided when it is retrieved for the first time.
    pub const fn get_static_with(
        &'static self,
        function: fn() -> Cow<'static, [u8]>,
    ) -> StaticPooledBuffer<S> {
        StaticPooledBuffer::new_fn(self, function)
    }
}

/// A lazily-initialized [`GlobalString`] that stays allocated for the duration
/// of the process.
#[derive(Debug)]
pub struct StaticPooledString<S = RandomState>
where
    S: BuildHasher + 'static,
{
    init: StaticStringInit<S>,
    cell: OnceLock<GlobalString<S>>,
}

/// A lazily-initialized [`GlobalBuffer`] that stays allocated for the duration
/// of the process.
#[derive(Debug)]
pub struct StaticPooledBuffer<S = RandomState>
where
    S: BuildHasher + 'static,
{
    init: StaticBufferInit<S>,
    cell: OnceLock<GlobalBuffer<S>>,
}

/// A lazily-initialized [`GlobalPath`] that stays allocated for the duration
/// of the process.
#[derive(Debug)]
pub struct StaticPooledPath<S = RandomState>
where
    S: BuildHasher + 'static,
{
    init: StaticPathInit<S>,
    cell: OnceLock<GlobalPath<S>>,
}

macro_rules! impl_static_pooled {
    ($name:ident, $pooled:ident, $statename:ident, $owned:ty, $borrowed:ty) => {
        impl<S> $name<S>
        where
            S: BuildHasher + 'static,
        {
            #[allow(dead_code)] // This function isn't called for StaticPooledPath, because there's no way to get a static Path.
            const fn new(pool: &'static GlobalPool<$owned, S>, value: &'static $borrowed) -> Self {
                Self {
                    init: $statename::Static(pool, value),
                    cell: OnceLock::new(),
                }
            }

            const fn new_fn(
                pool: &'static GlobalPool<$owned, S>,
                init: fn() -> Cow<'static, $borrowed>,
            ) -> Self {
                Self {
                    init: $statename::Fn(pool, init),
                    cell: OnceLock::new(),
                }
            }

            /// Returns a reference-counted clone of the contained resource.
            ///
            /// If this is the first time the contents are accessed, the value
            /// will be initialized from the pool on the first access. This
            /// requires synchronization and can block the current thraed very
            /// briefly.
            ///
            /// All subsequent accesses will be non-blocking.
            pub fn get(&self) -> &$pooled<S> {
                self.cell.get_or_init(|| match self.init {
                    $statename::Static(pool, value) => pool.get(value).clone(),
                    $statename::Fn(pool, init) => pool.get(init()).clone(),
                })
            }
        }

        #[derive(Debug, Clone, Copy)]
        #[allow(dead_code)] // Path can't use the Static variant.
        enum $statename<S>
        where
            S: BuildHasher + 'static,
        {
            Static(&'static GlobalPool<$owned, S>, &'static $borrowed),
            Fn(
                &'static GlobalPool<$owned, S>,
                fn() -> Cow<'static, $borrowed>,
            ),
        }

        impl<S> std::ops::Deref for $name<S>
        where
            S: BuildHasher,
        {
            type Target = $pooled<S>;

            fn deref(&self) -> &Self::Target {
                self.get()
            }
        }

        impl<S, S2> PartialEq<Pooled<&'static GlobalPool<$owned, S2>, S2>> for $name<S>
        where
            S: BuildHasher,
            S2: BuildHasher,
        {
            fn eq(&self, other: &Pooled<&'static GlobalPool<$owned, S2>, S2>) -> bool {
                self.get() == other
            }
        }

        impl<S, S2> PartialEq<$name<S>> for Pooled<&'static GlobalPool<$owned, S2>, S2>
        where
            S: BuildHasher,
            S2: BuildHasher,
        {
            fn eq(&self, other: &$name<S>) -> bool {
                self == other.get()
            }
        }
    };
}

impl_static_pooled!(
    StaticPooledString,
    GlobalString,
    StaticStringInit,
    String,
    str
);
impl_static_pooled!(
    StaticPooledBuffer,
    GlobalBuffer,
    StaticBufferInit,
    Vec<u8>,
    [u8]
);
impl_static_pooled!(StaticPooledPath, GlobalPath, StaticPathInit, PathBuf, Path);