Struct zerovec::maps::ZeroMap2dBorrowed

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

A borrowed-only version of ZeroMap2d

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 ZeroMap2dBorrowed instance.

§Examples

use zerovec::maps::ZeroMap2dBorrowed;

// 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 ZeroMap2d requires no heap allocations.
let zero_map: ZeroMap2dBorrowed<u16, u16, str> =
    bincode::deserialize(BINCODE_BYTES)
        .expect("Should deserialize successfully");
assert_eq!(zero_map.get_2d(&1, &2), Some("three"));

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

Implementations§

source§

impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized, K0::Slice: 'static, K1::Slice: 'static, V::Slice: 'static,

source

pub fn new() -> Self

Creates a new, empty ZeroMap2dBorrowed<K0, K1, V>.

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

§Examples
use zerovec::maps::ZeroMap2dBorrowed;

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

impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized,

source

pub fn len(&self) -> usize

The number of elements in the ZeroMap2dBorrowed

source

pub fn is_empty(&self) -> bool

Whether the ZeroMap2dBorrowed is empty

source§

impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + Ord + ?Sized, K1: ZeroMapKV<'a> + Ord + ?Sized, V: ZeroMapKV<'a> + ?Sized,

source

pub fn get_2d(&self, key0: &K0, key1: &K1) -> Option<&'a V::GetType>

Get the value associated with key0 and key1, 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 ZeroMap2dBorrowed over ZeroMap2d.

use zerovec::ZeroMap2d;

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

let borrowed = map.as_borrowed();
assert_eq!(borrowed.get_2d(&1, "one"), Some("foo"));
assert_eq!(borrowed.get_2d(&1, "two"), None);
assert_eq!(borrowed.get_2d(&2, "one"), Some("bar"));
assert_eq!(borrowed.get_2d(&2, "two"), Some("baz"));
assert_eq!(borrowed.get_2d(&3, "three"), None);
source§

impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + Ord + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized,

source

pub fn get0<'l>( &'l self, key0: &K0, ) -> Option<ZeroMap2dCursor<'a, '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(&1, "one", "foo");
map.insert(&2, "two", "bar");
let borrowed = map.as_borrowed();
assert!(matches!(borrowed.get0(&1), Some(_)));
assert!(matches!(borrowed.get0(&3), None));
source

pub fn get0_by<'l>( &'l self, predicate: impl FnMut(&K0) -> Ordering, ) -> Option<ZeroMap2dCursor<'a, '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");
let borrowed = map.as_borrowed();
assert!(matches!(borrowed.get0_by(|probe| probe.cmp(&1)), Some(_)));
assert!(matches!(borrowed.get0_by(|probe| probe.cmp(&3)), None));
source

pub fn contains_key0(&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");
let borrowed = map.as_borrowed();
assert!(borrowed.contains_key0(&1));
assert!(!borrowed.contains_key0(&3));
source§

impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized,

source

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

Produce an ordered iterator over keys0

source§

impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + Ord + ?Sized, K1: ZeroMapKV<'a> + Ord + ?Sized, V: ZeroMapKV<'a> + Copy,

source

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

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

Trait Implementations§

source§

impl<'a, K0, K1, V> Bake for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized, &'a K0::Slice: Bake, &'a K1::Slice: Bake, &'a V::Slice: Bake,

source§

fn bake(&self, env: &CrateEnv) -> TokenStream

Returns a TokenStream that would evaluate to self. Read more
source§

impl<'a, K0, K1, V> BakeSize for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized, &'a K0::Slice: BakeSize, &'a K1::Slice: BakeSize, &'a V::Slice: BakeSize,

source§

fn borrows_size(&self) -> usize

Returns the size
source§

impl<'a, K0, K1, V> Clone for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, K0, K1, V> Debug for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized, K0::Slice: Debug, K1::Slice: Debug, V::Slice: Debug,

source§

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

Formats the value using the given formatter. Read more
source§

impl<'a, K0, K1, V> Default for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized, K0::Slice: 'static, K1::Slice: 'static, V::Slice: 'static,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de, 'a, K0, K1, V> Deserialize<'de> for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + Ord + ?Sized, K1: ZeroMapKV<'a> + Ord + ?Sized, V: ZeroMapKV<'a> + ?Sized, K0::Container: Deserialize<'de>, K1::Container: Deserialize<'de>, V::Container: Deserialize<'de>, K0::OwnedType: Deserialize<'de>, K1::OwnedType: Deserialize<'de>, V::OwnedType: Deserialize<'de>, 'de: 'a,

This impl requires enabling the optional serde Cargo feature of the zerovec crate

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a, K0, K1, V> From<ZeroMap2dBorrowed<'a, K0, K1, V>> for ZeroMap2d<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized,

source§

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

Converts to this type from the input type.
source§

impl<'a, 'b, K0, K1, V> PartialEq<ZeroMap2dBorrowed<'b, K0, K1, V>> for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: for<'c> ZeroMapKV<'c> + ?Sized, K1: for<'c> ZeroMapKV<'c> + ?Sized, V: for<'c> ZeroMapKV<'c> + ?Sized, <K0 as ZeroMapKV<'a>>::Slice: PartialEq<<K0 as ZeroMapKV<'b>>::Slice>, <K1 as ZeroMapKV<'a>>::Slice: PartialEq<<K1 as ZeroMapKV<'b>>::Slice>, <V as ZeroMapKV<'a>>::Slice: PartialEq<<V as ZeroMapKV<'b>>::Slice>,

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, K0, K1, V> Serialize for ZeroMap2dBorrowed<'a, K0, K1, V>

This impl requires enabling the optional serde Cargo feature of the zerovec crate

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'a, K0, K1, V> Yokeable<'a> for ZeroMap2dBorrowed<'static, K0, K1, V>
where K0: 'static + for<'b> ZeroMapKV<'b> + ?Sized, K1: 'static + for<'b> ZeroMapKV<'b> + ?Sized, V: 'static + for<'b> ZeroMapKV<'b> + ?Sized, &'static <K0 as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>, &'static <K1 as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>, &'static <V as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,

This impl requires enabling the optional yoke Cargo feature of the zerovec crate

source§

type Output = ZeroMap2dBorrowed<'a, K0, K1, V>

This type MUST be Self with the 'static replaced with 'a, i.e. Self<'a>
source§

fn transform(&'a self) -> &'a Self::Output

This method must cast self between &'a Self<'static> and &'a Self<'a>. Read more
source§

fn transform_owned(self) -> Self::Output

This method must cast self between Self<'static> and Self<'a>. Read more
source§

unsafe fn make(from: Self::Output) -> Self

This method can be used to cast away Self<'a>’s lifetime. Read more
source§

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
source§

impl<'a, K0, K1, V> Copy for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ZeroMapKV<'a> + ?Sized, K1: ZeroMapKV<'a> + ?Sized, V: ZeroMapKV<'a> + ?Sized,

Auto Trait Implementations§

§

impl<'a, K0, K1, V> Freeze for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ?Sized, K1: ?Sized, V: ?Sized,

§

impl<'a, K0, K1, V> RefUnwindSafe for ZeroMap2dBorrowed<'a, K0, K1, V>
where <K0 as ZeroMapKV<'a>>::Slice: RefUnwindSafe, <K1 as ZeroMapKV<'a>>::Slice: RefUnwindSafe, <V as ZeroMapKV<'a>>::Slice: RefUnwindSafe, K0: ?Sized, K1: ?Sized, V: ?Sized,

§

impl<'a, K0, K1, V> Send for ZeroMap2dBorrowed<'a, K0, K1, V>
where <K0 as ZeroMapKV<'a>>::Slice: Sync, <K1 as ZeroMapKV<'a>>::Slice: Sync, <V as ZeroMapKV<'a>>::Slice: Sync, K0: ?Sized, K1: ?Sized, V: ?Sized,

§

impl<'a, K0, K1, V> Sync for ZeroMap2dBorrowed<'a, K0, K1, V>
where <K0 as ZeroMapKV<'a>>::Slice: Sync, <K1 as ZeroMapKV<'a>>::Slice: Sync, <V as ZeroMapKV<'a>>::Slice: Sync, K0: ?Sized, K1: ?Sized, V: ?Sized,

§

impl<'a, K0, K1, V> Unpin for ZeroMap2dBorrowed<'a, K0, K1, V>
where K0: ?Sized, K1: ?Sized, V: ?Sized,

§

impl<'a, K0, K1, V> UnwindSafe for ZeroMap2dBorrowed<'a, K0, K1, V>
where <K0 as ZeroMapKV<'a>>::Slice: RefUnwindSafe, <K1 as ZeroMapKV<'a>>::Slice: RefUnwindSafe, <V as ZeroMapKV<'a>>::Slice: RefUnwindSafe, K0: ?Sized, K1: ?Sized, V: ?Sized,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<T> ErasedDestructor for T
where T: 'static,