pub struct ZeroMap2d<'a, K0, K1, V>where
    K0: ZeroMapKV<'a>,
    K1: ZeroMapKV<'a>,
    V: ZeroMapKV<'a>,
    K0: ?Sized,
    K1: ?Sized,
    V: ?Sized,
{ /* private fields */ }
Expand description

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:

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; 51] = &[
    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, 11, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 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

Creates a new, empty ZeroMap2d.

Examples
use zerovec::ZeroMap2d;

let zm: ZeroMap2d<u16, str, str> = ZeroMap2d::new();
assert!(zm.is_empty());

Construct a new ZeroMap2d with a given capacity

Obtain a borrowed version of this map

The number of values in the ZeroMap2d

Whether the ZeroMap2d is empty

Remove all elements from the ZeroMap2d

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

See Vec::reserve() for more information.

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);

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);

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

See example in Self::get_2d().

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);

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);

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);

Binary search the map for key0, returning a cursor.

use zerovec::maps::ZeroMap2dBorrowed;
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));

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_eq!(map.contains_key0(&1), true);
assert_eq!(map.contains_key0(&3), false);

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

Examples
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));

Trait Implementations

Returns a TokenStream that would evalutate to self. Read more
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

This impl can be made available by enabling the optional serde feature of the zerovec crate

Deserialize this value from the given Serde deserializer. Read more
Converts to this type from the input type.
Creates a value from an iterator. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

This impl can be made available by enabling the optional serde feature of the zerovec crate

Serialize this value into the given Serde serializer. Read more

This impl can be made available by enabling the optional yoke feature of the zerovec crate

This type MUST be Self with the 'static replaced with 'a, i.e. Self<'a>
This method must cast self between &'a Self<'static> and &'a Self<'a>. Read more
This method must cast self between Self<'static> and Self<'a>. Read more
This method can be used to cast away Self<'a>’s lifetime. Read more
This method must cast self between &'a mut Self<'static> and &'a mut Self<'a>, and pass it to f. Read more
Clone the other C into a struct that may retain references into C.

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

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. 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.