Skip to content

Commit

Permalink
Set: add try_insert
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jun 12, 2024
1 parent af9ecc4 commit 450f57c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libspecr/src/map/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<K: Obj, V: Obj> Map<K, V> {

/// Like `insert`, but fails if `k` was already in the map.
pub fn try_insert(&mut self, k: K, v: V) -> Result<(), ()> {
if self.contains_key(k.clone()) {
if self.contains_key(k) {
return Err(());
}

Expand Down
13 changes: 13 additions & 0 deletions libspecr/src/set/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ impl<T: Obj> Set<T> {
});
}

/// Like `insert`, but fails if `t` was already in the set.
pub fn try_insert(&mut self, t: T) -> Result<(), ()> {
if self.contains(t) {
return Err(());
}

self.0.mutate(|s| {
s.insert(t);
});

Ok(())
}

/// Removes `t` from `self`.
pub fn remove(&mut self, t: T) {
self.0.mutate(|s| {
Expand Down

0 comments on commit 450f57c

Please sign in to comment.