Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Sep 15, 2023
1 parent 751c963 commit f4f8255
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 42 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ mod tests {
test_iter(ConstGenericRingBuffer::<i32, 8>::new());
}


#[test]
fn run_test_forward_iter_non_power_of_two() {
fn test_iter(mut b: impl RingBuffer<i32>) {
Expand Down
26 changes: 6 additions & 20 deletions src/ringbuffer_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>:
Sized + IntoIterator<Item = T> + Extend<T> +
Index<usize, Output = T> + IndexMut<usize>
Sized + IntoIterator<Item = T> + Extend<T> + Index<usize, Output = T> + IndexMut<usize>
{
/// Returns the length of the internal buffer.
/// This length grows up to the capacity and then stops growing.
Expand Down Expand Up @@ -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))
}
})
}
Expand All @@ -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> {
Expand Down Expand Up @@ -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))
}
})
}
Expand Down
9 changes: 7 additions & 2 deletions src/with_alloc/alloc_ringbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ unsafe impl<T> RingBuffer<T> for AllocRingBuffer<T> {
}
}

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<F: FnMut() -> T>(&mut self, mut f: F) {
Expand Down Expand Up @@ -362,7 +368,6 @@ impl<T> IndexMut<usize> for AllocRingBuffer<T> {
}
}


#[cfg(test)]
mod tests {
use crate::{AllocRingBuffer, RingBuffer};
Expand Down
3 changes: 1 addition & 2 deletions src/with_alloc/vecdeque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ unsafe impl<T> RingBuffer<T> for GrowableAllocRingBuffer<T> {

(*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> {
Expand Down Expand Up @@ -275,7 +275,6 @@ impl<T> IndexMut<usize> for GrowableAllocRingBuffer<T> {
}
}


impl<T> FromIterator<T> for GrowableAllocRingBuffer<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self(VecDeque::from_iter(iter))
Expand Down
74 changes: 57 additions & 17 deletions src/with_const_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ impl<T, const CAP: usize> IndexMut<usize> for ConstGenericRingBuffer<T, CAP> {
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -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::<i32, 3>::from([1, 2, 3]).to_vec(), vec![1, 2, 3]);
assert_eq!(
ConstGenericRingBuffer::<i32, 3>::from([1, 2, 3]).to_vec(),
vec![1, 2, 3]
);

let v: &[i32; 3] = &[1, 2, 3];
assert_eq!(ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(), vec![1, 2, 3]);
assert_eq!(
ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(),
vec![1, 2, 3]
);

let v: &[i32] = &[1, 2, 3];
assert_eq!(ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(), vec![1, 2, 3]);
assert_eq!(
ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(),
vec![1, 2, 3]
);

let v: &mut [i32; 3] = &mut [1, 2, 3];
assert_eq!(ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(), vec![1, 2, 3]);
assert_eq!(
ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(),
vec![1, 2, 3]
);

let v: &mut [i32] = &mut [1, 2, 3];
assert_eq!(ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(), vec![1, 2, 3]);

assert_eq!(ConstGenericRingBuffer::<i32, 3>::from(vec![1, 2, 3]).to_vec(), vec![1, 2, 3]);
assert_eq!(ConstGenericRingBuffer::<i32, 3>::from(vec![1, 2, 3].into_iter().collect::<VecDeque<_>>()).to_vec(), vec![1, 2, 3]);
assert_eq!(ConstGenericRingBuffer::<i32, 3>::from(vec![1, 2, 3].into_iter().collect::<LinkedList<_>>()).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::<i32, 3>::from(v).to_vec(),
vec![1, 2, 3]
);

assert_eq!(
ConstGenericRingBuffer::<i32, 3>::from(vec![1, 2, 3]).to_vec(),
vec![1, 2, 3]
);
assert_eq!(
ConstGenericRingBuffer::<i32, 3>::from(
vec![1, 2, 3].into_iter().collect::<VecDeque<_>>()
)
.to_vec(),
vec![1, 2, 3]
);
assert_eq!(
ConstGenericRingBuffer::<i32, 3>::from(
vec![1, 2, 3].into_iter().collect::<LinkedList<_>>()
)
.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]
);
}
}


}

1 comment on commit f4f8255

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.