From b997afe89ae2ca72ee3393ff71824b057304d25c Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Tue, 6 Jul 2021 12:36:38 +0530 Subject: [PATCH] Fix push function in `Array` --- server/src/coredb/array.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/server/src/coredb/array.rs b/server/src/coredb/array.rs index e8557e08..9f220a25 100644 --- a/server/src/coredb/array.rs +++ b/server/src/coredb/array.rs @@ -115,8 +115,8 @@ impl Array { ptr::write(self.as_mut_ptr().add(len), element); self.set_len(len + 1); } - pub fn push(&mut self, element: T) -> Result<(), ()> { - if self.capacity() < self.len() { + pub fn push_panic(&mut self, element: T) -> Result<(), ()> { + if self.len() < N { // so we can push it in unsafe { self.push_unchecked(element) }; Ok(()) @@ -124,8 +124,8 @@ impl Array { Err(()) } } - pub fn push_panic(&mut self, element: T) { - self.push(element).unwrap(); + pub fn push(&mut self, element: T) { + self.push_panic(element).unwrap(); } pub fn pop(&mut self) -> Option { if self.len() == 0 { @@ -447,3 +447,10 @@ fn test_basic() { Array::from([b'H', b'e', b'l', b'l', b'o', b' ', b'W', b'o', b'r', b'l', b'd']) ); } + +#[test] +fn test_uninitialized() { + let mut b: Array = Array::new(); + b.push(b'S'); + assert_eq!(b.iter().count(), 1); +}