From fff586c13d642da57fed8bb60e7ed88ac1a3cb9f Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Thu, 8 Jun 2023 19:12:13 +0200 Subject: [PATCH] Add a size parameter for ConstGenericRingBuffer::new --- src/with_const_generics.rs | 5 ++++- .../test_const_generic_array_zero_length.rs | 4 ++-- .../test_const_generic_array_zero_length_new.rs | 10 ++++++++++ tests/conversions.rs | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/compile-fail/test_const_generic_array_zero_length_new.rs diff --git a/src/with_const_generics.rs b/src/with_const_generics.rs index 56e6cf8..557a770 100644 --- a/src/with_const_generics.rs +++ b/src/with_const_generics.rs @@ -174,7 +174,10 @@ impl ConstGenericRingBuffer { /// of two might be significantly (up to 3 times) slower. #[inline] #[must_use] - pub const fn new() -> Self { + pub const fn new() -> Self + where + ConstGenericRingBuffer: From>, + { #[allow(clippy::let_unit_value)] let _ = Self::ERROR_CAPACITY_IS_NOT_ALLOWED_TO_BE_ZERO; diff --git a/tests/compile-fail/test_const_generic_array_zero_length.rs b/tests/compile-fail/test_const_generic_array_zero_length.rs index b9caa56..3b69f1a 100644 --- a/tests/compile-fail/test_const_generic_array_zero_length.rs +++ b/tests/compile-fail/test_const_generic_array_zero_length.rs @@ -4,6 +4,6 @@ use ringbuffer::ConstGenericRingBuffer; fn main() { let _ = ConstGenericRingBuffer::::new(); - //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new` + //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` // ringbuffer can't be zero length -} \ No newline at end of file +} diff --git a/tests/compile-fail/test_const_generic_array_zero_length_new.rs b/tests/compile-fail/test_const_generic_array_zero_length_new.rs new file mode 100644 index 0000000..24a3494 --- /dev/null +++ b/tests/compile-fail/test_const_generic_array_zero_length_new.rs @@ -0,0 +1,10 @@ +extern crate ringbuffer; + +use ringbuffer::{ConstGenericRingBuffer, RingBufferWrite}; + +fn main() { + let mut buf = ConstGenericRingBuffer::new::<0>(); + //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` + // ringbuffer can't be zero length + buf.push(5); +} diff --git a/tests/conversions.rs b/tests/conversions.rs index 8140f40..969427e 100644 --- a/tests/conversions.rs +++ b/tests/conversions.rs @@ -118,3 +118,18 @@ fn test_extra_conversions_const() { let a = ConstGenericRingBuffer::<_, 2>::from(a); assert_eq!(a.to_vec(), vec![1, 2]); } + +#[test] +fn test_const_generic_new_parameter() { + // Can we specify size only on the method? + let mut a = ConstGenericRingBuffer::new::<2>(); + a.push(5); + + // Can we specify size in both positions? + let mut a = ConstGenericRingBuffer::::new::<50>(); + a.push(5); + + // Can we specify size only on the struct? + let mut a = ConstGenericRingBuffer::::new(); + a.push(5); +}