Skip to main content

Map

Struct Map 

Source
pub struct Map<K, V> { /* private fields */ }
Expand description

An immutable, reference-counted map from K to V, passed across languages as ffi.Map.

Cloning a Map is cheap: it shares the same entries.

§Examples

use tvm_ffi::{Map, String};

let map: Map<String, i64> = [(String::from("a"), 1), (String::from("b"), 2)]
    .into_iter()
    .collect();
assert_eq!(map.len(), 2);
assert_eq!(map.get("b")?, Some(2));
assert_eq!(map.get("c")?, None);

Implementations§

Source§

impl<K, V> Map<K, V>

Source

pub fn new() -> Self

Creates an empty map.

Source

pub fn len(&self) -> usize

Returns the number of entries.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no entries.

Source

pub fn contains_key(&self, key: impl IntoArg<K>) -> Result<bool>

Returns whether key is present.

§Errors

Returns an error if the runtime cannot look key up, such as when it is not hashable.

The result for a key of another type than the map’s keys is unspecified, as for Map::get. Code generic over K that passes a &K adds the bound for<'a> &'a K: IntoArg<K>.

Source

pub fn get(&self, key: impl IntoArg<K>) -> Result<Option<V>>

Looks up key, returning Ok(None) if it is absent.

§Errors

Returns an error if the runtime cannot look key up, such as when it is not hashable, or if the value is not a V.

The result for a key of another type than the map’s keys is unspecified: it currently reads as absent, since a lookup hashes the key instead of reading a stored one. Debug builds assert against it.

A &str key builds a temporary String for the runtime lookup. Code generic over K that passes a &K adds the bound for<'a> &'a K: IntoArg<K>.

Source

pub fn iter(&self) -> Iter<K, V> ⓘ

Returns an iterator over the (key, value) pairs of the map.

Source

pub fn keys(&self) -> Keys<K> ⓘ

Returns an iterator over the keys of the map.

Source

pub fn values(&self) -> Values<V> ⓘ

Returns an iterator over the values of the map.

Source§

impl<K, V> Map<K, V>

Source

pub fn upcast<KU: TypeSubsumes<K>, VU: TypeSubsumes<V>>(self) -> Map<KU, VU>

View this map as a Map<KU, VU> without a copy, as C++ Map<KU, VU>(const Map<K, V>&) does for bases KU and VU.

It is a method, not From, for the reasons on Array::upcast.

Source

pub fn upcast_ref<KU: TypeSubsumes<K>, VU: TypeSubsumes<V>>( &self, ) -> &Map<KU, VU>

Borrow this map as a Map<KU, VU>, as upcast does.

Trait Implementations§

Source§

impl<K, V> AnyCompatible for Map<K, V>

Source§

unsafe fn is_undefined(field: *const Self) -> bool

Whether field, which holds a value of this type or all-zero bytes, holds all-zero bytes that are no value of this type, as a null object reference, a string of type index None or a zero device type: a field that a creator left undefined for reflection to set. An undefined field is no value; a nullable reference is Option<T>, whose None is a defined value. Read more
Source§

fn type_str() -> String

The type name in conversion errors and type schemas, such as int, ffi.String or Array<int>.
Source§

fn type_schema() -> String

The type schema of the type: the JSON object that describes it, which the type schemas of functions and fields embed. Read more
Source§

unsafe fn check_any_strict(data: &TVMFFIAny) -> bool

Whether data holds a Self as is, without conversion. Read more
Source§

unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny)

Write a cell that borrows src into data. Read more
Source§

unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny)

Write a cell that owns src into data. Read more
Source§

unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self

Copy a Self out of a cell that holds one, retaining any object. Read more
Source§

unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self

Move a Self out of an owning cell that holds one. Read more
Source§

unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()>

Convert data into a Self by the FFI rules, such as an integer into a float; Err if it does not convert. Read more
Source§

fn get_mismatch_type_info(data: &TVMFFIAny) -> String

The name of the mismatched value data in conversion errors; by default its runtime type key.
Source§

fn to_any(&self) -> Any

Borrow self into an owned Any, increfing object-backed values. Read more
Source§

impl<K, V> Clone for Map<K, V>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<K, V> Debug for Map<K, V>

Source§

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

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

impl<K, V> Default for Map<K, V>

Source§

fn default() -> Self

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

impl<K, V> Deref for Map<K, V>

Source§

type Target = MapObj

The resulting type after dereferencing.
Source§

fn deref(&self) -> &MapObj

Dereferences the value.
Source§

impl<K: ContainerElement, V: ContainerElement> Display for Map<K, V>

Formats the map as its repr; see AnyView.

Source§

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

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

impl<K, V> DowncastRefFromAny for Map<K, V>

Source§

unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self>

Borrow cell as Self when it holds one. Read more
Source§

impl<K, V> From<&Map<K, V>> for Map<K, V>

A reference-count bump, as From<&T> for T of an object reference.

Source§

fn from(value: &Map<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K: ContainerElement, V: ContainerElement, const N: usize> From<[(K, V); N]> for Map<K, V>

Source§

fn from(pairs: [(K, V); N]) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<Map<K, V>> for ObjectRef
where Map<K, V>: ObjectRefCore,

A map upcasts to the root ObjectRef.

Source§

fn from(value: Map<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K, V> FromIterator<(K, V)> for Map<K, V>

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

For duplicate keys, a later pair overwrites an earlier one, as in HashMap, so the resulting map may be smaller than the iterator.

Source§

impl<'a, K, V, KU: TypeSubsumes<K> + 'a, VU: TypeSubsumes<V> + 'a> IntoArg<Map<KU, VU>> for &'a Map<K, V>

Source§

type Output = &'a Map<KU, VU>

T when owned, &T when borrowed.
Source§

fn into_arg(self) -> &'a Map<KU, VU>

Convert into a T or a &T.
Source§

fn into_owned(self) -> Map<KU, VU>

Convert into an owned T, cloning only a borrowed value.
Source§

impl<K, V, KU: TypeSubsumes<K>, VU: TypeSubsumes<V>> IntoArg<Map<KU, VU>> for Map<K, V>

Source§

type Output = Map<KU, VU>

T when owned, &T when borrowed.
Source§

fn into_arg(self) -> Map<KU, VU>

Convert into a T or a &T.
Source§

fn into_owned(self) -> Map<KU, VU>

Convert into an owned T, cloning only a borrowed value.
Source§

impl<K, V> IntoIterator for &Map<K, V>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: ContainerElement, V: ContainerElement> ObjectRefCore for Map<K, V>

Source§

type ContainerType = MapObj

The object type the reference points to.
Source§

fn data(this: &Self) -> &ObjectArc<MapObj>

Borrow the object’s handle.
Source§

fn into_data(this: Self) -> ObjectArc<MapObj>

Take the object’s handle.
Source§

unsafe fn from_data(data: ObjectArc<MapObj>) -> Self

Make a reference from the object’s handle. Read more
Source§

fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool

Whether two references point to the same object.
Source§

fn type_key(&self) -> &'static str

The registered type key of the object’s runtime type, such as "ffi.Array", as C++ ObjectRef::GetTypeKey.
Source§

fn is<T: AnyCompatible>(&self) -> bool

Whether the reference is a T as is, such as a subtype or a container whose elements are checked too, by the strict check of downcast, without a reference count change. See also crate::Any::is.
Source§

fn downcast_ref<T: DowncastRefFromObject>(&self) -> Option<&T>

Borrow the underlying object as T when it is one. Read more
Source§

fn downcast<B>(self) -> Result<B, Self>
where B: ObjectRefCore,

Take self as B, a subtype or a container such as Array<i64> whose elements are checked too, when its object is one, without copying it. On a mismatch the reference is returned unchanged. Read more
Source§

impl<'a, K: ContainerElement, V: ContainerElement> PackedCallArg for &'a Map<K, V>

Source§

type Holder = &'a Map<K, V>

What keeps the argument alive during the call.
Source§

fn into_packed_arg(self) -> &'a Map<K, V>

Source§

fn as_packed_arg(holder: &Self::Holder) -> AnyView<'_>

Source§

impl<K: ContainerElement, V: ContainerElement> PackedCallArg for Map<K, V>

Source§

type Holder = Map<K, V>

What keeps the argument alive during the call.
Source§

fn into_packed_arg(self) -> Map<K, V>

Source§

fn as_packed_arg(holder: &Self::Holder) -> AnyView<'_>

Source§

impl<'b, K, V> TryFrom<&'b Any> for Map<K, V>

Source§

type Error = Error

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

fn try_from(value: &'b Any) -> Result<Self>

Performs the conversion.
Source§

impl<K, V> TryFrom<&ObjectRef> for Map<K, V>
where Map<K, V>: ObjectRefCore + DowncastRefFromObject,

Source§

type Error = Error

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

fn try_from(value: &ObjectRef) -> Result<Self>

Performs the conversion.
Source§

impl<K, V> TryFrom<Any> for Map<K, V>

Source§

type Error = Error

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

fn try_from(value: Any) -> Result<Self>

Performs the conversion.
Source§

impl<'a, K, V> TryFrom<AnyView<'a>> for Map<K, V>

Source§

type Error = Error

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

fn try_from(value: AnyView<'a>) -> Result<Self>

Performs the conversion.
Source§

impl<K, V> TryFrom<ObjectRef> for Map<K, V>
where Map<K, V>: ObjectRefCore,

The strict downcast from the root ObjectRef.

Source§

type Error = Error

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

fn try_from(value: ObjectRef) -> Result<Self>

Performs the conversion.
Source§

impl<K: Send + Sync, V: Send + Sync> Send for Map<K, V>

Source§

impl<K: Send + Sync, V: Send + Sync> Sync for Map<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for Map<K, V>

§

impl<K, V> RefUnwindSafe for Map<K, V>

§

impl<K, V> Unpin for Map<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnsafeUnpin for Map<K, V>

§

impl<K, V> UnwindSafe for Map<K, V>
where K: UnwindSafe, V: UnwindSafe,

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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> ContainerElement for T
where T: AnyCompatible,

Source§

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