summaryrefslogtreecommitdiff
path: root/learn_4/src/main.rs
diff options
context:
space:
mode:
authorBrad Clawsie <brad@b7j0c.org>2026-11-13 14:28:46 -0600
committerBrad Clawsie <brad@b7j0c.org>2026-11-13 14:28:46 -0600
commite44626f1b0bdc26d888408789eefb2b3603c163f (patch)
tree709f81440d16908668fbd97641b59d6df0a6f962 /learn_4/src/main.rs
parent96e9806883d34fe60c433e84f201a751dee40a7a (diff)
fix: handle zero capacity in push
Diffstat (limited to 'learn_4/src/main.rs')
-rw-r--r--learn_4/src/main.rs8
1 files changed, 8 insertions, 0 deletions
diff --git a/learn_4/src/main.rs b/learn_4/src/main.rs
index 4361b52..47408ef 100644
--- a/learn_4/src/main.rs
+++ b/learn_4/src/main.rs
@@ -12,6 +12,9 @@ impl<T> History<T> {
}
pub fn push(&mut self, value: T) {
+ if self.capacity == 0 {
+ return;
+ }
if self.entries.len() == self.capacity {
self.entries.remove(0);
}
@@ -63,6 +66,11 @@ mod tests {
history.push(33);
assert_eq!(history.entries.len(), 2);
assert_eq!(history.entries.as_slice(), &[22, 33]);
+
+ let mut history0 = History::<u32>::new(0);
+ history0.push(11);
+ assert_eq!(history0.entries.len(), 0);
+ assert!(history0.pop().is_none());
}
#[test]