string_interner/backend/mod.rs
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
//! 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 buffer;
mod string;
#[cfg(feature = "backends")]
pub use self::{bucket::BucketBackend, buffer::BufferBackend, string::StringBackend};
use crate::Symbol;
/// The default backend recommended for general use.
#[cfg(feature = "backends")]
pub type DefaultBackend = StringBackend<crate::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: Default {
/// The symbol used by the string interner backend.
type Symbol: Symbol;
/// The iterator over the symbols and their strings.
type Iter<'a>: Iterator<Item = (Self::Symbol, &'a str)>
where
Self: 'a;
/// 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 and returns its interned ref and symbol.
///
/// # Note
///
/// The backend must make sure that the returned symbol maps back to the
/// original string in its [`resolve`](`Backend::resolve`) method.
fn intern(&mut self, string: &str) -> Self::Symbol;
/// Interns the given static string and returns its interned ref and symbol.
///
/// # Note
///
/// The backend must make sure that the returned symbol maps back to the
/// original string in its [`resolve`](`Backend::resolve`) method.
#[inline]
fn intern_static(&mut self, string: &'static str) -> Self::Symbol {
// The default implementation simply forwards to the normal [`intern`]
// implementation. Backends that can optimize for this use case should
// implement this method.
self.intern(string)
}
/// Shrink backend capacity to fit interned symbols exactly.
fn shrink_to_fit(&mut self);
/// Resolves the given symbol to its original string contents.
fn resolve(&self, symbol: Self::Symbol) -> Option<&str>;
/// Resolves the given symbol to its original string contents.
///
/// # Safety
///
/// Does not perform validity checks on the given symbol and relies
/// on the caller to be provided with a symbol that has been generated
/// by the [`intern`](`Backend::intern`) or
/// [`intern_static`](`Backend::intern_static`) methods of the same
/// interner backend.
unsafe fn resolve_unchecked(&self, symbol: Self::Symbol) -> &str;
/// Creates an iterator that yields all interned strings and their symbols.
fn iter(&self) -> Self::Iter<'_>;
}