Trait spinoso_symbol::InternerAllSymbols

source ·
pub trait InternerAllSymbols: Intern {
    // Required method
    fn all_symbols(&self) -> AllSymbols ;
}
Expand description

Extension trait to return an iterator that returns all symbol identifiers stored in an interner as Symbols.

The returned iterator yields Symbol as its item type.

Implementors of this trait must issue symbol identifiers as an arithmetic progression with a common difference of 1. The sequence of symbol identifiers must be representable by a Range<u32>.

This trait is automatically implemented for all types that implement Intern from artichoke_core.

§Examples

use alloc::borrow::Cow;
use alloc::boxed::Box;

use artichoke_core::intern::Intern;
use spinoso_symbol::{InternerAllSymbols, Symbol};

#[derive(Default)]
struct Interner(u32);

impl Intern for Interner {
    type Symbol = u32;
    type Error = &'static str;
    const SYMBOL_RANGE_START: u32 = 1;

    fn intern_bytes<T>(&mut self, symbol: T) -> Result<Self::Symbol, Self::Error>
    where
        T: Into<Cow<'static, [u8]>>,
    {
        let boxed = Box::<[u8]>::from(symbol.into());
        Box::leak(boxed);
        self.0 += 1;
        let sym = self.0;
        Ok(sym)
    }

    fn check_interned_bytes(&self, symbol: &[u8]) -> Result<Option<Self::Symbol>, Self::Error> {
        Err("not implemented")
    }

    fn lookup_symbol(&self, symbol: Self::Symbol) -> Result<Option<&[u8]>, Self::Error> {
        Err("not implemented")
    }

    fn symbol_count(&self) -> usize {
        self.0 as usize
    }
}

let mut interner = Interner::default();
let mut all_symbols = interner.all_symbols();
assert_eq!(all_symbols.count(), 0);

interner.intern_bytes(&b"Spinoso"[..]);
interner.intern_bytes(&b"Artichoke"[..]);

let mut all_symbols = interner.all_symbols();
assert_eq!(all_symbols.next(), Some(Symbol::new(1)));
assert_eq!(all_symbols.next(), Some(Symbol::new(2)));
assert_eq!(all_symbols.next(), None);

Required Methods§

source

fn all_symbols(&self) -> AllSymbols

Returns an iterator that returns all symbol identifiers stored in an interner as Symbols.

The returned iterator yields Symbol as its item type.

This function requires that the interner issues symbol identifiers as an arithmetic progression with a common difference of 1. The sequence of symbol identifiers must be representable by a Range<u32>.

AllSymbols supports yielding up to u32::MAX - 1 Symbols.

§Examples

See trait-level documentation for examples.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T, U> InternerAllSymbols for T
where T: Intern<Symbol = U>, U: Copy + Into<u32>,