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
//! Backends for the [`StringInterner`](`crate::StringInterner`).
//!
//! The backend is the method or strategy that handles the actual interning.
//! There are trade-offs for the different kinds of backends. A user should
//! find the backend that suits their use case best.

mod bucket;
mod interned_str;
mod simple;

pub use self::{
    bucket::BucketBackend,
    interned_str::InternedStr,
    simple::SimpleBackend,
};
use crate::{
    DefaultSymbol,
    Symbol,
};

/// The default backend recommended for general use.
pub type DefaultBackend = BucketBackend<DefaultSymbol>;

/// Types implementing this trait may act as backends for the string interner.
///
/// The job of a backend is to actually store, manage and organize the interned
/// strings. Different backends have different trade-offs. Users should pick
/// their backend with hinsight of their personal use-case.
pub trait Backend<S>: Default
where
    S: Symbol,
{
    /// Creates a new backend for the given capacity.
    ///
    /// The capacity denotes how many strings are expected to be interned.
    fn with_capacity(cap: usize) -> Self;

    /// Interns the given string returns its interned view and its symbol.
    ///
    /// # Safety
    ///
    /// The returned `InternedStr` points to an actually interned string. The
    /// backend must make sure that it never moves its interned string arounds.
    /// This is why this method is `unsafe`.
    unsafe fn intern(&mut self, string: &str) -> (InternedStr, S);

    /// Resolves the given symbol to its original string contents.
    fn resolve(&self, symbol: S) -> Option<&str>;
}