From bc2cd1be708c3b376d8cd6c5bee3d91a83d98775 Mon Sep 17 00:00:00 2001 From: Jacob Alexander Date: Tue, 28 Dec 2021 11:11:20 -0800 Subject: [PATCH 1/2] Relax Vec bounds to allow for zero-length Vecs Use when building generated code where it's possible to have a zero length array/vector (e.g. wire-format). See #252 - Added vec::tests::zero_capacity test - Added sealed::greater_than_eq_0 --- src/sealed.rs | 6 ++++++ src/vec.rs | 23 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/sealed.rs b/src/sealed.rs index 2efb07f2..53692319 100644 --- a/src/sealed.rs +++ b/src/sealed.rs @@ -52,6 +52,12 @@ pub(crate) const fn smaller_than() { Assert::::LESS; } +#[allow(dead_code)] +#[allow(path_statements)] +pub(crate) const fn greater_than_eq_0() { + Assert::::GREATER_EQ; +} + #[allow(dead_code)] #[allow(path_statements)] pub(crate) const fn greater_than_0() { diff --git a/src/vec.rs b/src/vec.rs index 51b4d50c..d458ea45 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -56,8 +56,8 @@ impl Vec { /// ``` /// `Vec` `const` constructor; wrap the returned value in [`Vec`](../struct.Vec.html) pub const fn new() -> Self { - // Const assert N > 0 - crate::sealed::greater_than_0::(); + // Const assert N >= 0 + crate::sealed::greater_than_eq_0::(); Self { buffer: [Self::INIT; N], @@ -1233,4 +1233,23 @@ mod tests { assert!(!v.ends_with(b"ba")); assert!(!v.ends_with(b"a")); } + + #[test] + fn zero_capacity() { + let mut v: Vec = Vec::new(); + // Validate capacity + assert_eq!(v.capacity(), 0); + + // Make sure there is no capacity + assert!(v.push(1).is_err()); + + // Validate length + assert_eq!(v.len(), 0); + + // Validate pop + assert_eq!(v.pop(), None); + + // Validate slice + assert_eq!(v.as_slice(), &[]); + } } From 0b92953ef12e93cb51c55759edb73e30625b6806 Mon Sep 17 00:00:00 2001 From: Jacob Alexander Date: Wed, 29 Dec 2021 12:01:45 -0800 Subject: [PATCH 2/2] Adding is_empty and is_full tests for zero capacity --- src/vec.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/vec.rs b/src/vec.rs index d458ea45..f2866c0c 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1251,5 +1251,11 @@ mod tests { // Validate slice assert_eq!(v.as_slice(), &[]); + + // Validate empty + assert!(v.is_empty()); + + // Validate full + assert!(v.is_full()); } }