From f4f82557bee57fa78229e7d321881362380cbf2e Mon Sep 17 00:00:00 2001 From: jdonszelmann Date: Fri, 15 Sep 2023 13:32:15 +0200 Subject: [PATCH] fmt --- src/lib.rs | 1 - src/ringbuffer_trait.rs | 26 +++-------- src/with_alloc/alloc_ringbuffer.rs | 9 +++- src/with_alloc/vecdeque.rs | 3 +- src/with_const_generics.rs | 74 +++++++++++++++++++++++------- 5 files changed, 71 insertions(+), 42 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 50caec0..ce57209 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -216,7 +216,6 @@ mod tests { test_iter(ConstGenericRingBuffer::::new()); } - #[test] fn run_test_forward_iter_non_power_of_two() { fn test_iter(mut b: impl RingBuffer) { diff --git a/src/ringbuffer_trait.rs b/src/ringbuffer_trait.rs index 8d70640..bc3dee8 100644 --- a/src/ringbuffer_trait.rs +++ b/src/ringbuffer_trait.rs @@ -19,8 +19,7 @@ use alloc::vec::Vec; /// implementation, since these safety guarantees are necessary for /// [`iter_mut`](RingBuffer::iter_mut) to work pub unsafe trait RingBuffer: - Sized + IntoIterator + Extend + - Index + IndexMut + Sized + IntoIterator + Extend + Index + IndexMut { /// Returns the length of the internal buffer. /// This length grows up to the capacity and then stops growing. @@ -466,10 +465,7 @@ macro_rules! impl_ringbuffer_ext { unsafe { // SAFETY: index has been modulo-ed to be within range // to be within bounds - $get_unchecked( - self, - $mask(self.buffer_size(), normalized_index as usize), - ) + $get_unchecked(self, $mask(self.buffer_size(), normalized_index as usize)) } }) } @@ -478,20 +474,15 @@ macro_rules! impl_ringbuffer_ext { fn get(&self, index: usize) -> Option<&T> { use core::ops::Not; self.is_empty().not().then(move || { - let normalized_index = - self.$readptr + index.rem_euclid(self.len()); + let normalized_index = self.$readptr + index.rem_euclid(self.len()); unsafe { // SAFETY: index has been modulo-ed to be within range // to be within bounds - $get_unchecked( - self, - $mask(self.buffer_size(), normalized_index), - ) + $get_unchecked(self, $mask(self.buffer_size(), normalized_index)) } }) } - #[inline] #[doc(hidden)] unsafe fn ptr_get_mut_signed(rb: *mut Self, index: isize) -> Option<*mut T> { @@ -520,17 +511,12 @@ macro_rules! impl_ringbuffer_ext { #[doc(hidden)] unsafe fn ptr_get_mut(rb: *mut Self, index: usize) -> Option<*mut T> { (Self::ptr_len(rb) != 0).then(move || { - - let normalized_index = (*rb).$readptr - + index.rem_euclid(Self::ptr_len(rb)); + let normalized_index = (*rb).$readptr + index.rem_euclid(Self::ptr_len(rb)); unsafe { // SAFETY: index has been modulo-ed to be within range // to be within bounds - $get_unchecked_mut( - rb, - $mask(Self::ptr_buffer_size(rb), normalized_index), - ) + $get_unchecked_mut(rb, $mask(Self::ptr_buffer_size(rb), normalized_index)) } }) } diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index 571238b..730b67d 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -272,7 +272,13 @@ unsafe impl RingBuffer for AllocRingBuffer { } } - impl_ringbuffer_ext!(get_unchecked, get_unchecked_mut, readptr, writeptr, mask_and); + impl_ringbuffer_ext!( + get_unchecked, + get_unchecked_mut, + readptr, + writeptr, + mask_and + ); #[inline] fn fill_with T>(&mut self, mut f: F) { @@ -362,7 +368,6 @@ impl IndexMut for AllocRingBuffer { } } - #[cfg(test)] mod tests { use crate::{AllocRingBuffer, RingBuffer}; diff --git a/src/with_alloc/vecdeque.rs b/src/with_alloc/vecdeque.rs index 7193cd0..23adeb1 100644 --- a/src/with_alloc/vecdeque.rs +++ b/src/with_alloc/vecdeque.rs @@ -241,7 +241,7 @@ unsafe impl RingBuffer for GrowableAllocRingBuffer { (*rb).0.get_mut(index) } - .map(|i| i as *mut T) + .map(|i| i as *mut T) } unsafe fn ptr_get_mut(rb: *mut Self, index: usize) -> Option<*mut T> { @@ -275,7 +275,6 @@ impl IndexMut for GrowableAllocRingBuffer { } } - impl FromIterator for GrowableAllocRingBuffer { fn from_iter>(iter: I) -> Self { Self(VecDeque::from_iter(iter)) diff --git a/src/with_const_generics.rs b/src/with_const_generics.rs index 88f98c1..e4b6570 100644 --- a/src/with_const_generics.rs +++ b/src/with_const_generics.rs @@ -352,7 +352,6 @@ impl IndexMut for ConstGenericRingBuffer { } } - #[cfg(test)] mod tests { use super::*; @@ -421,36 +420,77 @@ mod tests { #[cfg(test)] mod tests { + use crate::{AllocRingBuffer, ConstGenericRingBuffer, GrowableAllocRingBuffer, RingBuffer}; use alloc::collections::{LinkedList, VecDeque}; use alloc::string::ToString; - use crate::{AllocRingBuffer, ConstGenericRingBuffer, GrowableAllocRingBuffer, RingBuffer}; use alloc::vec; #[test] fn from() { - assert_eq!(ConstGenericRingBuffer::::from([1, 2, 3]).to_vec(), vec![1, 2, 3]); + assert_eq!( + ConstGenericRingBuffer::::from([1, 2, 3]).to_vec(), + vec![1, 2, 3] + ); let v: &[i32; 3] = &[1, 2, 3]; - assert_eq!(ConstGenericRingBuffer::::from(v).to_vec(), vec![1, 2, 3]); + assert_eq!( + ConstGenericRingBuffer::::from(v).to_vec(), + vec![1, 2, 3] + ); let v: &[i32] = &[1, 2, 3]; - assert_eq!(ConstGenericRingBuffer::::from(v).to_vec(), vec![1, 2, 3]); + assert_eq!( + ConstGenericRingBuffer::::from(v).to_vec(), + vec![1, 2, 3] + ); let v: &mut [i32; 3] = &mut [1, 2, 3]; - assert_eq!(ConstGenericRingBuffer::::from(v).to_vec(), vec![1, 2, 3]); + assert_eq!( + ConstGenericRingBuffer::::from(v).to_vec(), + vec![1, 2, 3] + ); let v: &mut [i32] = &mut [1, 2, 3]; - assert_eq!(ConstGenericRingBuffer::::from(v).to_vec(), vec![1, 2, 3]); - - assert_eq!(ConstGenericRingBuffer::::from(vec![1, 2, 3]).to_vec(), vec![1, 2, 3]); - assert_eq!(ConstGenericRingBuffer::::from(vec![1, 2, 3].into_iter().collect::>()).to_vec(), vec![1, 2, 3]); - assert_eq!(ConstGenericRingBuffer::::from(vec![1, 2, 3].into_iter().collect::>()).to_vec(), vec![1, 2, 3]); - assert_eq!(ConstGenericRingBuffer::<_, 3>::from("abc".to_string()).to_vec(), vec!['a', 'b', 'c']); - assert_eq!(ConstGenericRingBuffer::<_, 3>::from("abc").to_vec(), vec!['a', 'b', 'c']); - assert_eq!(ConstGenericRingBuffer::<_, 3>::from(GrowableAllocRingBuffer::from(vec![1, 2, 3])).to_vec(), vec![1, 2, 3]); - assert_eq!(ConstGenericRingBuffer::<_, 3>::from(AllocRingBuffer::from(vec![1, 2, 3])).to_vec(), vec![1, 2, 3]); + assert_eq!( + ConstGenericRingBuffer::::from(v).to_vec(), + vec![1, 2, 3] + ); + + assert_eq!( + ConstGenericRingBuffer::::from(vec![1, 2, 3]).to_vec(), + vec![1, 2, 3] + ); + assert_eq!( + ConstGenericRingBuffer::::from( + vec![1, 2, 3].into_iter().collect::>() + ) + .to_vec(), + vec![1, 2, 3] + ); + assert_eq!( + ConstGenericRingBuffer::::from( + vec![1, 2, 3].into_iter().collect::>() + ) + .to_vec(), + vec![1, 2, 3] + ); + assert_eq!( + ConstGenericRingBuffer::<_, 3>::from("abc".to_string()).to_vec(), + vec!['a', 'b', 'c'] + ); + assert_eq!( + ConstGenericRingBuffer::<_, 3>::from("abc").to_vec(), + vec!['a', 'b', 'c'] + ); + assert_eq!( + ConstGenericRingBuffer::<_, 3>::from(GrowableAllocRingBuffer::from(vec![1, 2, 3])) + .to_vec(), + vec![1, 2, 3] + ); + assert_eq!( + ConstGenericRingBuffer::<_, 3>::from(AllocRingBuffer::from(vec![1, 2, 3])).to_vec(), + vec![1, 2, 3] + ); } } - - }