Skip to content

Commit 047bb88

Browse files
Apply suggested fix to src/store/weight.rs from Copilot Autofix
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
1 parent 6216bf8 commit 047bb88

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/store/weight.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,16 @@ where
501501
pub fn try_insert(&mut self, key: K, value: Arc<V>) -> Result<Option<Arc<V>>, StoreFull> {
502502
let new_weight = self.compute_weight(value.as_ref());
503503
if let Some(entry) = self.map.get_mut(&key) {
504-
let next_total = self.total_weight - entry.weight + new_weight;
504+
debug_assert!(
505+
self.total_weight >= entry.weight,
506+
"WeightStore invariant violated: total_weight ({}) is less than entry.weight ({})",
507+
self.total_weight,
508+
entry.weight
509+
);
510+
let next_total = self
511+
.total_weight
512+
.saturating_sub(entry.weight)
513+
.saturating_add(new_weight);
505514
if next_total > self.capacity_weight {
506515
return Err(StoreFull);
507516
}

0 commit comments

Comments
 (0)