Struct zerovec::map::ZeroMap [−][src]
pub struct ZeroMap<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized, { /* fields omitted */ }
Expand description
A zero-copy map datastructure, built on sorted binary-searchable ZeroVec
and VarZeroVec
.
This type, like ZeroVec
and VarZeroVec
, is able to zero-copy
deserialize from appropriately formatted byte buffers. It is internally copy-on-write, so it can be mutated
afterwards as necessary.
Internally, a ZeroMap
is a zero-copy vector for keys paired with a zero-copy vector for
values, sorted by the keys. Therefore, all types used in ZeroMap
need to work with either
ZeroVec
or VarZeroVec
.
Examples
use zerovec::ZeroMap;
// Example byte buffer representing the map { 1: "one" }
let BINCODE_BYTES: &[u8; 31] = &[
4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 111, 110, 101
];
// Deserializing to ZeroMap requires no heap allocations.
let zero_map: ZeroMap<u32, str> = bincode::deserialize(BINCODE_BYTES)
.expect("Should deserialize successfully");
assert_eq!(zero_map.get(&1), Some("one"));
Implementations
Construct a new ZeroMap
with a given capacity
Reserve capacity for additional
more elements to be inserted into
the ZeroMap
to avoid frequent reallocations.
See Vec::reserve()
for more information.
Get the value associated with key
, if it exists.
use zerovec::ZeroMap;
let mut map = ZeroMap::new();
map.insert(&1, "one");
map.insert(&2, "two");
assert_eq!(map.get(&1), Some("one"));
assert_eq!(map.get(&3), None);
Returns whether key
is contained in this map
use zerovec::ZeroMap;
let mut map = ZeroMap::new();
map.insert(&1, "one");
map.insert(&2, "two");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&3), false);
Insert value
with key
, returning the existing value if it exists.
use zerovec::ZeroMap;
let mut map = ZeroMap::new();
map.insert(&1, "one");
map.insert(&2, "two");
assert_eq!(map.get(&1), Some("one"));
assert_eq!(map.get(&3), None);
Remove the value at key
, returning it if it exists.
use zerovec::ZeroMap;
let mut map = ZeroMap::new();
map.insert(&1, "one");
map.insert(&2, "two");
assert_eq!(map.remove(&1), Some("one".to_owned().into_boxed_str()));
assert_eq!(map.get(&1), 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::ZeroMap;
let mut map = ZeroMap::new();
assert!(map.try_append(&1, "uno").is_none());
assert!(map.try_append(&3, "tres").is_none());
let unsuccessful = map.try_append(&3, "tres-updated");
assert!(unsuccessful.is_some(), "append duplicate of last key");
let unsuccessful = map.try_append(&2, "dos");
assert!(unsuccessful.is_some(), "append out of order");
assert_eq!(map.get(&1), Some("uno"));
// contains the original value for the key: 3
assert_eq!(map.get(&3), Some("tres"));
// not appended since it wasn't in order
assert_eq!(map.get(&2), None);
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
impl<'de, K: ?Sized, V: ?Sized> Deserialize<'de> for ZeroMap<'de, K, V> where
K: Ord,
K::Container: Deserialize<'de>,
V::Container: Deserialize<'de>,
K: ZeroMapKV<'de>,
V: ZeroMapKV<'de>,
K::OwnedType: Deserialize<'de>,
V::OwnedType: Deserialize<'de>,
impl<'de, K: ?Sized, V: ?Sized> Deserialize<'de> for ZeroMap<'de, K, V> where
K: Ord,
K::Container: Deserialize<'de>,
V::Container: Deserialize<'de>,
K: ZeroMapKV<'de>,
V: ZeroMapKV<'de>,
K::OwnedType: Deserialize<'de>,
V::OwnedType: Deserialize<'de>,
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
This impl can be made available by enabling the optional serde
feature of the zerovec
crate
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
fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
This method must cast self
between &'a mut Self<'static>
and &'a mut Self<'a>
,
and pass it to f
. Read more
impl<'a, K, V> ZeroCopyFrom<ZeroMap<'a, K, V>> for ZeroMap<'static, K, V> where
K: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
<K as ZeroMapKV<'static>>::Container: for<'b> ZeroCopyFrom<<K as ZeroMapKV<'b>>::Container>,
<V as ZeroMapKV<'static>>::Container: for<'b> ZeroCopyFrom<<V as ZeroMapKV<'b>>::Container>,
<K as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b, Output = <K as ZeroMapKV<'b>>::Container>,
<V as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b, Output = <V as ZeroMapKV<'b>>::Container>,
impl<'a, K, V> ZeroCopyFrom<ZeroMap<'a, K, V>> for ZeroMap<'static, K, V> where
K: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
<K as ZeroMapKV<'static>>::Container: for<'b> ZeroCopyFrom<<K as ZeroMapKV<'b>>::Container>,
<V as ZeroMapKV<'static>>::Container: for<'b> ZeroCopyFrom<<V as ZeroMapKV<'b>>::Container>,
<K as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b, Output = <K as ZeroMapKV<'b>>::Container>,
<V as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b, Output = <V as ZeroMapKV<'b>>::Container>,
Clone the cart C
into a Yokeable
struct, which may retain references into C
.