pub struct FrozenIndexSet<T, S = RandomState> { /* private fields */ }
Expand description

Append-only version of indexmap::IndexSet where insertion does not require mutable access

Implementations§

source§

impl<T: Eq + Hash> FrozenIndexSet<T>

source

pub fn new() -> Self

Examples found in repository?
examples/string_interner.rs (line 13)
11
12
13
14
15
    fn new() -> Self {
        StringInterner {
            set: FrozenIndexSet::new(),
        }
    }
source§

impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S>

source

pub fn insert(&self, value: T) -> &T::Target

If the value exists in the set, returns a reference to the corresponding value, otherwise inserts a new entry in the set for that value and returns a reference to it.

Existing values are never overwritten.

Example
use elsa::index_set::FrozenIndexSet;
let set = FrozenIndexSet::new();
let a_ref = set.insert(Box::new("a"));
let aa = "a";
let other_a_ref = unsafe { aa.as_ptr() as *const &str};
let other_a = Box::new(aa);
assert!(!std::ptr::eq(a_ref, other_a_ref));
assert!(std::ptr::eq(a_ref, set.insert(other_a)));
source

pub fn insert_full(&self, value: T) -> (usize, &T::Target)

If the key exists in the set, returns a reference to the corresponding value and its index, otherwise inserts a new entry in the set for that value and returns a reference to it and its index.

Existing values are never overwritten.

Example
use elsa::index_set::FrozenIndexSet;
let map = FrozenIndexSet::new();
assert_eq!(map.insert_full(Box::new("a")), (0, &"a"));
assert_eq!(map.insert_full(Box::new("b")), (1, &"b"));
Examples found in repository?
examples/string_interner.rs (line 23)
17
18
19
20
21
22
23
24
    fn get_or_intern<T>(&self, value: T) -> usize
    where
        T: AsRef<str>,
    {
        // TODO use Entry in case the standard Entry API gets improved
        // (here to avoid premature allocation or double lookup)
        self.set.insert_full(value.as_ref().to_string()).0
    }
source

pub fn get<Q>(&self, k: &Q) -> Option<&T::Target>
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value passed as argument if present in the set.

Examples
use elsa::index_set::FrozenIndexSet;

let set = FrozenIndexSet::new();
set.insert(Box::new("a"));
assert_eq!(set.get(&Box::new("a")), Some(&"a"));
assert_eq!(set.get(&Box::new("b")), None);
source

pub fn get_full<Q>(&self, k: &Q) -> Option<(usize, &T::Target)>
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value passed as argument if present in the set, along with its index

Examples
use elsa::index_set::FrozenIndexSet;

let set = FrozenIndexSet::new();
set.insert(Box::new("a"));
assert_eq!(set.get_full(&Box::new("a")), Some((0, &"a")));
assert_eq!(set.get_full(&Box::new("b")), None);
Examples found in repository?
examples/string_interner.rs (line 30)
26
27
28
29
30
31
    fn get<T>(&self, value: T) -> Option<usize>
    where
        T: AsRef<str>,
    {
        self.set.get_full(value.as_ref()).map(|(i, _r)| i)
    }
source

pub fn get_index(&self, index: usize) -> Option<&T::Target>

Returns a reference to value at the index passed as argument, if present in the set.

Examples
use elsa::index_set::FrozenIndexSet;

let set = FrozenIndexSet::new();
set.insert(Box::new("a"));
assert_eq!(set.get_index(0), Some(&"a"));
assert_eq!(set.get_index(1), None);
Examples found in repository?
examples/string_interner.rs (line 34)
33
34
35
    fn resolve(&self, index: usize) -> Option<&str> {
        self.set.get_index(index)
    }
source§

impl<T, S> FrozenIndexSet<T, S>

source

pub fn into_set(self) -> IndexSet<T, S>

source

pub fn as_mut(&mut self) -> &mut IndexSet<T, S>

Get mutable access to the underlying IndexSet.

This is safe, as it requires a &mut self, ensuring nothing is using the ‘frozen’ contents.

Trait Implementations§

source§

impl<K: Clone, V: Clone> Clone for FrozenIndexSet<K, V>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Eq + Hash, S: Default> Default for FrozenIndexSet<T, S>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T, S> From<IndexSet<T, S>> for FrozenIndexSet<T, S>

source§

fn from(set: IndexSet<T, S>) -> Self

Converts to this type from the input type.
source§

impl<T: Eq + Hash, S: Default + BuildHasher> FromIterator<T> for FrozenIndexSet<T, S>

source§

fn from_iter<U>(iter: U) -> Self
where U: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
source§

impl<T: Eq + Hash + StableDeref, S> Index<usize> for FrozenIndexSet<T, S>

§

type Output = <T as Deref>::Target

The returned type after indexing.
source§

fn index(&self, idx: usize) -> &T::Target

Performs the indexing (container[index]) operation. Read more
source§

impl<T: Hash + Eq, S: BuildHasher> PartialEq for FrozenIndexSet<T, S>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<T, S = RandomState> !RefUnwindSafe for FrozenIndexSet<T, S>

§

impl<T, S> Send for FrozenIndexSet<T, S>
where S: Send, T: Send,

§

impl<T, S = RandomState> !Sync for FrozenIndexSet<T, S>

§

impl<T, S> Unpin for FrozenIndexSet<T, S>
where S: Unpin, T: Unpin,

§

impl<T, S> UnwindSafe for FrozenIndexSet<T, S>
where S: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.