Skip to content

Commit c82478f

Browse files
authored
Merge pull request #229 from japaric/symmetric_vec_interface
Made pop_unchecked public, added is_empty
2 parents e0323e7 + dadf5bf commit c82478f

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/vec.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ impl<T, const N: usize> Vec<T, N> {
236236
/// # Safety
237237
///
238238
/// This assumes the vec to have at least one element.
239-
pub(crate) unsafe fn pop_unchecked(&mut self) -> T {
240-
debug_assert!(!self.as_slice().is_empty());
239+
pub unsafe fn pop_unchecked(&mut self) -> T {
240+
debug_assert!(!self.is_empty());
241241

242242
self.len -= 1;
243243
(self.buffer.get_unchecked_mut(self.len).as_ptr() as *const T).read()
@@ -481,6 +481,12 @@ impl<T, const N: usize> Vec<T, N> {
481481
self.len == self.capacity()
482482
}
483483

484+
/// Returns true if the vec is empty
485+
#[inline]
486+
pub fn is_empty(&self) -> bool {
487+
self.len == 0
488+
}
489+
484490
/// Returns `true` if `needle` is a prefix of the Vec.
485491
///
486492
/// Always returns `true` if `needle` is an empty slice.
@@ -849,6 +855,30 @@ mod tests {
849855
let mut _v: Vec<i32, 4> = Vec::new();
850856
}
851857

858+
#[test]
859+
fn is_full_empty() {
860+
let mut v: Vec<i32, 4> = Vec::new();
861+
862+
assert!(v.is_empty());
863+
assert!(!v.is_full());
864+
865+
v.push(1).unwrap();
866+
assert!(!v.is_empty());
867+
assert!(!v.is_full());
868+
869+
v.push(1).unwrap();
870+
assert!(!v.is_empty());
871+
assert!(!v.is_full());
872+
873+
v.push(1).unwrap();
874+
assert!(!v.is_empty());
875+
assert!(!v.is_full());
876+
877+
v.push(1).unwrap();
878+
assert!(!v.is_empty());
879+
assert!(v.is_full());
880+
}
881+
852882
macro_rules! droppable {
853883
() => {
854884
struct Droppable;

0 commit comments

Comments
 (0)