Skip to content

Commit

Permalink
Merge pull request #13 from Sajjon/try_update_with
Browse files Browse the repository at this point in the history
Try update with
  • Loading branch information
Sajjon authored Dec 21, 2023
2 parents a4cce78 + dd1515a commit a6299e5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "identified_vec"
version = "0.1.9"
version = "0.1.10"
edition = "2021"
authors = ["Alexander Cyon <[email protected]>"]
description = "Like HashSet but retaining INSERTION order and without `Hash` requirement on the Element type."
Expand Down
16 changes: 16 additions & 0 deletions src/vec/identified_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,22 @@ where
true
}

#[allow(unused_mut)]
#[inline]
fn try_update_with<F, Er>(&mut self, id: &I, mut mutate: F) -> Result<bool, Er>
where
F: FnMut(E) -> Result<E, Er>,
{
if !self.contains_id(id) {
return Ok(false);
}
let mut existing = self.elements.remove(id).expect("Element for existing id");
mutate(existing).map(|updated| {
self.elements.insert(id.clone(), updated);
true
})
}

/// Try to update the given element to the `identified_vec` if a element with the same ID is already present.
///
/// - Parameter item: The value to append or replace.
Expand Down
4 changes: 4 additions & 0 deletions src/vec/is_identified_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ where
where
F: FnMut(&mut Element);

fn try_update_with<F, Er>(&mut self, id: &ID, mutate: F) -> Result<bool, Er>
where
F: FnMut(Element) -> Result<Element, Er>;

/// Insert a new member to this identified_vec at the specified index, if the identified_vec doesn't already contain
/// it.
///
Expand Down
13 changes: 13 additions & 0 deletions src/vec_of/is_identified_vec_of_via.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ where
self.via_mut().update_with(id, mutate)
}

#[allow(unused_mut)]
#[inline]
fn try_update_with<F, Er>(
&mut self,
id: &<Element as Identifiable>::ID,
mut mutate: F,
) -> Result<bool, Er>
where
F: FnMut(Element) -> Result<Element, Er>,
{
self.via_mut().try_update_with(id, mutate)
}

#[inline]
fn try_append_new(&mut self, element: Element) -> Result<(bool, usize), Error> {
self.via_mut().try_append_new(element)
Expand Down
30 changes: 30 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,36 @@ fn update_with() {
assert_eq!(sut.update_with(&999, |_| panic!("not called")), false);
}

#[test]
fn try_update_with_contains() {
let mut sut = Users::new();
sut.append(User::new(2, "Blob, Jr."));
assert_eq!(
sut.try_update_with(&2, |x| {
let mut y = x;
*y.name.get_mut() = "Junior, Jr.".to_string();
Result::<User, ()>::Ok(y)
}),
Ok(true)
);
assert_eq!(sut.items(), [User::new(2, "Junior, Jr.")]);
}

#[test]
fn try_update_with_not_contains() {
let mut sut = Users::new();
sut.append(User::new(2, "Blob, Jr."));
assert_eq!(
sut.try_update_with(&999, |x| {
let mut y = x;
*y.name.get_mut() = "Will never happen.".to_string();
Result::<User, ()>::Ok(y)
}),
Ok(false)
);
assert_eq!(sut.items(), [User::new(2, "Blob, Jr.")]);
}

#[test]
#[should_panic(expected = "Expected element at index {index}")]
fn update_at_expect_panic_unknown_index() {
Expand Down

0 comments on commit a6299e5

Please sign in to comment.