Struct zerovec::maps::ZeroMapBorrowed
source · [−]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
sourceimpl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K::Slice: 'static,
V::Slice: 'static,
K: ?Sized,
V: ?Sized,
impl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K::Slice: 'static,
V::Slice: 'static,
K: ?Sized,
V: ?Sized,
sourcepub fn new() -> Self
pub fn new() -> Self
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());
sourceimpl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
impl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The number of elements in the ZeroMapBorrowed
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the ZeroMapBorrowed
is empty
sourceimpl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + Ord,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
impl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + Ord,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
sourcepub fn get(&self, key: &K) -> Option<&'a V::GetType>
pub fn get(&self, key: &K) -> Option<&'a V::GetType>
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"));
sourcepub fn get_by(
&self,
predicate: impl FnMut(&K) -> Ordering
) -> Option<&'a V::GetType>
pub fn get_by(
&self,
predicate: impl FnMut(&K) -> Ordering
) -> Option<&'a V::GetType>
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"));
sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
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);
sourceimpl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
impl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
sourcepub fn iter<'b>(
&'b self
) -> impl Iterator<Item = (&'a <K as ZeroMapKV<'a>>::GetType, &'a <V as ZeroMapKV<'a>>::GetType)> + 'b
pub fn iter<'b>(
&'b self
) -> impl Iterator<Item = (&'a <K as ZeroMapKV<'a>>::GetType, &'a <V as ZeroMapKV<'a>>::GetType)> + 'b
Produce an ordered iterator over key-value pairs
sourcepub fn iter_keys<'b>(
&'b self
) -> impl Iterator<Item = &'a <K as ZeroMapKV<'a>>::GetType> + 'b
pub fn iter_keys<'b>(
&'b self
) -> impl Iterator<Item = &'a <K as ZeroMapKV<'a>>::GetType> + 'b
Produce an ordered iterator over keys
sourcepub fn iter_values<'b>(
&'b self
) -> impl Iterator<Item = &'a <V as ZeroMapKV<'a>>::GetType> + 'b
pub fn iter_values<'b>(
&'b self
) -> impl Iterator<Item = &'a <V as ZeroMapKV<'a>>::GetType> + 'b
Produce an iterator over values, ordered by keys
sourceimpl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + ?Sized + Ord,
V: ZeroMapKV<'a, Slice = ZeroSlice<V>> + ?Sized,
V: AsULE + Ord + Copy + 'static,
impl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + ?Sized + Ord,
V: ZeroMapKV<'a, Slice = ZeroSlice<V>> + ?Sized,
V: AsULE + Ord + Copy + 'static,
sourcepub fn get_copied(&self, key: &K) -> Option<V>
pub fn get_copied(&self, key: &K) -> Option<V>
For cases when V
is fixed-size, obtain a direct copy of V
instead of V::ULE
sourcepub fn iter_copied_values<'b>(
&'b self
) -> impl Iterator<Item = (&'b <K as ZeroMapKV<'a>>::GetType, V)>
pub fn iter_copied_values<'b>(
&'b self
) -> impl Iterator<Item = (&'b <K as ZeroMapKV<'a>>::GetType, V)>
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
sourceimpl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a, Slice = ZeroSlice<K>> + ?Sized,
V: ZeroMapKV<'a, Slice = ZeroSlice<V>> + ?Sized,
K: AsULE + Copy + Ord + 'static,
V: AsULE + Copy + Ord + 'static,
impl<'a, K, V> ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a, Slice = ZeroSlice<K>> + ?Sized,
V: ZeroMapKV<'a, Slice = ZeroSlice<V>> + ?Sized,
K: AsULE + Copy + Ord + 'static,
V: AsULE + Copy + Ord + 'static,
sourcepub fn iter_copied<'b: 'a>(&'b self) -> impl Iterator<Item = (K, V)> + 'b
pub fn iter_copied<'b: 'a>(&'b self) -> impl Iterator<Item = (K, V)> + 'b
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
sourceimpl<'a, K, V> Bake for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
&'a K::Slice: Bake,
&'a V::Slice: Bake,
impl<'a, K, V> Bake for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
&'a K::Slice: Bake,
&'a V::Slice: Bake,
sourcefn bake(&self, env: &CrateEnv) -> TokenStream
fn bake(&self, env: &CrateEnv) -> TokenStream
Returns a TokenStream
that would evalutate to self
. Read more
sourceimpl<'a, K, V> Clone for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
impl<'a, K, V> Clone for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
sourceimpl<'a, K, V> Debug for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
K::Slice: Debug,
V::Slice: Debug,
impl<'a, K, V> Debug for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
K::Slice: Debug,
V::Slice: Debug,
sourceimpl<'a, K, V> Default for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K::Slice: 'static,
V::Slice: 'static,
K: ?Sized,
V: ?Sized,
impl<'a, K, V> Default for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K::Slice: 'static,
V::Slice: 'static,
K: ?Sized,
V: ?Sized,
sourceimpl<'de, 'a, K, V> Deserialize<'de> for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + Ord + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
K::Container: Deserialize<'de>,
V::Container: Deserialize<'de>,
K::OwnedType: Deserialize<'de>,
V::OwnedType: Deserialize<'de>,
'de: 'a,
impl<'de, 'a, K, V> Deserialize<'de> for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + Ord + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
K::Container: Deserialize<'de>,
V::Container: Deserialize<'de>,
K::OwnedType: Deserialize<'de>,
V::OwnedType: Deserialize<'de>,
'de: 'a,
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<'a, K, V> From<ZeroMapBorrowed<'a, K, V>> for ZeroMap<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
impl<'a, K, V> From<ZeroMapBorrowed<'a, K, V>> for ZeroMap<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
sourcefn from(other: ZeroMapBorrowed<'a, K, V>) -> Self
fn from(other: ZeroMapBorrowed<'a, K, V>) -> Self
Converts to this type from the input type.
sourceimpl<'a, 'b, K, V> PartialEq<ZeroMapBorrowed<'b, K, V>> for ZeroMapBorrowed<'a, K, V> where
K: for<'c> ZeroMapKV<'c> + ?Sized,
V: for<'c> ZeroMapKV<'c> + ?Sized,
<K as ZeroMapKV<'a>>::Slice: PartialEq<<K as ZeroMapKV<'b>>::Slice>,
<V as ZeroMapKV<'a>>::Slice: PartialEq<<V as ZeroMapKV<'b>>::Slice>,
impl<'a, 'b, K, V> PartialEq<ZeroMapBorrowed<'b, K, V>> for ZeroMapBorrowed<'a, K, V> where
K: for<'c> ZeroMapKV<'c> + ?Sized,
V: for<'c> ZeroMapKV<'c> + ?Sized,
<K as ZeroMapKV<'a>>::Slice: PartialEq<<K as ZeroMapKV<'b>>::Slice>,
<V as ZeroMapKV<'a>>::Slice: PartialEq<<V as ZeroMapKV<'b>>::Slice>,
sourceimpl<'a, K, V> Serialize for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + Serialize + ?Sized + Ord,
V: ZeroMapKV<'a> + Serialize + ?Sized,
K::Container: Serialize,
V::Container: Serialize,
impl<'a, K, V> Serialize for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a> + Serialize + ?Sized + Ord,
V: ZeroMapKV<'a> + Serialize + ?Sized,
K::Container: Serialize,
V::Container: Serialize,
This impl can be made available by enabling the optional serde
feature of the zerovec
crate
sourceimpl<'a, K, V> Yokeable<'a> for ZeroMapBorrowed<'static, K, V> where
K: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
&'static <K as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
&'static <V as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
impl<'a, K, V> Yokeable<'a> for ZeroMapBorrowed<'static, K, V> where
K: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
&'static <K as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
&'static <V as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
This impl can be made available by enabling the optional yoke
feature of the zerovec
crate
type Output = ZeroMapBorrowed<'a, K, V>
type Output = ZeroMapBorrowed<'a, K, V>
This type MUST be Self
with the 'static
replaced with 'a
, i.e. Self<'a>
sourcefn transform(&'a self) -> &'a Self::Output
fn transform(&'a self) -> &'a Self::Output
This method must cast self
between &'a Self<'static>
and &'a Self<'a>
. Read more
sourcefn transform_owned(self) -> Self::Output
fn transform_owned(self) -> Self::Output
This method must cast self
between Self<'static>
and Self<'a>
. Read more
sourceunsafe fn make(from: Self::Output) -> Self
unsafe fn make(from: Self::Output) -> Self
This method can be used to cast away Self<'a>
’s lifetime. Read more
sourcefn 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> Copy for ZeroMapBorrowed<'a, K, V> where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
Auto Trait Implementations
impl<'a, K: ?Sized, V: ?Sized> RefUnwindSafe for ZeroMapBorrowed<'a, K, V> where
<K as ZeroMapKV<'a>>::Slice: RefUnwindSafe,
<V as ZeroMapKV<'a>>::Slice: RefUnwindSafe,
impl<'a, K: ?Sized, V: ?Sized> Send for ZeroMapBorrowed<'a, K, V> where
<K as ZeroMapKV<'a>>::Slice: Sync,
<V as ZeroMapKV<'a>>::Slice: Sync,
impl<'a, K: ?Sized, V: ?Sized> Sync for ZeroMapBorrowed<'a, K, V> where
<K as ZeroMapKV<'a>>::Slice: Sync,
<V as ZeroMapKV<'a>>::Slice: Sync,
impl<'a, K: ?Sized, V: ?Sized> Unpin for ZeroMapBorrowed<'a, K, V>
impl<'a, K: ?Sized, V: ?Sized> UnwindSafe for ZeroMapBorrowed<'a, K, V> where
<K as ZeroMapKV<'a>>::Slice: RefUnwindSafe,
<V as ZeroMapKV<'a>>::Slice: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more