Struct linera_views::views::map_view::MapView
source · pub struct MapView<C, I, V> { /* private fields */ }
Expand description
A View
that has a type for keys. The ordering of the entries
is determined by the serialization of the context.
Implementations§
source§impl<C, I, V> MapView<C, I, V>
impl<C, I, V> MapView<C, I, V>
sourcepub fn insert<Q>(&mut self, index: &Q, value: V) -> Result<(), ViewError>
pub fn insert<Q>(&mut self, index: &Q, value: V) -> Result<(), ViewError>
Inserts or resets a value at an index.
let mut map: MapView<_, u32, _> = MapView::load(context).await.unwrap();
map.insert(&(24 as u32), String::from("Hello"));
assert_eq!(
map.get(&(24 as u32)).await.unwrap(),
Some(String::from("Hello"))
);
sourcepub fn remove<Q>(&mut self, index: &Q) -> Result<(), ViewError>
pub fn remove<Q>(&mut self, index: &Q) -> Result<(), ViewError>
Removes a value. If absent then the operation does nothing.
let mut map = MapView::<_, u32, String>::load(context).await.unwrap();
map.remove(&(37 as u32));
assert_eq!(map.get(&(37 as u32)).await.unwrap(), None);
sourcepub async fn contains_key<Q>(&self, index: &Q) -> Result<bool, ViewError>
pub async fn contains_key<Q>(&self, index: &Q) -> Result<bool, ViewError>
Returns true
if the map contains a value for the specified key.
let mut map = MapView::<_, u32, String>::load(context).await.unwrap();
map.insert(&(37 as u32), String::from("Hello"));
assert!(map.contains_key(&(37 as u32)).await.unwrap());
assert!(!map.contains_key(&(34 as u32)).await.unwrap());
source§impl<C, I, V> MapView<C, I, V>
impl<C, I, V> MapView<C, I, V>
sourcepub async fn get<Q>(&self, index: &Q) -> Result<Option<V>, ViewError>
pub async fn get<Q>(&self, index: &Q) -> Result<Option<V>, ViewError>
Reads the value at the given position, if any.
let mut map: MapView<_, u32, _> = MapView::load(context).await.unwrap();
map.insert(&(37 as u32), String::from("Hello"));
assert_eq!(
map.get(&(37 as u32)).await.unwrap(),
Some(String::from("Hello"))
);
assert_eq!(map.get(&(34 as u32)).await.unwrap(), None);
sourcepub async fn get_mut<Q>(
&mut self,
index: &Q,
) -> Result<Option<&mut V>, ViewError>
pub async fn get_mut<Q>( &mut self, index: &Q, ) -> Result<Option<&mut V>, ViewError>
Obtains a mutable reference to a value at a given position if available
let mut map: MapView<_, u32, String> = MapView::load(context).await.unwrap();
map.insert(&(37 as u32), String::from("Hello"));
assert_eq!(map.get_mut(&(34 as u32)).await.unwrap(), None);
let value = map.get_mut(&(37 as u32)).await.unwrap().unwrap();
*value = String::from("Hola");
assert_eq!(
map.get(&(37 as u32)).await.unwrap(),
Some(String::from("Hola"))
);
source§impl<C, I, V> MapView<C, I, V>
impl<C, I, V> MapView<C, I, V>
sourcepub async fn indices(&self) -> Result<Vec<I>, ViewError>
pub async fn indices(&self) -> Result<Vec<I>, ViewError>
Returns the list of indices in the map. The order is determined by serialization.
let mut map: MapView<_, u32, String> = MapView::load(context).await.unwrap();
map.insert(&(37 as u32), String::from("Hello"));
assert_eq!(map.indices().await.unwrap(), vec![37 as u32]);
sourcepub async fn for_each_index_while<F>(&self, f: F) -> Result<(), ViewError>
pub async fn for_each_index_while<F>(&self, f: F) -> Result<(), ViewError>
Applies a function f on each index. Indices are visited in an order determined by the serialization. If the function returns false, then the loop ends prematurely.
let mut map: MapView<_, u128, String> = MapView::load(context).await.unwrap();
map.insert(&(34 as u128), String::from("Thanks"));
map.insert(&(37 as u128), String::from("Spasiba"));
map.insert(&(38 as u128), String::from("Merci"));
let mut count = 0;
map.for_each_index_while(|_index| {
count += 1;
Ok(count < 2)
})
.await
.unwrap();
assert_eq!(count, 2);
sourcepub async fn for_each_index<F>(&self, f: F) -> Result<(), ViewError>
pub async fn for_each_index<F>(&self, f: F) -> Result<(), ViewError>
Applies a function f on each index. Indices are visited in the order determined by serialization.
let mut map: MapView<_, u128, String> = MapView::load(context).await.unwrap();
map.insert(&(34 as u128), String::from("Hello"));
let mut count = 0;
map.for_each_index(|_index| {
count += 1;
Ok(())
})
.await
.unwrap();
assert_eq!(count, 1);
sourcepub async fn for_each_index_value_while<'a, F>(
&'a self,
f: F,
) -> Result<(), ViewError>
pub async fn for_each_index_value_while<'a, F>( &'a self, f: F, ) -> Result<(), ViewError>
Applies a function f on each index/value pair. Indices and values are visited in an order determined by serialization. If the function returns false, then the loop ends prematurely.
let mut map: MapView<_, u128, String> = MapView::load(context).await.unwrap();
map.insert(&(34 as u128), String::from("Thanks"));
map.insert(&(37 as u128), String::from("Spasiba"));
map.insert(&(38 as u128), String::from("Merci"));
let mut values = Vec::new();
map.for_each_index_value_while(|_index, value| {
values.push(value);
Ok(values.len() < 2)
})
.await
.unwrap();
assert_eq!(values.len(), 2);
sourcepub async fn for_each_index_value<'a, F>(
&'a self,
f: F,
) -> Result<(), ViewError>
pub async fn for_each_index_value<'a, F>( &'a self, f: F, ) -> Result<(), ViewError>
Applies a function on each index/value pair. Indices and values are visited in an order determined by serialization.
let mut map: MapView<_, Vec<u8>, _> = MapView::load(context).await.unwrap();
map.insert(&vec![0, 1], String::from("Hello"));
let mut count = 0;
map.for_each_index_value(|_index, _value| {
count += 1;
Ok(())
})
.await
.unwrap();
assert_eq!(count, 1);
source§impl<C, I, V> MapView<C, I, V>
impl<C, I, V> MapView<C, I, V>
sourcepub async fn index_values(&self) -> Result<Vec<(I, V)>, ViewError>
pub async fn index_values(&self) -> Result<Vec<(I, V)>, ViewError>
Obtains all the (index,value)
pairs.
let mut map: MapView<_, String, _> = MapView::load(context).await.unwrap();
map.insert("Italian", String::from("Ciao"));
let index_values = map.index_values().await.unwrap();
assert_eq!(
index_values,
vec![("Italian".to_string(), "Ciao".to_string())]
);
sourcepub async fn count(&self) -> Result<usize, ViewError>
pub async fn count(&self) -> Result<usize, ViewError>
Obtains the number of entries in the map
let mut map: MapView<_, String, _> = MapView::load(context).await.unwrap();
map.insert("Italian", String::from("Ciao"));
map.insert("French", String::from("Bonjour"));
assert_eq!(map.count().await.unwrap(), 2);
source§impl<C, I, V> MapView<C, I, V>
impl<C, I, V> MapView<C, I, V>
sourcepub async fn get_mut_or_default<Q>(
&mut self,
index: &Q,
) -> Result<&mut V, ViewError>
pub async fn get_mut_or_default<Q>( &mut self, index: &Q, ) -> Result<&mut V, ViewError>
Obtains a mutable reference to a value at a given position. Default value if the index is missing.
let mut map: MapView<_, u32, u128> = MapView::load(context).await.unwrap();
let value = map.get_mut_or_default(&(34 as u32)).await.unwrap();
assert_eq!(*value, 0 as u128);
Trait Implementations§
source§impl<C, I, V> ClonableView<C> for MapView<C, I, V>
impl<C, I, V> ClonableView<C> for MapView<C, I, V>
source§fn clone_unchecked(&mut self) -> Result<Self, ViewError>
fn clone_unchecked(&mut self) -> Result<Self, ViewError>
source§impl<C, I, V> ContainerType for MapView<C, I, V>where
C: Context + Send + Sync,
I: OutputType + InputType + Serialize + DeserializeOwned + Debug + Clone + Send + Sync + 'static,
V: OutputType + Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
impl<C, I, V> ContainerType for MapView<C, I, V>where
C: Context + Send + Sync,
I: OutputType + InputType + Serialize + DeserializeOwned + Debug + Clone + Send + Sync + 'static,
V: OutputType + Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
source§async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>>
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>>
async_graphql::Value
. Read moresource§async fn find_entity(
&self,
ctx: &Context<'_>,
params: &Value,
) -> ServerResult<Option<Value>>
async fn find_entity( &self, ctx: &Context<'_>, params: &Value, ) -> ServerResult<Option<Value>>
source§fn collect_all_fields<'a>(
&'a self,
ctx: &ContextBase<'a, &'a Positioned<SelectionSet>>,
fields: &mut Fields<'a>,
) -> Result<(), ServerError>
fn collect_all_fields<'a>( &'a self, ctx: &ContextBase<'a, &'a Positioned<SelectionSet>>, fields: &mut Fields<'a>, ) -> Result<(), ServerError>
source§impl<C, I, V> HashableView<C> for MapView<C, I, V>
impl<C, I, V> HashableView<C> for MapView<C, I, V>
§type Hasher = CoreWrapper<Sha3_256Core>
type Hasher = CoreWrapper<Sha3_256Core>
source§impl<C, I, V> OutputType for MapView<C, I, V>where
C: Context + Send + Sync,
I: OutputType + InputType + Serialize + DeserializeOwned + Debug + Clone + Send + Sync + 'static,
V: OutputType + Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
impl<C, I, V> OutputType for MapView<C, I, V>where
C: Context + Send + Sync,
I: OutputType + InputType + Serialize + DeserializeOwned + Debug + Clone + Send + Sync + 'static,
V: OutputType + Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
source§fn create_type_info(registry: &mut Registry) -> String
fn create_type_info(registry: &mut Registry) -> String
source§async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
_field: &Positioned<Field>,
) -> ServerResult<Value>
async fn resolve( &self, ctx: &ContextSelectionSet<'_>, _field: &Positioned<Field>, ) -> ServerResult<Value>
async_graphql::Value
.source§fn qualified_type_name() -> String
fn qualified_type_name() -> String
source§impl<C: Send + Sync, I: OutputType, V: OutputType> TypeName for MapView<C, I, V>
impl<C: Send + Sync, I: OutputType, V: OutputType> TypeName for MapView<C, I, V>
source§impl<C, I, V> View<C> for MapView<C, I, V>
impl<C, I, V> View<C> for MapView<C, I, V>
source§const NUM_INIT_KEYS: usize = 0usize
const NUM_INIT_KEYS: usize = 0usize
source§fn pre_load(context: &C) -> Result<Vec<Vec<u8>>, ViewError>
fn pre_load(context: &C) -> Result<Vec<Vec<u8>>, ViewError>
source§fn post_load(context: C, values: &[Option<Vec<u8>>]) -> Result<Self, ViewError>
fn post_load(context: C, values: &[Option<Vec<u8>>]) -> Result<Self, ViewError>
source§fn load<'async_trait>(
context: C,
) -> Pin<Box<dyn Future<Output = Result<Self, ViewError>> + Send + 'async_trait>>where
Self: 'async_trait,
fn load<'async_trait>(
context: C,
) -> Pin<Box<dyn Future<Output = Result<Self, ViewError>> + Send + 'async_trait>>where
Self: 'async_trait,
source§fn rollback(&mut self)
fn rollback(&mut self)
flush
should have no effect to storage.source§fn has_pending_changes<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn has_pending_changes<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
true
if flushing this view would result in changes to the persistent storage.source§fn flush(&mut self, batch: &mut Batch) -> Result<bool, ViewError>
fn flush(&mut self, batch: &mut Batch) -> Result<bool, ViewError>
batch
variable first. If the view is dropped without calling flush
, staged
changes are simply lost.
The returned boolean indicates whether the operation removes the view or not.impl<C, I, V> ObjectType for MapView<C, I, V>where
C: Context + Send + Sync,
I: OutputType + InputType + Serialize + DeserializeOwned + Debug + Clone + Send + Sync + 'static,
V: OutputType + Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
Auto Trait Implementations§
impl<C, I, V> Freeze for MapView<C, I, V>where
C: Freeze,
impl<C, I, V> RefUnwindSafe for MapView<C, I, V>
impl<C, I, V> Send for MapView<C, I, V>
impl<C, I, V> Sync for MapView<C, I, V>
impl<C, I, V> Unpin for MapView<C, I, V>
impl<C, I, V> UnwindSafe for MapView<C, I, V>
Blanket Implementations§
source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
source§impl<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
source§impl<T> MockResults for T
impl<T> MockResults for T
§type Results = T
type Results = T
MockInstance
.source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<M, I> RuntimeMemory<&mut I> for Mwhere
M: RuntimeMemory<I>,
impl<M, I> RuntimeMemory<&mut I> for Mwhere
M: RuntimeMemory<I>,
source§fn read<'instance>(
&self,
instance: &'instance &mut I,
location: GuestPointer,
length: u32,
) -> Result<Cow<'instance, [u8]>, RuntimeError>
fn read<'instance>( &self, instance: &'instance &mut I, location: GuestPointer, length: u32, ) -> Result<Cow<'instance, [u8]>, RuntimeError>
Reads length
bytes from memory from the provided location
.
source§fn write(
&mut self,
instance: &mut &mut I,
location: GuestPointer,
bytes: &[u8],
) -> Result<(), RuntimeError>
fn write( &mut self, instance: &mut &mut I, location: GuestPointer, bytes: &[u8], ) -> Result<(), RuntimeError>
Writes the bytes
to memory at the provided location
.