[go: up one dir, main page]

Struct ExtractMap

Source
pub struct ExtractMap<K, V, S = RandomState> { /* private fields */ }
Expand description

A hash map for memory efficent storage of value types which contain their own keys.

This is achieved by the V type deriving the ExtractKey trait for their K type, and is backed by a HashSet<Wrap<K>, V, S>, meaning this library only uses unsafe code for performance reasons.

The default hashing algorithm is the same as the standard library’s HashSet, RandomState, although your own hasher can be provided via ExtractMap::with_hasher and it’s similar methods.

Implementations§

Source§

impl<K, V> ExtractMap<K, V, RandomState>

Source

pub fn new() -> Self

Creates a new, empty ExtractMap with the RandomState hasher.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new ExtractMap with the RandomState hasher and preallocated capacity.

§Examples
use extract_map::{ExtractMap, ExtractKey};

struct User {
    id: u64,
    name: &'static str,
}

impl ExtractKey<u64> for User {
    fn extract_key(&self) -> &u64 {
        &self.id
    }
}

let map = ExtractMap::<u64, User>::with_capacity(5);

assert_eq!(map.len(), 0);
assert!(map.capacity() >= 5);
Source§

impl<K, V, S> ExtractMap<K, V, S>

Source

pub fn with_hasher(hasher: S) -> Self

Creates a new, empty ExtractMap with the provided hasher.

Source

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

Creates a new ExtractMap with the provided hasher and preallocated capacity.

§Examples
use std::collections::hash_map::RandomState;

use extract_map::{ExtractMap, ExtractKey};

struct User {
    id: u64,
    name: &'static str,
}

impl ExtractKey<u64> for User {
    fn extract_key(&self) -> &u64 {
        &self.id
    }
}

let map = ExtractMap::<u64, User>::with_capacity_and_hasher(5, RandomState::new());

assert!(map.is_empty());
assert!(map.capacity() >= 5);
Source§

impl<K, V, S> ExtractMap<K, V, S>
where K: Hash + Eq, V: ExtractKey<K>, S: BuildHasher,

Source

pub fn insert(&mut self, value: V) -> Option<V>

Inserts a value into the ExtractMap.

This extracts the key from the value using the ExtractKey trait, and therefore does not need a key to be provided.

§Examples
use extract_map::{ExtractMap, ExtractKey};

struct User {
    id: u64,
    name: &'static str,
}

impl ExtractKey<u64> for User {
    fn extract_key(&self) -> &u64 {
        &self.id
    }
}

let mut map = ExtractMap::new();
map.insert(User { id: 1, name: "Daisy" });
map.insert(User { id: 2, name: "Elliott" });

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

pub fn remove(&mut self, key: &K) -> Option<V>

Removes a value from the ExtractMap.

§Examples
use extract_map::{ExtractMap, ExtractKey};

#[derive(Debug, Clone, PartialEq)]
struct User {
    id: u64,
    name: &'static str,
}

impl ExtractKey<u64> for User {
    fn extract_key(&self) -> &u64 {
        &self.id
    }
}

let user = User { id: 1, name: "Daisy" };
let mut map = ExtractMap::new();
map.insert(user.clone());

assert_eq!(map.remove(&1), Some(user));
assert!(map.is_empty())
Source

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

Checks if a value is in the ExtractMap.

Source

pub fn get(&self, key: &K) -> Option<&V>

Retrieves a value from the ExtractMap.

Source

pub fn get_mut<'a>(&'a mut self, key: &K) -> Option<MutGuard<'a, K, V, S>>

Retrieves a mutable guard to a value in the ExtractMap.

This guard is required as the current implementation takes the value out of the map and reinserts on Drop to allow mutation of the key field.

Source§

impl<K, V, S> ExtractMap<K, V, S>

Source

pub fn capacity(&self) -> usize

Retrieves the number of remaining values that can be inserted before a reallocation.

Source

pub fn len(&self) -> usize

Retrieves the number of values currently in the ExtractMap.

Source

pub fn is_empty(&self) -> bool

Retrieves if the ExtractMap contains no values.

Source

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

Retrieves an iterator over the borrowed values.

If you need an iterator over the keys and values, simply use ExtractKey.

Use IntoIterator::into_iter for an iterator over owned values.

Trait Implementations§

Source§

impl<K, V: Clone, S: Clone> Clone for ExtractMap<K, V, S>

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<K, V, S> Debug for ExtractMap<K, V, S>
where K: Debug + Hash + Eq, V: Debug + ExtractKey<K>,

Source§

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

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

impl<K, V> Default for ExtractMap<K, V, RandomState>

Source§

fn default() -> Self

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

impl<K, V, S> Extend<V> for ExtractMap<K, V, S>
where K: Hash + Eq, V: ExtractKey<K>, S: BuildHasher,

Source§

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

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<K, V, S> FromIterator<V> for ExtractMap<K, V, S>
where K: Hash + Eq, V: ExtractKey<K>, S: BuildHasher + Default,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a ExtractMap<K, V, S>

Source§

type Item = &'a V

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, S> IntoIterator for ExtractMap<K, V, S>

Source§

type Item = V

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, S> PartialEq for ExtractMap<K, V, S>
where K: Hash + Eq, V: ExtractKey<K> + PartialEq, S: BuildHasher,

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.

Auto Trait Implementations§

§

impl<K, V, S> Freeze for ExtractMap<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for ExtractMap<K, V, S>

§

impl<K, V, S> Send for ExtractMap<K, V, S>
where S: Send, V: Send, K: Send,

§

impl<K, V, S> Sync for ExtractMap<K, V, S>
where S: Sync, V: Sync, K: Sync,

§

impl<K, V, S> Unpin for ExtractMap<K, V, S>
where S: Unpin, V: Unpin, K: Unpin,

§

impl<K, V, S> UnwindSafe for ExtractMap<K, V, S>
where S: UnwindSafe, V: UnwindSafe, K: 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.