id_cache

Struct IdCache

Source
pub struct IdCache<I: Id, T> { /* private fields */ }
Expand description

A cache which generates sequentially-assigned ids for unique values.

§Example

use id_collections::id_type;
use id_cache::IdCache;

#[id_type]
struct WordId(u32);

let mut word_cache: IdCache<WordId, &str> = IdCache::new();

let foo_id = word_cache.make_id("foo");
let bar_id = word_cache.make_id("bar");

assert_eq!(word_cache[foo_id], "foo");
assert_eq!(word_cache[bar_id], "bar");

// ids for repeated values are reused:
assert_eq!(word_cache.make_id("foo"), foo_id);

§Serde Support

When the serde Cargo feature is enabled, the IdCache<I, T> type can be serialized and deserialized using Serde. An IdCache<I, T> is serialized as a sequence consisting of the unique values in the cache, ordered by id:

use id_collections::id_type;
use id_cache::IdCache;

#[id_type]
struct WordId(u32);

let mut word_cache: IdCache<WordId, &str> = IdCache::new();
word_cache.make_id("foo");
word_cache.make_id("bar");

let serialized = serde_json::to_string(&word_cache).unwrap();
assert_eq!(&serialized, r#"["foo","bar"]"#);

Implementations§

Source§

impl<I: Id, T> IdCache<I, T>

Source

pub fn new() -> Self

Constructs a new, empty IdCache<I, T>.

§Examples
let cache: IdCache<u32, &str> = IdCache::new();
assert!(cache.is_empty());
Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty IdCache<I, T> with space to hold at least capacity unique values.

§Examples
let mut cache: IdCache<u32, &str> = IdCache::with_capacity(100);
assert!(cache.is_empty());
Source

pub fn count(&self) -> Count<I>

Returns the total number of ids that have been assigned to unique values in the IdCache<I, T>.

§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
assert!(cache.count().is_empty());
cache.make_id("foo");
cache.make_id("bar");
assert_eq!(cache.count().to_value(), 2);
cache.make_id("foo"); // value already present, so does not assign a new id
assert_eq!(cache.count().to_value(), 2);
Source

pub fn len(&self) -> usize

Returns the total number of unique values in the IdCache<I, T>.

§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
assert_eq!(cache.len(), 0);
cache.make_id("foo");
cache.make_id("bar");
assert_eq!(cache.len(), 2);
cache.make_id("foo"); // value already present, so does not increase the len
assert_eq!(cache.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the IdCache<I, T> contains no values.

§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
assert!(cache.is_empty());
cache.make_id("foo");
assert!(!cache.is_empty());
Source

pub fn make_id(&mut self, value: T) -> I
where T: Eq + Hash + Clone,

Ensures value has an id in the IdCache<I, T>, and returns that id.

If value is already present in the IdCache<I, T>, then make_id returns its existing id. Otherwise, make_id returns a new sequentally-assigned id.

§Panics

Panics if the number of ids in the IdCache<I, T> overflows I.

§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
assert_eq!(cache.make_id("foo"), 0);
assert_eq!(cache.make_id("bar"), 1);
assert_eq!(cache.make_id("foo"), 0);
Source

pub fn get_id<U>(&self, value: &U) -> Option<I>
where T: Borrow<U> + Eq + Hash, U: Eq + Hash,

Returns the id of a value in the IdCache<I, T>, or None if the value is not present.

§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
let foo_id = cache.make_id("foo");
assert_eq!(cache.get_id(&"foo"), Some(foo_id));
assert_eq!(cache.get_id(&"bar"), None);
Source

pub fn get_value(&self, id: I) -> Option<&T>

Returns a reference to the value in the IdCache<I, T> associated with a given id, or None if the id has not been assigned.

§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
let foo_id = cache.make_id("foo");
assert_eq!(foo_id, 0);
assert_eq!(cache.get_value(foo_id), Some(&"foo"));
assert_eq!(cache.get_value(1), None);

Trait Implementations§

Source§

impl<I: Clone + Id, T: Clone> Clone for IdCache<I, T>

Source§

fn clone(&self) -> IdCache<I, T>

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<I: Id + Debug, T: Debug> Debug for IdCache<I, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I: Id, T> Default for IdCache<I, T>

Source§

fn default() -> Self

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

impl<I: Id, T, J: Borrow<I>> Index<J> for IdCache<I, T>

Source§

fn index(&self, id: J) -> &Self::Output

Returns a reference to the value in the IdCache<I, T> associated with a given id.

§Panics

Panics if id has not been assigned.

§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
let foo_id = cache.make_id("foo");
assert_eq!(cache[foo_id], "foo");
Source§

type Output = T

The returned type after indexing.
Source§

impl<I: Id, T: PartialEq> PartialEq for IdCache<I, T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<I: Id, T: Eq> Eq for IdCache<I, T>

Auto Trait Implementations§

§

impl<I, T> Freeze for IdCache<I, T>

§

impl<I, T> RefUnwindSafe for IdCache<I, T>

§

impl<I, T> Send for IdCache<I, T>
where I: Send, T: Send,

§

impl<I, T> Sync for IdCache<I, T>
where I: Sync, T: Sync,

§

impl<I, T> Unpin for IdCache<I, T>
where I: Unpin, T: Unpin,

§

impl<I, T> UnwindSafe for IdCache<I, T>
where T: UnwindSafe, I: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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>,

Source§

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>,

Source§

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.