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

A borrowed-only version of ZeroMap

This is useful for fully-zero-copy deserialization from non-human-readable serialization formats. It also has the advantage that it can return references that live for the lifetime of the backing buffer as opposed to that of the ZeroMapBorrowed instance.

Examples

use zerovec::maps::ZeroMapBorrowed;

// Example byte buffer representing the map { 1: "one" }
let BINCODE_BYTES: &[u8; 29] = &[
    4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 111,
    110, 101,
];

// Deserializing to ZeroMap requires no heap allocations.
let zero_map: ZeroMapBorrowed<u32, str> =
    bincode::deserialize(BINCODE_BYTES).expect("Should deserialize successfully");
assert_eq!(zero_map.get(&1), Some("one"));

This can be obtained from a ZeroMap via ZeroMap::as_borrowed

Implementations

Creates a new, empty ZeroMapBorrowed<K, V>.

Note: Since ZeroMapBorrowed is not mutable, the return value will be a stub unless converted into a ZeroMap.

Examples
use zerovec::maps::ZeroMapBorrowed;

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

The number of elements in the ZeroMapBorrowed

Whether the ZeroMapBorrowed is empty

Get the value associated with key, if it exists.

This is able to return values that live longer than the map itself since they borrow directly from the backing buffer. This is the primary advantage of using ZeroMapBorrowed over ZeroMap.

use zerovec::maps::ZeroMapBorrowed;
use zerovec::ZeroMap;

let mut map = ZeroMap::new();
map.insert(&1, "one");
map.insert(&2, "two");
let borrowed = map.as_borrowed();
assert_eq!(borrowed.get(&1), Some("one"));
assert_eq!(borrowed.get(&3), None);

let borrow = borrowed.get(&1);
drop(borrowed);
// still exists after the ZeroMapBorrowed has been dropped
assert_eq!(borrow, Some("one"));

Binary search the map with predicate to find a key, returning the value.

This is able to return values that live longer than the map itself since they borrow directly from the backing buffer. This is the primary advantage of using ZeroMapBorrowed over ZeroMap.

use zerovec::maps::ZeroMapBorrowed;
use zerovec::ZeroMap;

let mut map = ZeroMap::new();
map.insert(&1, "one");
map.insert(&2, "two");
let borrowed = map.as_borrowed();
assert_eq!(borrowed.get_by(|probe| probe.cmp(&1)), Some("one"));
assert_eq!(borrowed.get_by(|probe| probe.cmp(&3)), None);

let borrow = borrowed.get_by(|probe| probe.cmp(&1));
drop(borrowed);
// still exists after the ZeroMapBorrowed has been dropped
assert_eq!(borrow, Some("one"));

Returns whether key is contained in this map

use zerovec::maps::ZeroMapBorrowed;
use zerovec::ZeroMap;

let mut map = ZeroMap::new();
map.insert(&1, "one");
map.insert(&2, "two");
let borrowed = map.as_borrowed();
assert_eq!(borrowed.contains_key(&1), true);
assert_eq!(borrowed.contains_key(&3), false);

Produce an ordered iterator over key-value pairs

Produce an ordered iterator over keys

Produce an iterator over values, ordered by keys

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

Similar to Self::iter() except it returns a direct copy of the values instead of references to V::ULE, in cases when V is fixed-size

Similar to Self::iter() except it returns a direct copy of the keys values instead of references to K::ULE and V::ULE, in cases when K and V are fixed-size

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

Deserialize this value from the given Serde deserializer. Read more

Converts to this type from the input type.

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

This method tests for !=.

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

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.