Skip to content

Commit

Permalink
Merge pull request #80 from aakamenov/deque-set
Browse files Browse the repository at this point in the history
Deque: Add set() method
  • Loading branch information
uint authored Jul 4, 2024
2 parents e2db9c7 + 0851048 commit c1b1008
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ impl<T: Serialize + DeserializeOwned> Deque<T> {
.map(Some)
}

/// Sets the value at the given position in the queue. Returns [`StdError::NotFound`] if index is out of bounds
pub fn set(&self, storage: &mut dyn Storage, pos: u32, value: &T) -> StdResult<()> {
let head = self.head(storage)?;
let tail = self.tail(storage)?;

if pos >= calc_len(head, tail) {
// out of bounds
return Err(StdError::not_found(format!("deque position {}", pos)));
}

self.set_unchecked(storage, pos, value)
}

/// Tries to get the value at the given position
/// Used internally
fn get_unchecked(&self, storage: &dyn Storage, pos: u32) -> StdResult<Option<T>> {
Expand Down Expand Up @@ -676,4 +689,35 @@ mod tests {
"out of bounds access should return None"
);
}

#[test]
fn set() {
let mut store = MockStorage::new();
let deque = Deque::new("test");

deque.push_back(&mut store, &1u32).unwrap();
deque.push_back(&mut store, &2).unwrap();

deque.set(&mut store, 1, &3).unwrap();

assert_eq!(deque.get(&store, 1).unwrap(), Some(3));
assert_eq!(deque.back(&store).unwrap(), Some(3));
assert_eq!(
deque.get(&store, 2).unwrap(),
None,
"out of bounds access should return None"
);

assert!(
matches!(deque.set(&mut store, 2, &3), Err(StdError::NotFound { .. })),
"setting value at an out of bounds index should error"
);

assert_eq!(deque.pop_back(&mut store), Ok(Some(3)));

assert!(
matches!(deque.set(&mut store, 1, &3), Err(StdError::NotFound { .. })),
"setting value at an out of bounds index should error"
);
}
}

0 comments on commit c1b1008

Please # to comment.