Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/base_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::{
Memory, Storable,
};
use std::borrow::{Borrow, Cow};
use std::cell::Cell;
use std::cmp::min;
use std::fmt;
use std::marker::PhantomData;
Expand Down Expand Up @@ -95,6 +96,7 @@ impl std::error::Error for InitError {}

pub struct BaseVec<T: Storable, M: Memory> {
memory: M,
cached_len: Cell<u64>,
_marker: PhantomData<T>,
}

Expand All @@ -117,6 +119,7 @@ impl<T: Storable, M: Memory> BaseVec<T, M> {
Self::write_header(&header, &memory)?;
Ok(Self {
memory,
cached_len: Cell::new(0),
_marker: PhantomData,
})
}
Expand Down Expand Up @@ -148,6 +151,7 @@ impl<T: Storable, M: Memory> BaseVec<T, M> {

Ok(Self {
memory,
cached_len: Cell::new(header.len),
_marker: PhantomData,
})
}
Expand All @@ -168,7 +172,7 @@ impl<T: Storable, M: Memory> BaseVec<T, M> {
///
/// Complexity: O(1)
pub fn len(&self) -> u64 {
read_u64(&self.memory, Address::from(LEN_OFFSET))
self.cached_len.get()
}

/// Sets the item at the specified index to the specified value.
Expand Down Expand Up @@ -254,6 +258,7 @@ impl<T: Storable, M: Memory> BaseVec<T, M> {
/// Sets the vector's length.
fn set_len(&self, new_len: u64) {
write_u64(&self.memory, Address::from(LEN_OFFSET), new_len);
self.cached_len.set(new_len);
}

/// Writes the size of the item at the specified offset.
Expand Down
Loading