Struct ZeroMap2d

struct ZeroMap2d<'a, K0, K1, V> { ... }
where
    K0: ZeroMapKV<'a> + ?Sized,
    K1: ZeroMapKV<'a> + ?Sized,
    V: ZeroMapKV<'a> + ?Sized

A zero-copy, two-dimensional map datastructure .

This is an extension of ZeroMap that supports two layers of keys. For example, to map a pair of an integer and a string to a buffer, you can write:

# use zerovec::ZeroMap2d;
let _: ZeroMap2d<u32, str, [u8]> = unimplemented!();

Internally, ZeroMap2d stores four zero-copy vectors, one for each type argument plus one more to match between the two vectors of keys.

Examples

use zerovec::ZeroMap2d;

// Example byte buffer representing the map { 1: {2: "three" } }
let BINCODE_BYTES: &[u8; 47] = &[
    2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0,
    0, 0, 0, 0, 0, 0, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 1, 0, 116, 104, 114,
    101, 101,
];

// Deserializing to ZeroMap requires no heap allocations.
let zero_map: ZeroMap2d<u16, u16, str> =
    bincode::deserialize(BINCODE_BYTES)
        .expect("Should deserialize successfully");
assert_eq!(zero_map.get_2d(&1, &2), Some("three"));

Implementations

impl<'a, K0, K1, V> ZeroMap2d<'a, K0, K1, V>

fn new() -> Self

Creates a new, empty ZeroMap2d.

Examples

use zerovec::ZeroMap2d;

let zm: ZeroMap2d<u16, str, str> = ZeroMap2d::new();
assert!(zm.is_empty());
fn with_capacity(capacity: usize) -> Self

Construct a new ZeroMap2d with a given capacity

fn as_borrowed(self: &'a Self) -> ZeroMap2dBorrowed<'a, K0, K1, V>

Obtain a borrowed version of this map

fn len(self: &Self) -> usize

The number of values in the ZeroMap2d

fn is_empty(self: &Self) -> bool

Whether the ZeroMap2d is empty

fn clear(self: &mut Self)

Remove all elements from the ZeroMap2d

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

Reserve capacity for additional more elements to be inserted into the ZeroMap2d to avoid frequent reallocations.

See Vec::reserve() for more information.

fn iter0<'l>(self: &'l Self) -> impl Iterator<Item = ZeroMap2dCursor<'l, 'a, K0, K1, V>> + 'l

Produce an ordered iterator over keys0, which can then be used to get an iterator over keys1 for a particular key0.

Example

Loop over all elements of a ZeroMap2d:

use zerovec::ZeroMap2d;

let mut map: ZeroMap2d<u16, u16, str> = ZeroMap2d::new();
map.insert(&1, &1, "foo");
map.insert(&2, &3, "bar");
map.insert(&2, &4, "baz");

let mut total_value = 0;

for cursor in map.iter0() {
    for (key1, value) in cursor.iter1() {
        // This code runs for every (key0, key1) pair
        total_value += cursor.key0().as_unsigned_int() as usize;
        total_value += key1.as_unsigned_int() as usize;
        total_value += value.len();
    }
}

assert_eq!(total_value, 22);

impl<'a, K0, K1, V> ZeroMap2d<'a, K0, K1, V>

fn get_copied_2d(self: &Self, key0: &K0, key1: &K1) -> Option<V>

For cases when V is fixed-size, obtain a direct copy of V instead of V::ULE

Examples

# use zerovec::ZeroMap2d;
let mut map: ZeroMap2d<u16, u16, u16> = ZeroMap2d::new();
map.insert(&1, &2, &3);
map.insert(&1, &4, &5);
map.insert(&6, &7, &8);

assert_eq!(map.get_copied_2d(&6, &7), Some(8));

impl<'a, K0, K1, V> ZeroMap2d<'a, K0, K1, V>

fn get_2d(self: &Self, key0: &K0, key1: &K1) -> Option<&<V as >::GetType>

Get the value associated with key0 and key1, if it exists.

For more fine-grained error handling, use ZeroMap2d::get0.

use zerovec::ZeroMap2d;

let mut map = ZeroMap2d::new();
map.insert(&1, "one", "foo");
map.insert(&2, "one", "bar");
map.insert(&2, "two", "baz");
assert_eq!(map.get_2d(&1, "one"), Some("foo"));
assert_eq!(map.get_2d(&1, "two"), None);
assert_eq!(map.get_2d(&2, "one"), Some("bar"));
assert_eq!(map.get_2d(&2, "two"), Some("baz"));
assert_eq!(map.get_2d(&3, "three"), None);
fn insert(self: &mut Self, key0: &K0, key1: &K1, value: &V) -> Option<<V as >::OwnedType>

Insert value with key, returning the existing value if it exists.

use zerovec::ZeroMap2d;

let mut map = ZeroMap2d::new();
assert_eq!(map.insert(&0, "zero", "foo"), None,);
assert_eq!(map.insert(&1, "one", "bar"), None,);
assert_eq!(map.insert(&1, "one", "baz").as_deref(), Some("bar"),);
assert_eq!(map.get_2d(&1, "one").as_deref(), Some("baz"));
assert_eq!(map.len(), 2);
fn remove(self: &mut Self, key0: &K0, key1: &K1) -> Option<<V as >::OwnedType>

Remove the value at key, returning it if it exists.

use zerovec::ZeroMap2d;

let mut map = ZeroMap2d::new();
map.insert(&1, "one", "foo");
map.insert(&2, "two", "bar");
assert_eq!(
    map.remove(&1, "one"),
    Some("foo".to_owned().into_boxed_str())
);
assert_eq!(map.get_2d(&1, "one"), None);
assert_eq!(map.remove(&1, "one"), None);
fn try_append<'b>(self: &mut Self, key0: &'b K0, key1: &'b K1, value: &'b V) -> Option<(&'b K0, &'b K1, &'b V)>

Appends value with key to the end of the underlying vector, returning key and value if it failed. Useful for extending with an existing sorted list.

use zerovec::ZeroMap2d;

let mut map = ZeroMap2d::new();
assert!(map.try_append(&1, "one", "uno").is_none());
assert!(map.try_append(&3, "three", "tres").is_none());

let unsuccessful = map.try_append(&3, "three", "tres-updated");
assert!(unsuccessful.is_some(), "append duplicate of last key");

let unsuccessful = map.try_append(&2, "two", "dos");
assert!(unsuccessful.is_some(), "append out of order");

assert_eq!(map.get_2d(&1, "one"), Some("uno"));

// contains the original value for the key: 3
assert_eq!(map.get_2d(&3, "three"), Some("tres"));

// not appended since it wasn't in order
assert_eq!(map.get_2d(&2, "two"), None);

impl<'a, K0, K1, V> ZeroMap2d<'a, K0, K1, V>

fn get0<'l>(self: &'l Self, key0: &K0) -> Option<ZeroMap2dCursor<'l, 'a, K0, K1, V>>

Gets a cursor for key0. If None, then key0 is not in the map. If Some, then key0 is in the map, and key1 can be queried.

use zerovec::ZeroMap2d;

let mut map = ZeroMap2d::new();
map.insert(&1u32, "one", "foo");
map.insert(&2, "one", "bar");
map.insert(&2, "two", "baz");
assert_eq!(map.get0(&1).unwrap().get1("one").unwrap(), "foo");
assert_eq!(map.get0(&1).unwrap().get1("two"), None);
assert_eq!(map.get0(&2).unwrap().get1("one").unwrap(), "bar");
assert_eq!(map.get0(&2).unwrap().get1("two").unwrap(), "baz");
assert_eq!(map.get0(&3), None);
fn get0_by<'l, impl FnMut(&K0) -> Ordering: FnMut(&K0) -> Ordering>(self: &'l Self, predicate: impl FnMut(&K0) -> Ordering) -> Option<ZeroMap2dCursor<'l, 'a, K0, K1, V>>

Binary search the map for key0, returning a cursor.

use zerovec::ZeroMap2d;

let mut map = ZeroMap2d::new();
map.insert(&1, "one", "foo");
map.insert(&2, "two", "bar");
assert!(matches!(map.get0_by(|probe| probe.cmp(&1)), Some(_)));
assert!(matches!(map.get0_by(|probe| probe.cmp(&3)), None));
fn contains_key0(self: &Self, key0: &K0) -> bool

Returns whether key0 is contained in this map

use zerovec::ZeroMap2d;

let mut map = ZeroMap2d::new();
map.insert(&1, "one", "foo");
map.insert(&2, "two", "bar");
assert!(map.contains_key0(&1));
assert!(!map.contains_key0(&3));

impl<'a, 'b, K0, K1, V> PartialEq for ZeroMap2d<'a, K0, K1, V>

fn eq(self: &Self, other: &ZeroMap2d<'b, K0, K1, V>) -> bool

impl<'a, A, B, C, K0, K1, V> FromIterator for ZeroMap2d<'a, K0, K1, V>

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

impl<'a, K0, K1, V> Clone for ZeroMap2d<'a, K0, K1, V>

fn clone(self: &Self) -> Self

impl<'a, K0, K1, V> Debug for ZeroMap2d<'a, K0, K1, V>

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

impl<'a, K0, K1, V> Default for ZeroMap2d<'a, K0, K1, V>

fn default() -> Self

impl<'a, K0, K1, V> Freeze for ZeroMap2d<'a, K0, K1, V>

impl<'a, K0, K1, V> From for ZeroMap2d<'a, K0, K1, V>

fn from(other: ZeroMap2dBorrowed<'a, K0, K1, V>) -> Self

impl<'a, K0, K1, V> RefUnwindSafe for ZeroMap2d<'a, K0, K1, V>

impl<'a, K0, K1, V> Send for ZeroMap2d<'a, K0, K1, V>

impl<'a, K0, K1, V> Sync for ZeroMap2d<'a, K0, K1, V>

impl<'a, K0, K1, V> Unpin for ZeroMap2d<'a, K0, K1, V>

impl<'a, K0, K1, V> UnsafeUnpin for ZeroMap2d<'a, K0, K1, V>

impl<'a, K0, K1, V> UnwindSafe for ZeroMap2d<'a, K0, K1, V>

impl<'a, K0, K1, V> Yokeable for ZeroMap2d<'static, K0, K1, V>

fn transform(self: &'a Self) -> &'a <Self as >::Output
fn transform_owned(self: Self) -> <Self as >::Output
unsafe fn make(from: <Self as >::Output) -> Self
fn transform_mut<F>(self: &'a mut Self, f: F)
where
    F: 'static + for<'b> FnOnce(&'b mut <Self as >::Output)

impl<'zf, 's, K0, K1, V> ZeroFrom for ZeroMap2d<'zf, K0, K1, V>

fn zero_from(other: &'zf ZeroMap2d<'s, K0, K1, V>) -> Self

impl<T> Any for ZeroMap2d<'a, K0, K1, V>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for ZeroMap2d<'a, K0, K1, V>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for ZeroMap2d<'a, K0, K1, V>

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

impl<T> CloneToUninit for ZeroMap2d<'a, K0, K1, V>

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

impl<T> ErasedDestructor for ZeroMap2d<'a, K0, K1, V>

impl<T> From for ZeroMap2d<'a, K0, K1, V>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for ZeroMap2d<'a, K0, K1, V>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for ZeroMap2d<'a, K0, K1, V>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for ZeroMap2d<'a, K0, K1, V>

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

impl<T, U> TryInto for ZeroMap2d<'a, K0, K1, V>

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