IdHashMap

Struct IdHashMap 

Source
pub struct IdHashMap<T, S = DefaultHashBuilder, A: Allocator = Global> { /* private fields */ }
Expand description

A hash map where the key is part of the value.

The storage mechanism is a fast hash table of integer indexes to items, with these indexes stored in a hash table. This allows for efficient lookups by the key and prevents duplicates.

§Examples

use iddqd::{IdHashItem, IdHashMap, id_upcast};

// Define a struct with a key.
#[derive(Debug, PartialEq, Eq, Hash)]
struct MyItem {
    id: String,
    value: u32,
}

// Implement IdHashItem for the struct.
impl IdHashItem for MyItem {
    // Keys can borrow from the item.
    type Key<'a> = &'a str;

    fn key(&self) -> Self::Key<'_> {
        &self.id
    }

    id_upcast!();
}

// Create an IdHashMap and insert items.
let mut map = IdHashMap::new();
map.insert_unique(MyItem { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(MyItem { id: "bar".to_string(), value: 20 }).unwrap();

// Look up items by their keys.
assert_eq!(map.get("foo").unwrap().value, 42);
assert_eq!(map.get("bar").unwrap().value, 20);
assert!(map.get("baz").is_none());

Implementations§

Source§

impl<T: IdHashItem> IdHashMap<T>

Source

pub fn new() -> Self

Creates a new, empty IdHashMap.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let map: IdHashMap<Item> = IdHashMap::new();
assert!(map.is_empty());
assert_eq!(map.len(), 0);
Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new IdHashMap with the given capacity.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let map: IdHashMap<Item> = IdHashMap::with_capacity(10);
assert!(map.capacity() >= 10);
assert!(map.is_empty());
Source§

impl<T: IdHashItem, S: BuildHasher> IdHashMap<T, S>

Source

pub const fn with_hasher(hasher: S) -> Self

Creates a new, empty IdHashMap with the given hasher.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
use std::collections::hash_map::RandomState;

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let hasher = RandomState::new();
let map: IdHashMap<Item, _> = IdHashMap::with_hasher(hasher);
assert!(map.is_empty());
Source

pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self

Creates a new IdHashMap with the given capacity and hasher.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
use std::collections::hash_map::RandomState;

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let hasher = RandomState::new();
let map: IdHashMap<Item, _> =
    IdHashMap::with_capacity_and_hasher(10, hasher);
assert!(map.capacity() >= 10);
assert!(map.is_empty());
Source§

impl<T: IdHashItem, A: Clone + Allocator> IdHashMap<T, DefaultHashBuilder, A>

Source

pub fn new_in(alloc: A) -> Self

Creates a new empty IdHashMap using the given allocator.

Requires the allocator-api2 feature to be enabled.

§Examples

Using the bumpalo allocator:

use iddqd::{IdHashMap, IdHashItem, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> { &self.id }
    id_upcast!();
}

// Define a new allocator.
let bump = bumpalo::Bump::new();
// Create a new IdHashMap using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> = IdHashMap::new_in(&bump);
assert!(map.is_empty());
Source

pub fn with_capacity_in(capacity: usize, alloc: A) -> Self

Creates an empty IdHashMap with the specified capacity using the given allocator.

Requires the allocator-api2 feature to be enabled.

§Examples

Using the bumpalo allocator:

use iddqd::{IdHashMap, IdHashItem, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> { &self.id }
    id_upcast!();
}

// Define a new allocator.
let bump = bumpalo::Bump::new();
// Create a new IdHashMap with capacity using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> = IdHashMap::with_capacity_in(10, &bump);
assert!(map.capacity() >= 10);
assert!(map.is_empty());
Source§

impl<T: IdHashItem, S: BuildHasher, A: Clone + Allocator> IdHashMap<T, S, A>

Source

pub fn with_hasher_in(hasher: S, alloc: A) -> Self

Creates a new, empty IdHashMap with the given hasher and allocator.

Requires the allocator-api2 feature to be enabled.

§Examples

Using the bumpalo allocator:

use iddqd::{IdHashItem, IdHashMap, id_upcast};
use std::collections::hash_map::RandomState;

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

// Define a new allocator.
let bump = bumpalo::Bump::new();
let hasher = RandomState::new();
// Create a new IdHashMap with hasher using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> =
    IdHashMap::with_hasher_in(hasher, &bump);
assert!(map.is_empty());
Source

pub fn with_capacity_and_hasher_in(capacity: usize, hasher: S, alloc: A) -> Self

Creates a new, empty IdHashMap with the given capacity, hasher, and allocator.

Requires the allocator-api2 feature to be enabled.

§Examples

Using the bumpalo allocator:

use iddqd::{IdHashItem, IdHashMap, id_upcast};
use std::collections::hash_map::RandomState;

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

// Define a new allocator.
let bump = bumpalo::Bump::new();
let hasher = RandomState::new();
// Create a new IdHashMap with capacity and hasher using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> =
    IdHashMap::with_capacity_and_hasher_in(10, hasher, &bump);
assert!(map.capacity() >= 10);
assert!(map.is_empty());
Source§

impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IdHashMap<T, S, A>

Source

pub fn allocator(&self) -> &A

Returns the allocator.

Requires the allocator-api2 feature to be enabled.

§Examples

Using the bumpalo allocator:

use iddqd::{IdHashMap, IdHashItem, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> { &self.id }
    id_upcast!();
}

// Define a new allocator.
let bump = bumpalo::Bump::new();
// Create a new IdHashMap using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> = IdHashMap::new_in(&bump);
let _allocator = map.allocator();
Source

pub fn capacity(&self) -> usize

Returns the currently allocated capacity of the map.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let map: IdHashMap<Item> = IdHashMap::with_capacity(10);
assert!(map.capacity() >= 10);
Source

pub fn is_empty(&self) -> bool

Returns true if the map is empty.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
assert!(map.is_empty());

map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
assert!(!map.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of items in the map.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
assert_eq!(map.len(), 0);

map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
assert_eq!(map.len(), 1);

map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
assert_eq!(map.len(), 2);
Source

pub fn clear(&mut self)

Clears the map, removing all items.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
assert_eq!(map.len(), 2);

map.clear();
assert!(map.is_empty());
assert_eq!(map.len(), 0);
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the IdHashMap. The collection may reserve more space to speculatively avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

§Panics

Panics if the new capacity overflows isize::MAX bytes, and aborts the program in case of an allocation error. Use try_reserve instead if you want to handle memory allocation failure.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map: IdHashMap<Item> = IdHashMap::new();
map.reserve(100);
assert!(map.capacity() >= 100);
Source

pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>

Tries to reserve capacity for at least additional more elements to be inserted in the IdHashMap. The collection may reserve more space to speculatively avoid frequent reallocations. After calling try_reserve, capacity will be greater than or equal to self.len() + additional if it returns Ok(()). Does nothing if capacity is already sufficient.

§Errors

If the capacity overflows, or the allocator reports a failure, then an error is returned.

§Notes

If reservation fails partway through, some internal structures may have already increased their capacity. The map remains in a valid state but may have uneven capacities across its internal structures.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map: IdHashMap<Item> = IdHashMap::new();
map.try_reserve(100).expect("allocation should succeed");
assert!(map.capacity() >= 100);
Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map: IdHashMap<Item> = IdHashMap::with_capacity(100);
map.insert_unique(Item { id: "foo".to_string(), value: 1 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 2 }).unwrap();
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() >= 2);
Source

pub fn shrink_to(&mut self, min_capacity: usize)

Shrinks the capacity of the map with a lower limit. It will drop down no lower than the supplied limit while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

If the current capacity is less than the lower limit, this is a no-op.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map: IdHashMap<Item> = IdHashMap::with_capacity(100);
map.insert_unique(Item { id: "foo".to_string(), value: 1 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 2 }).unwrap();
assert!(map.capacity() >= 100);
map.shrink_to(10);
assert!(map.capacity() >= 10);
map.shrink_to(0);
assert!(map.capacity() >= 2);
Source

pub fn iter(&self) -> Iter<'_, T>

Iterates over the items in the map.

Similar to HashMap, the iteration order is arbitrary and not guaranteed to be stable.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();

let mut values: Vec<u32> = map.iter().map(|item| item.value).collect();
values.sort();
assert_eq!(values, vec![20, 42]);
Source

pub fn iter_mut(&mut self) -> IterMut<'_, T, S, A>

Iterates over the items in the map, allowing for mutation.

Similar to HashMap, the iteration order is arbitrary and not guaranteed to be stable.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();

for mut item in map.iter_mut() {
    item.value *= 2;
}

assert_eq!(map.get("foo").unwrap().value, 84);
assert_eq!(map.get("bar").unwrap().value, 40);
Source

pub fn insert_overwrite(&mut self, value: T) -> Option<T>

Inserts a value into the map, removing and returning the conflicting item, if any.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();

// First insertion returns None
let old = map.insert_overwrite(Item { id: "foo".to_string(), value: 42 });
assert!(old.is_none());

// Second insertion with same key returns the old value
let old = map.insert_overwrite(Item { id: "foo".to_string(), value: 100 });
assert_eq!(old.unwrap().value, 42);
assert_eq!(map.get("foo").unwrap().value, 100);
Source

pub fn insert_unique(&mut self, value: T) -> Result<(), DuplicateItem<T, &T>>

Inserts a value into the set, returning an error if any duplicates were added.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();

// First insertion succeeds
assert!(
    map.insert_unique(Item { id: "foo".to_string(), value: 42 }).is_ok()
);

// Second insertion with different key succeeds
assert!(
    map.insert_unique(Item { id: "bar".to_string(), value: 20 }).is_ok()
);

// Third insertion with duplicate key fails
assert!(
    map.insert_unique(Item { id: "foo".to_string(), value: 100 }).is_err()
);
Source

pub fn contains_key<'a, Q>(&'a self, key1: &Q) -> bool
where Q: ?Sized + Hash + Equivalent<T::Key<'a>>,

Returns true if the map contains the given key.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();

assert!(map.contains_key("foo"));
assert!(!map.contains_key("bar"));
Source

pub fn get<'a, Q>(&'a self, key: &Q) -> Option<&'a T>
where Q: ?Sized + Hash + Equivalent<T::Key<'a>>,

Gets a reference to the value associated with the given key.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();

assert_eq!(map.get("foo").unwrap().value, 42);
assert!(map.get("bar").is_none());
Source

pub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<RefMut<'a, T, S>>
where Q: ?Sized + Hash + Equivalent<T::Key<'a>>,

Gets a mutable reference to the value associated with the given key.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();

if let Some(mut item) = map.get_mut("foo") {
    item.value = 100;
}

assert_eq!(map.get("foo").unwrap().value, 100);
assert!(map.get_mut("bar").is_none());
Source

pub fn remove<'a, Q>(&'a mut self, key: &Q) -> Option<T>
where Q: ?Sized + Hash + Equivalent<T::Key<'a>>,

Removes an item from the map by its key.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();

let removed = map.remove("foo");
assert_eq!(removed.unwrap().value, 42);
assert!(map.is_empty());

// Removing non-existent key returns None
assert!(map.remove("bar").is_none());
Source

pub fn entry<'a>(&'a mut self, key: T::Key<'_>) -> Entry<'a, T, S, A>

Retrieves an entry by its key.

Due to borrow checker limitations, this always accepts an owned key rather than a borrowed form of it.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();

// Use entry API for conditional insertion
map.entry("foo").or_insert(Item { id: "foo".to_string(), value: 42 });
map.entry("bar").or_insert(Item { id: "bar".to_string(), value: 20 });

assert_eq!(map.len(), 2);
Source

pub fn retain<'a, F>(&'a mut self, f: F)
where F: FnMut(RefMut<'a, T, S>) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all items T for which f(RefMut<T>) returns false. The elements are visited in an arbitrary order.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;

    fn key(&self) -> Self::Key<'_> {
        &self.id
    }

    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
map.insert_unique(Item { id: "baz".to_string(), value: 99 }).unwrap();

// Retain only items where value is greater than 30
map.retain(|item| item.value > 30);

assert_eq!(map.len(), 2);
assert_eq!(map.get("foo").unwrap().value, 42);
assert_eq!(map.get("baz").unwrap().value, 99);
assert!(map.get("bar").is_none());

Trait Implementations§

Source§

impl<T: Clone, S: Clone, A: Clone + Allocator> Clone for IdHashMap<T, S, A>

Source§

fn clone(&self) -> IdHashMap<T, S, A>

Returns a duplicate 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<'a, T, S: Clone + BuildHasher, A: Allocator> Debug for IdHashMap<T, S, A>
where T: IdHashItem + Debug + 'a, T::Key<'a>: Debug,

Source§

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

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

impl<T: IdHashItem, S: Default, A: Allocator + Default> Default for IdHashMap<T, S, A>

Source§

fn default() -> Self

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

impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> Extend<T> for IdHashMap<T, S, A>

The Extend implementation overwrites duplicates. In the future, there will also be an extend_unique method that will return an error.

§Examples

use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();

let new_items = vec![
    Item { id: "foo".to_string(), value: 100 }, // overwrites existing
    Item { id: "bar".to_string(), value: 20 },  // new item
];

map.extend(new_items);
assert_eq!(map.len(), 2);
assert_eq!(map.get("foo").unwrap().value, 100); // overwritten
assert_eq!(map.get("bar").unwrap().value, 20); // new
Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: IdHashItem, S: Default + Clone + BuildHasher, A: Allocator + Default> FromIterator<T> for IdHashMap<T, S, A>

The FromIterator implementation for IdHashMap overwrites duplicate items.

§Examples

use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let items = vec![
    Item { id: "foo".to_string(), value: 42 },
    Item { id: "bar".to_string(), value: 20 },
    Item { id: "foo".to_string(), value: 100 }, // duplicate key, overwrites
];

let map: IdHashMap<Item> = items.into_iter().collect();
assert_eq!(map.len(), 2);
assert_eq!(map.get("foo").unwrap().value, 100); // last value wins
assert_eq!(map.get("bar").unwrap().value, 20);
Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for &'a IdHashMap<T, S, A>

Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator over references to the items in the map.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();

let mut values: Vec<u32> =
    (&map).into_iter().map(|item| item.value).collect();
values.sort();
assert_eq!(values, vec![20, 42]);
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for &'a mut IdHashMap<T, S, A>

Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator over mutable references to the items in the map.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();

for mut item in &mut map {
    item.value *= 2;
}

assert_eq!(map.get("foo").unwrap().value, 84);
assert_eq!(map.get("bar").unwrap().value, 40);
Source§

type Item = RefMut<'a, T, S>

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T, S, A>

Which kind of iterator are we turning this into?
Source§

impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for IdHashMap<T, S, A>

Source§

fn into_iter(self) -> Self::IntoIter

Consumes the map and creates an iterator over the owned items.

§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};

#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
    id: String,
    value: u32,
}

impl IdHashItem for Item {
    type Key<'a> = &'a str;
    fn key(&self) -> Self::Key<'_> {
        &self.id
    }
    id_upcast!();
}

let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();

let mut values: Vec<u32> = map.into_iter().map(|item| item.value).collect();
values.sort();
assert_eq!(values, vec![20, 42]);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, A>

Which kind of iterator are we turning this into?
Source§

impl<T: IdHashItem + PartialEq, S: Clone + BuildHasher, A: Allocator> PartialEq for IdHashMap<T, S, A>

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<T: IdHashItem + Eq, S: Clone + BuildHasher, A: Allocator> Eq for IdHashMap<T, S, A>

Auto Trait Implementations§

§

impl<T, S, A> Freeze for IdHashMap<T, S, A>
where S: Freeze, A: Freeze,

§

impl<T, S, A> RefUnwindSafe for IdHashMap<T, S, A>

§

impl<T, S, A> Send for IdHashMap<T, S, A>
where S: Send, T: Send, A: Send,

§

impl<T, S, A> Sync for IdHashMap<T, S, A>
where S: Sync, T: Sync, A: Sync,

§

impl<T, S, A> Unpin for IdHashMap<T, S, A>
where S: Unpin, A: Unpin, T: Unpin,

§

impl<T, S, A> UnwindSafe for IdHashMap<T, S, A>
where S: UnwindSafe, A: 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> CloneToUninit for T
where T: Clone,

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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.