Skip to content

Commit 9144399

Browse files
authored
Merge pull request #14 from Sajjon/fix_bug_dont_delete_element_if-try_update_with_fails
Fix bug, dont delete element of try_update_with fails.
2 parents a6299e5 + a4379bb commit 9144399

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

src/vec/identified_vec.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,16 +490,22 @@ where
490490
#[inline]
491491
fn try_update_with<F, Er>(&mut self, id: &I, mut mutate: F) -> Result<bool, Er>
492492
where
493-
F: FnMut(E) -> Result<E, Er>,
493+
F: FnMut(&mut E) -> Result<E, Er>,
494494
{
495495
if !self.contains_id(id) {
496496
return Ok(false);
497497
}
498498
let mut existing = self.elements.remove(id).expect("Element for existing id");
499-
mutate(existing).map(|updated| {
500-
self.elements.insert(id.clone(), updated);
501-
true
502-
})
499+
500+
mutate(&mut existing)
501+
.map(|updated| {
502+
self.elements.insert(id.clone(), updated);
503+
true
504+
})
505+
.map_err(|e| {
506+
self.elements.insert(id.clone(), existing);
507+
e
508+
})
503509
}
504510

505511
/// Try to update the given element to the `identified_vec` if a element with the same ID is already present.

src/vec/is_identified_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ where
147147

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

152152
/// Insert a new member to this identified_vec at the specified index, if the identified_vec doesn't already contain
153153
/// it.

src/vec_of/is_identified_vec_of_via.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ where
186186
mut mutate: F,
187187
) -> Result<bool, Er>
188188
where
189-
F: FnMut(Element) -> Result<Element, Er>,
189+
F: FnMut(&mut Element) -> Result<Element, Er>,
190190
{
191191
self.via_mut().try_update_with(id, mutate)
192192
}

tests/tests.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,8 @@ fn try_update_with_contains() {
410410
sut.append(User::new(2, "Blob, Jr."));
411411
assert_eq!(
412412
sut.try_update_with(&2, |x| {
413-
let mut y = x;
414-
*y.name.get_mut() = "Junior, Jr.".to_string();
415-
Result::<User, ()>::Ok(y)
413+
*x.name.get_mut() = "Junior, Jr.".to_string();
414+
Result::<User, ()>::Ok(x.clone())
416415
}),
417416
Ok(true)
418417
);
@@ -425,15 +424,43 @@ fn try_update_with_not_contains() {
425424
sut.append(User::new(2, "Blob, Jr."));
426425
assert_eq!(
427426
sut.try_update_with(&999, |x| {
428-
let mut y = x;
429-
*y.name.get_mut() = "Will never happen.".to_string();
430-
Result::<User, ()>::Ok(y)
427+
*x.name.get_mut() = "Will never happen.".to_string();
428+
Result::<User, ()>::Ok(x.clone())
431429
}),
432430
Ok(false)
433431
);
434432
assert_eq!(sut.items(), [User::new(2, "Blob, Jr.")]);
435433
}
436434

435+
#[test]
436+
fn try_update_with_failure_does_not_delete_element() {
437+
let mut sut = Users::new();
438+
sut.append(User::new(1, "Blob."));
439+
sut.append(User::new(2, "Blob, Jr."));
440+
sut.append(User::new(3, "Blob, Sr."));
441+
442+
assert_eq!(
443+
sut.items(),
444+
[
445+
User::new(1, "Blob."),
446+
User::new(2, "Blob, Jr."),
447+
User::new(3, "Blob, Sr.")
448+
]
449+
);
450+
451+
assert_eq!(sut.try_update_with(&2, |_| { Err(false) }), Err(false));
452+
453+
// remains unchanged
454+
assert_eq!(
455+
sut.items(),
456+
[
457+
User::new(1, "Blob."),
458+
User::new(2, "Blob, Jr."),
459+
User::new(3, "Blob, Sr.")
460+
]
461+
);
462+
}
463+
437464
#[test]
438465
#[should_panic(expected = "Expected element at index {index}")]
439466
fn update_at_expect_panic_unknown_index() {

0 commit comments

Comments
 (0)