Skip to content

Commit

Permalink
[storage] Add mutable indexed access to StorageVec #167 (#168)
Browse files Browse the repository at this point in the history
* Update storage_vec.rs

* Update storage_vec.rs
  • Loading branch information
michaelvlach authored Sep 8, 2022
1 parent af09ea2 commit 962f7e8
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/storage/storage_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ impl<T: Serialize, S: Storage> StorageVec<T, S> {
Ok(())
}

pub(crate) fn set_value(&mut self, index: u64, value: &T) -> Result<(), DbError> {
if self.size <= index {
return Err(DbError::Storage("index out of bounds".to_string()));
}

self.storage
.borrow_mut()
.insert_at(self.index, Self::value_offset(index), value)
}

pub(crate) fn value(&mut self, index: u64) -> Result<T, DbError> {
if self.size <= index {
return Err(DbError::Storage("index out of bounds".to_string()));
Expand Down Expand Up @@ -92,6 +102,40 @@ mod tests {
);
}

#[test]
fn set_value() {
let test_file = TestFile::from("./storage_vec-set_value.agdb");
let storage = std::rc::Rc::new(std::cell::RefCell::new(
FileStorage::try_from(test_file.file_name().clone()).unwrap(),
));

let mut vec = StorageVec::<i64>::try_from(storage).unwrap();
vec.push(&1).unwrap();
vec.push(&3).unwrap();
vec.push(&5).unwrap();

vec.set_value(1, &10).unwrap();

assert_eq!(vec.value(0), Ok(1));
assert_eq!(vec.value(1), Ok(10));
assert_eq!(vec.value(2), Ok(5));
}

#[test]
fn set_value_out_of_bounds() {
let test_file = TestFile::from("./storage_vec-set_value_out_of_bounds.agdb");
let storage = std::rc::Rc::new(std::cell::RefCell::new(
FileStorage::try_from(test_file.file_name().clone()).unwrap(),
));

let mut vec = StorageVec::<i64>::try_from(storage).unwrap();

assert_eq!(
vec.set_value(0, &10),
Err(DbError::Storage("index out of bounds".to_string()))
);
}

#[test]
fn value() {
let test_file = TestFile::from("./storage_vec-value.agdb");
Expand Down

0 comments on commit 962f7e8

Please sign in to comment.