Struct abi_stable::std_types::RHashMap[][src]

#[repr(C)]
pub struct RHashMap<K, V, S = RandomState> { /* fields omitted */ }
Expand description

An ffi-safe hashmap, which wraps std::collections::HashMap<K, V, S>, only requiring the K: Eq + Hash bounds when constructing it.

Most of the API in HashMap is implemented here, including the Entry API.

Example

This example demonstrates how one can use the RHashMap as a dictionary.

use abi_stable::std_types::{RHashMap, RSome, RString, Tuple2};

let mut map = RHashMap::new();

map.insert(
    "dictionary",
    "A book/document containing definitions of words",
);
map.insert("bibliophile", "Someone who loves books.");
map.insert("pictograph", "A picture representating of a word.");

assert_eq!(
    map["dictionary"],
    "A book/document containing definitions of words",
);

assert_eq!(map.remove("bibliophile"), RSome("Someone who loves books."),);

assert_eq!(
    map.get("pictograph"),
    Some(&"A picture representating of a word."),
);

for Tuple2(k, v) in map {
    assert!(k == "dictionary" || k == "pictograph");

    assert!(
        v == "A book/document containing definitions of words" ||
        v == "A picture representating of a word.",
        "{} => {}",
        k,
        v,
    );
}

Implementations

Constructs an empty RHashMap.

Example
use abi_stable::std_types::{RHashMap, RString};

let mut map = RHashMap::<RString, u32>::new();
assert!(map.is_empty());
map.insert("Hello".into(), 10);
assert_eq!(map.is_empty(), false);

Constructs an empty RHashMap with at least the passed capacity.

Example
use abi_stable::std_types::{RHashMap, RString};

let mut map = RHashMap::<RString, u32>::with_capacity(10);
assert!(map.capacity() >= 10);

Constructs an empty RHashMap with the passed hash_builder to hash the keys.

Example
use abi_stable::std_types::{RHashMap, RString};
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mut map = RHashMap::<RString, u32, _>::with_hasher(s);
assert!(map.is_empty());
map.insert("Hello".into(), 10);
assert_eq!(map.is_empty(), false);

Constructs an empty RHashMap with at least the passed capacity, and the passed hash_builder to hash the keys.

Example
use abi_stable::std_types::{RHashMap, RString};
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mut map = RHashMap::<RString, u32, _>::with_capacity_and_hasher(10, s);
assert!(map.capacity() >= 10);

Returns whether the map associates a value with the key.

Example
use abi_stable::std_types::{RHashMap, RString};

let mut map = RHashMap::<RString, u32>::new();
assert_eq!(map.contains_key("boo"), false);
map.insert("boo".into(), 0);
assert_eq!(map.contains_key("boo"), true);

Returns a reference to the value associated with the key.

Returns a None if there is no entry for the key.

Example
use abi_stable::std_types::{RHashMap, RString};

let mut map = RHashMap::<RString, u32>::new();
assert_eq!(map.get("boo"), None);
map.insert("boo".into(), 0);
assert_eq!(map.get("boo"), Some(&0));

Returns a mutable reference to the value associated with the key.

Returns a None if there is no entry for the key.

Example
use abi_stable::std_types::{RHashMap, RString};

let mut map = RHashMap::<RString, u32>::new();
assert_eq!(map.get_mut("boo"), None);
map.insert("boo".into(), 0);
assert_eq!(map.get_mut("boo"), Some(&mut 0));

Removes the value associated with the key.

Example
use abi_stable::std_types::{RHashMap, RSome, RNone};

let mut map = vec![(0, 1), (3, 4)].into_iter().collect::<RHashMap<u32, u32>>();

assert_eq!(map.remove(&0), RSome(1));
assert_eq!(map.remove(&0), RNone);

assert_eq!(map.remove(&3), RSome(4));
assert_eq!(map.remove(&3), RNone);

pub fn remove_entry<Q>(&mut self, query: &Q) -> ROption<Tuple2<K, V>> where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized

Removes the entry for the key.

Example
use abi_stable::std_types::{RHashMap, RSome, RNone, Tuple2};

let mut map = vec![(0, 1), (3, 4)].into_iter().collect::<RHashMap<u32, u32>>();

assert_eq!(map.remove_entry(&0), RSome(Tuple2(0, 1)));
assert_eq!(map.remove_entry(&0), RNone);

assert_eq!(map.remove_entry(&3), RSome(Tuple2(3, 4)));
assert_eq!(map.remove_entry(&3), RNone);

Returns whether the map associates a value with the key.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();
assert_eq!(map.contains_key(&11), false);
map.insert(11, 0);
assert_eq!(map.contains_key(&11), true);

Returns a reference to the value associated with the key.

Returns a None if there is no entry for the key.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();
assert_eq!(map.get(&12), None);
map.insert(12, 0);
assert_eq!(map.get(&12), Some(&0));

Returns a mutable reference to the value associated with the key.

Returns a None if there is no entry for the key.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();
assert_eq!(map.get_mut(&12), None);
map.insert(12, 0);
assert_eq!(map.get_mut(&12), Some(&mut 0));

Removes the value associated with the key.

Example
use abi_stable::std_types::{RHashMap, RSome, RNone};

let mut map = vec![(0, 1), (3, 4)].into_iter().collect::<RHashMap<u32, u32>>();

assert_eq!(map.remove_p(&0), RSome(1));
assert_eq!(map.remove_p(&0), RNone);

assert_eq!(map.remove_p(&3), RSome(4));
assert_eq!(map.remove_p(&3), RNone);

pub fn remove_entry_p(&mut self, key: &K) -> ROption<Tuple2<K, V>>

Removes the entry for the key.

Example
use abi_stable::std_types::{RHashMap, RSome, RNone, Tuple2};

let mut map = vec![(0, 1), (3, 4)].into_iter().collect::<RHashMap<u32, u32>>();

assert_eq!(map.remove_entry_p(&0), RSome(Tuple2(0, 1)));
assert_eq!(map.remove_entry_p(&0), RNone);

assert_eq!(map.remove_entry_p(&3), RSome(Tuple2(3, 4)));
assert_eq!(map.remove_entry_p(&3), RNone);

Returns a reference to the value associated with the key.

Panics

Panics if the key is not associated with a value.

Example
use abi_stable::std_types::RHashMap;

let mut map = vec![(0, 1), (3, 4)].into_iter().collect::<RHashMap<u32, u32>>();

assert_eq!(map.index_p(&0), &1);
assert_eq!(map.index_p(&3), &4);
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();

assert_eq!(map.index_p(&0), &1);

Returns a mutable reference to the value associated with the key.

Panics

Panics if the key is not associated with a value.

Example
use abi_stable::std_types::RHashMap;

let mut map = vec![(0, 1), (3, 4)].into_iter().collect::<RHashMap<u32, u32>>();

assert_eq!(map.index_mut_p(&0), &mut 1);
assert_eq!(map.index_mut_p(&3), &mut 4);
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();

assert_eq!(map.index_mut_p(&0), &mut 1);

Inserts a value into the map, associating it with a key, returning the previous value.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();

map.insert(0, 1);
map.insert(2, 3);

assert_eq!(map[&0], 1);
assert_eq!(map[&2], 3);

Reserves enough space to insert reserved extra elements without reallocating.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();
map.reserve(10);

Removes all the entries in the map.

Example
use abi_stable::std_types::RHashMap;

let mut map = vec![(0, 1), (3, 4)].into_iter().collect::<RHashMap<u32, u32>>();

assert_eq!(map.contains_key(&0), true);
assert_eq!(map.contains_key(&3), true);

map.clear();

assert_eq!(map.contains_key(&0), false);
assert_eq!(map.contains_key(&3), false);

Returns the amount of entries in the map.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();

assert_eq!(map.len(), 0);
map.insert(0, 1);
assert_eq!(map.len(), 1);
map.insert(2, 3);
assert_eq!(map.len(), 2);

Returns the capacity of the map, the amount of elements it can store without reallocating.

Note that this is a lower bound, since hash maps don’t necessarily have an exact capacity.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::with_capacity(4);

assert!(map.capacity() >= 4);

Returns whether the map contains any entries.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();

assert_eq!(map.is_empty(), true);
map.insert(0, 1);
assert_eq!(map.is_empty(), false);

Iterates over the entries in the map, with references to the values in the map.

This returns a type that implements Iterator<Item= Tuple2< &K, &V > > + !Send + !Sync + Clone

Example
use abi_stable::std_types::{RHashMap, Tuple2};

let mut map = RHashMap::<u32, u32>::new();

map.insert(0, 1);
map.insert(3, 4);

let mut list = map.iter().collect::<Vec<_>>();
list.sort();
assert_eq!( list, vec![Tuple2(&0, &1), Tuple2(&3, &4)] );

Iterates over the entries in the map, with mutable references to the values in the map.

This returns a type that implements Iterator<Item= Tuple2< &K, &mut V > > + !Send + !Sync

Example
use abi_stable::std_types::{RHashMap, Tuple2};

let mut map = RHashMap::<u32, u32>::new();

map.insert(0, 1);
map.insert(3, 4);

let mut list = map.iter_mut().collect::<Vec<_>>();
list.sort();
assert_eq!( list, vec![Tuple2(&0, &mut 1), Tuple2(&3, &mut  4)] );

Clears the map, returning an iterator over all the entries that were removed.

This returns a type that implements Iterator<Item= Tuple2< K, V > > + !Send + !Sync

Example
use abi_stable::std_types::{RHashMap, Tuple2};

let mut map = RHashMap::<u32, u32>::new();

map.insert(0, 1);
map.insert(3, 4);

let mut list = map.drain().collect::<Vec<_>>();
list.sort();
assert_eq!( list, vec![Tuple2(0, 1), Tuple2(3, 4)] );

assert!(map.is_empty());

Gets a handle into the entry in the map for the key, that allows operating directly on the entry.

Example
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::<u32, u32>::new();

// Inserting an entry that wasn't there before.
{
    let mut entry = map.entry(0);
    assert_eq!(entry.get(), None);
    assert_eq!(entry.or_insert(3), &mut 3);
    assert_eq!(map.get(&0), Some(&3));
}

An iterator visiting all keys in arbitrary order. The iterator element type is &'a K.

Examples
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for key in map.keys() {
    println!("{}", key);
}

An iterator visiting all values in arbitrary order. The iterator element type is &'a V.

Examples
use abi_stable::std_types::RHashMap;

let mut map = RHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for val in map.values() {
    println!("{}", val);
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Performs the conversion.

Performs the conversion.

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

The returned type after indexing.

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

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

This returns an Iterator<Item= Tuple2< K, V > >+!Send+!Sync

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This returns an Iterator<Item= Tuple2< &K, &V > > + !Send + !Sync + Clone

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This returns a type that implements Iterator<Item= Tuple2< &K, &mut V > > + !Send + !Sync

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Whether this type has a single invalid bit-pattern. Read more

The layout of the type provided by implementors.

const-equivalents of the associated types.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

The owned type, stored in RCow::Owned

The borrowed type, stored in RCow::Borrowed

Performs the conversion.

This is always WithMetadata_<Self, Self>

Performs the conversion.

Gets a reference to a field, determined by offset. Read more

Gets a muatble reference to a field, determined by offset. Read more

Gets a const pointer to a field, the field is determined by offset. Read more

Gets a mutable pointer to a field, determined by offset. Read more

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more

Swaps a field (determined by offset) with the same field in right. Read more

Gets a copy of a field (determined by offset). The field is determined by offset. Read more

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more

Swaps a field (determined by offset) with the same field in right. Read more

Gets a copy of a field (determined by offset). The field is determined by offset. Read more

Compares the address of self with the address of other. Read more

Emulates the pipeline operator, allowing method syntax in more places. Read more

The same as piped except that the function takes &Self Useful for functions that take &Self instead of Self. Read more

The same as piped, except that the function takes &mut Self. Useful for functions that take &mut Self instead of Self. Read more

Mutates self using a closure taking self by mutable reference, passing it along the method chain. Read more

Observes the value of self, passing it along unmodified. Useful in long method chains. Read more

Performs a conversion with Into. using the turbofish .into_::<_>() syntax. Read more

Performs a reference to reference conversion with AsRef, using the turbofish .as_ref_::<_>() syntax. Read more

Performs a mutable reference to mutable reference conversion with AsMut, using the turbofish .as_mut_::<_>() syntax. Read more

Drops self using method notation. Alternative to std::mem::drop. Read more

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

Transmutes the element type of this pointer.. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

This is always Self.

Converts a value back to the original type.

Converts a reference back to the original type.

Converts a mutable reference back to the original type.

This is supported on crate feature alloc only.

Converts a box back to the original type.

This is supported on crate feature alloc only.

Converts an Arc back to the original type. Read more

This is supported on crate feature alloc only.

Converts an Rc back to the original type. Read more

Converts a value back to the original type.

Converts a reference back to the original type.

Converts a mutable reference back to the original type.

This is supported on crate feature alloc only.

Converts a box back to the original type.

This is supported on crate feature alloc only.

Converts an Arc back to the original type.

This is supported on crate feature alloc only.

Converts an Rc back to the original type.