Skip to content

Commit

Permalink
Merge pull request #110 from tertsdiepraam/size-parameter-on-new
Browse files Browse the repository at this point in the history
Add a size parameter for `ConstGenericRingBuffer::new`
  • Loading branch information
jdonszelmann committed Sep 15, 2023
2 parents 1ae229d + fff586c commit 5c75226
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/with_const_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ impl<T, const CAP: usize> ConstGenericRingBuffer<T, CAP> {
/// of two might be significantly (up to 3 times) slower.
#[inline]
#[must_use]
pub const fn new() -> Self {
pub const fn new<const N: usize>() -> Self
where
ConstGenericRingBuffer<T, CAP>: From<ConstGenericRingBuffer<T, N>>,
{
#[allow(clippy::let_unit_value)]
let _ = Self::ERROR_CAPACITY_IS_NOT_ALLOWED_TO_BE_ZERO;

Expand Down
4 changes: 2 additions & 2 deletions tests/compile-fail/test_const_generic_array_zero_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use ringbuffer::ConstGenericRingBuffer;

fn main() {
let _ = ConstGenericRingBuffer::<i32, 0>::new();
//~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::<i32, 0>::new`
//~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::<i32, 0>::new::<0>`
// ringbuffer can't be zero length
}
}
10 changes: 10 additions & 0 deletions tests/compile-fail/test_const_generic_array_zero_length_new.rs
Original file line number Diff line number Diff line change
@@ -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::<i32, 0>::new::<0>`
// ringbuffer can't be zero length
buf.push(5);
}
15 changes: 15 additions & 0 deletions tests/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32, 50>::new::<50>();
a.push(5);

// Can we specify size only on the struct?
let mut a = ConstGenericRingBuffer::<i32, 50>::new();
a.push(5);
}

0 comments on commit 5c75226

Please sign in to comment.