Skip to content

Commit

Permalink
fix!: rename Column::update to Column::set
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Nov 13, 2024
1 parent 3ea04ae commit ea76d6d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
10 changes: 5 additions & 5 deletions packages/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ where
Ok(id)
}

/// Update the value associated with the given ID.
/// Set the value associated with the given ID.
///
/// # Example
/// ```
Expand All @@ -380,10 +380,10 @@ where
/// access.push(&1337).unwrap();
/// assert_eq!(access.get(1).unwrap(), Some(1337));
///
/// access.update(1, &9001).unwrap();
/// access.set(1, &9001).unwrap();
/// assert_eq!(access.get(1).unwrap(), Some(9001));
/// ```
pub fn update(&mut self, id: u32, value: &T) -> Result<(), UpdateError<E::EncodeError>> {
pub fn set(&mut self, id: u32, value: &T) -> Result<(), UpdateError<E::EncodeError>> {
self.storage
.get(&encode_id(id))
.ok_or(UpdateError::NotFound)?;
Expand Down Expand Up @@ -497,8 +497,8 @@ mod tests {
assert_eq!(access.len().unwrap(), 2);

access.remove(1).unwrap();
assert_eq!(access.update(1, &9001), Err(UpdateError::NotFound));
access.update(2, &9001).unwrap();
assert_eq!(access.set(1, &9001), Err(UpdateError::NotFound));
access.set(2, &9001).unwrap();

assert_eq!(access.get(1).unwrap(), None);
assert_eq!(access.get(2).unwrap(), Some(9001));
Expand Down
58 changes: 47 additions & 11 deletions packages/storey/src/containers/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,37 @@ where
Ok(())
}

pub fn update<F>(&mut self, f: F) -> Result<(), UpdateError<E>>
/// Update the value of the item.
///
/// The function `f` is called with the current value of the item, if it exists.
/// If the function returns `Some`, the item is set to the new value.
/// If the function returns `None`, the item is removed.
///
/// # Example
/// ```
/// # use mocks::encoding::TestEncoding;
/// # use mocks::backend::TestStorage;
/// use storey::containers::Item;
///
/// let mut storage = TestStorage::new();
/// let item = Item::<u64, TestEncoding>::new(0);
///
/// item.access(&mut storage).set(&42).unwrap();
/// item.access(&mut storage).update(|value| value.map(|v| v + 1)).unwrap();
/// assert_eq!(item.access(&storage).get().unwrap(), Some(43));
/// ```
pub fn update<F>(&mut self, f: F) -> Result<(), UpdateError<E::DecodeError, E::EncodeError>>
where
F: FnOnce(Option<T>) -> T,
F: FnOnce(Option<T>) -> Option<T>,
{
let new_value = f(self.get().map_err(UpdateError::Decode)?);
self.set(&new_value).map_err(UpdateError::Encode)
match new_value {
Some(value) => self.set(&value).map_err(UpdateError::Encode),
None => {
self.remove();
Ok(())
}
}
}

/// Remove the value of the item.
Expand All @@ -263,16 +288,11 @@ where
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, thiserror::Error)]
pub enum UpdateError<E>
where
E: Encoding,
E::DecodeError: std::fmt::Display,
E::EncodeError: std::fmt::Display,
{
pub enum UpdateError<D, E> {
#[error("decode error: {0}")]
Decode(E::DecodeError),
Decode(D),
#[error("encode error: {0}")]
Encode(E::EncodeError),
Encode(E),
}

#[cfg(test)]
Expand All @@ -297,4 +317,20 @@ mod tests {
assert_eq!(access1.get().unwrap(), None);
assert_eq!(storage.get(&[1]), None);
}

#[test]
fn update() {
let mut storage = TestStorage::new();

let item = Item::<u64, TestEncoding>::new(0);
item.access(&mut storage).set(&42).unwrap();

item.access(&mut storage)
.update(|value| value.map(|v| v + 1))
.unwrap();
assert_eq!(item.access(&storage).get().unwrap(), Some(43));

item.access(&mut storage).update(|_| None).unwrap();
assert_eq!(item.access(&storage).get().unwrap(), None);
}
}

0 comments on commit ea76d6d

Please sign in to comment.