pub struct ByteCollectionView<C, W> { /* private fields */ }Expand description
A view that supports accessing a collection of views of the same kind, indexed by a
Vec<u8>, one subview at a time.
Implementations§
Source§impl<W: View> ByteCollectionView<W::Context, W>
impl<W: View> ByteCollectionView<W::Context, W>
Sourcepub async fn load_entry_mut(
&mut self,
short_key: &[u8],
) -> Result<&mut W, ViewError>
pub async fn load_entry_mut( &mut self, short_key: &[u8], ) -> Result<&mut W, ViewError>
Loads a subview for the data at the given index in the collection. If an entry is absent then a default entry is added to the collection. The resulting view can be modified.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
let subview = view.load_entry_mut(&[0, 1]).await.unwrap();
let value = subview.get();
assert_eq!(*value, String::default());Sourcepub async fn try_load_entry(
&self,
short_key: &[u8],
) -> Result<Option<ReadGuardedView<'_, W>>, ViewError>
pub async fn try_load_entry( &self, short_key: &[u8], ) -> Result<Option<ReadGuardedView<'_, W>>, ViewError>
Loads a subview for the data at the given index in the collection. If an entry
is absent then None is returned. The resulting view cannot be modified.
May fail if one subview is already being visited.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
{
let _subview = view.load_entry_mut(&[0, 1]).await.unwrap();
}
{
let subview = view.try_load_entry(&[0, 1]).await.unwrap().unwrap();
let value = subview.get();
assert_eq!(*value, String::default());
}
assert!(view.try_load_entry(&[0, 2]).await.unwrap().is_none());Sourcepub async fn try_load_entries(
&self,
short_keys: Vec<Vec<u8>>,
) -> Result<Vec<Option<ReadGuardedView<'_, W>>>, ViewError>
pub async fn try_load_entries( &self, short_keys: Vec<Vec<u8>>, ) -> Result<Vec<Option<ReadGuardedView<'_, W>>>, ViewError>
Load multiple entries for reading at once.
The entries in short_keys have to be all distinct.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
{
let _subview = view.load_entry_mut(&[0, 1]).await.unwrap();
}
let short_keys = vec![vec![0, 1], vec![2, 3]];
let subviews = view.try_load_entries(short_keys).await.unwrap();
let value0 = subviews[0].as_ref().unwrap().get();
assert_eq!(*value0, String::default());Sourcepub async fn try_load_entries_pairs(
&self,
short_keys: Vec<Vec<u8>>,
) -> Result<Vec<(Vec<u8>, Option<ReadGuardedView<'_, W>>)>, ViewError>
pub async fn try_load_entries_pairs( &self, short_keys: Vec<Vec<u8>>, ) -> Result<Vec<(Vec<u8>, Option<ReadGuardedView<'_, W>>)>, ViewError>
Loads multiple entries for reading at once with their keys.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
{
let subview = view.load_entry_mut(&vec![0, 1]).await.unwrap();
subview.set("Bonjour".into());
}
let short_keys = vec![vec![0, 1], vec![0, 2]];
let pairs = view.try_load_entries_pairs(short_keys).await.unwrap();
assert_eq!(pairs[0].0, vec![0, 1]);
assert_eq!(pairs[1].0, vec![0, 2]);
let value0 = pairs[0].1.as_ref().unwrap().get();
assert_eq!(*value0, "Bonjour".to_string());
assert!(pairs[1].1.is_none());Sourcepub async fn try_load_all_entries(
&self,
) -> Result<Vec<(Vec<u8>, ReadGuardedView<'_, W>)>, ViewError>
pub async fn try_load_all_entries( &self, ) -> Result<Vec<(Vec<u8>, ReadGuardedView<'_, W>)>, ViewError>
Load all entries for reading at once.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
{
let _subview = view.load_entry_mut(&[0, 1]).await.unwrap();
}
let subviews = view.try_load_all_entries().await.unwrap();
assert_eq!(subviews.len(), 1);Sourcepub fn reset_entry_to_default(
&mut self,
short_key: &[u8],
) -> Result<(), ViewError>
pub fn reset_entry_to_default( &mut self, short_key: &[u8], ) -> Result<(), ViewError>
Resets an entry to the default value.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
let subview = view.load_entry_mut(&[0, 1]).await.unwrap();
let value = subview.get_mut();
*value = String::from("Hello");
view.reset_entry_to_default(&[0, 1]).unwrap();
let subview = view.load_entry_mut(&[0, 1]).await.unwrap();
let value = subview.get_mut();
assert_eq!(*value, String::default());Sourcepub async fn contains_key(&self, short_key: &[u8]) -> Result<bool, ViewError>
pub async fn contains_key(&self, short_key: &[u8]) -> Result<bool, ViewError>
Tests if the collection contains a specified key and returns a boolean.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
{
let _subview = view.load_entry_mut(&[0, 1]).await.unwrap();
}
assert!(view.contains_key(&[0, 1]).await.unwrap());
assert!(!view.contains_key(&[0, 2]).await.unwrap());Sourcepub fn remove_entry(&mut self, short_key: Vec<u8>)
pub fn remove_entry(&mut self, short_key: Vec<u8>)
Marks the entry as removed. If absent then nothing is done.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
let subview = view.load_entry_mut(&[0, 1]).await.unwrap();
let value = subview.get_mut();
assert_eq!(*value, String::default());
view.remove_entry(vec![0, 1]);
let keys = view.keys().await.unwrap();
assert_eq!(keys.len(), 0);Source§impl<W: View> ByteCollectionView<W::Context, W>
impl<W: View> ByteCollectionView<W::Context, W>
Sourcepub async fn for_each_key_while<F>(&self, f: F) -> Result<(), ViewError>
pub async fn for_each_key_while<F>(&self, f: F) -> Result<(), ViewError>
Applies a function f on each index (aka key). Keys are visited in the lexicographic order. If the function returns false, then the loop ends prematurely.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
view.load_entry_mut(&[0, 1]).await.unwrap();
view.load_entry_mut(&[0, 2]).await.unwrap();
let mut count = 0;
view.for_each_key_while(|_key| {
count += 1;
Ok(count < 1)
})
.await
.unwrap();
assert_eq!(count, 1);Sourcepub async fn for_each_key<F>(&self, f: F) -> Result<(), ViewError>
pub async fn for_each_key<F>(&self, f: F) -> Result<(), ViewError>
Applies a function f on each index (aka key). Keys are visited in a lexicographic order.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
view.load_entry_mut(&[0, 1]).await.unwrap();
view.load_entry_mut(&[0, 2]).await.unwrap();
let mut count = 0;
view.for_each_key(|_key| {
count += 1;
Ok(())
})
.await
.unwrap();
assert_eq!(count, 2);Sourcepub async fn keys(&self) -> Result<Vec<Vec<u8>>, ViewError>
pub async fn keys(&self) -> Result<Vec<Vec<u8>>, ViewError>
Returns the list of keys in the collection. The order is lexicographic.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
view.load_entry_mut(&[0, 1]).await.unwrap();
view.load_entry_mut(&[0, 2]).await.unwrap();
let keys = view.keys().await.unwrap();
assert_eq!(keys, vec![vec![0, 1], vec![0, 2]]);Sourcepub async fn count(&self) -> Result<usize, ViewError>
pub async fn count(&self) -> Result<usize, ViewError>
Returns the number of entries in the collection.
let mut view: ByteCollectionView<_, RegisterView<_, String>> =
ByteCollectionView::load(context).await.unwrap();
view.load_entry_mut(&[0, 1]).await.unwrap();
view.load_entry_mut(&[0, 2]).await.unwrap();
assert_eq!(view.count().await.unwrap(), 2);Trait Implementations§
Source§impl<C, W: Allocative> Allocative for ByteCollectionView<C, W>
impl<C, W: Allocative> Allocative for ByteCollectionView<C, W>
Source§impl<W: ClonableView> ClonableView for ByteCollectionView<W::Context, W>
impl<W: ClonableView> ClonableView for ByteCollectionView<W::Context, W>
Source§fn clone_unchecked(&mut self) -> Result<Self, ViewError>
fn clone_unchecked(&mut self) -> Result<Self, ViewError>
Source§impl<W: HashableView> HashableView for ByteCollectionView<W::Context, W>
impl<W: HashableView> HashableView for ByteCollectionView<W::Context, W>
Source§impl<W: View> View for ByteCollectionView<W::Context, W>
impl<W: View> View for ByteCollectionView<W::Context, W>
Source§const NUM_INIT_KEYS: usize = 0usize
const NUM_INIT_KEYS: usize = 0usize
Source§fn pre_load(_context: &Self::Context) -> Result<Vec<Vec<u8>>, ViewError>
fn pre_load(_context: &Self::Context) -> Result<Vec<Vec<u8>>, ViewError>
Source§fn post_load(
context: Self::Context,
_values: &[Option<Vec<u8>>],
) -> Result<Self, ViewError>
fn post_load( context: Self::Context, _values: &[Option<Vec<u8>>], ) -> Result<Self, ViewError>
Source§fn rollback(&mut self)
fn rollback(&mut self)
flush should have no effect to storage.Source§async fn has_pending_changes(&self) -> bool
async fn has_pending_changes(&self) -> bool
true if flushing this view would result in changes to the persistent storage.Source§fn pre_save(&self, batch: &mut Batch) -> Result<bool, ViewError>
fn pre_save(&self, batch: &mut Batch) -> Result<bool, ViewError>
batch variable.
The returned boolean indicates whether the operation removes the view or not.Source§fn post_save(&mut self)
fn post_save(&mut self)
pre_save and after the batch has been successfully written to storage.
This leaves the view in a clean state with no pending changes. Read moreSource§fn clear(&mut self)
fn clear(&mut self)
Auto Trait Implementations§
impl<C, W> !Freeze for ByteCollectionView<C, W>
impl<C, W> !RefUnwindSafe for ByteCollectionView<C, W>
impl<C, W> Send for ByteCollectionView<C, W>
impl<C, W> Sync for ByteCollectionView<C, W>
impl<C, W> Unpin for ByteCollectionView<C, W>where
C: Unpin,
impl<C, W> UnwindSafe for ByteCollectionView<C, W>where
C: UnwindSafe,
W: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§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
Source§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.