-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimised extend #127
Draft
jdonszelmann
wants to merge
8
commits into
main
Choose a base branch
from
optimised_extend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Optimised extend #127
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7ab9e3c
add append
jdonszelmann d5a78de
add extend from slice
jdonszelmann 4e3b3ca
batched extend
jdonszelmann 0a68135
definitely some UB here oof
jdonszelmann 14824ab
more tests, miri doesn't mind
jdonszelmann 90675b6
bench after one
jdonszelmann cf0437d
batched extend implemented
jdonszelmann fbb1f31
fix docs
jdonszelmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ use core::ops::{Index, IndexMut}; | |
|
||
#[cfg(feature = "alloc")] | ||
extern crate alloc; | ||
|
||
#[cfg(feature = "alloc")] | ||
use alloc::vec::Vec; | ||
|
||
|
@@ -19,8 +20,10 @@ 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. | ||
/// This is because when the length is reached, new items are appended at the start. | ||
|
@@ -83,6 +86,41 @@ pub unsafe trait RingBuffer<T>: | |
self.push(value); | ||
} | ||
|
||
/// alias for [`extend`](RingBuffer::extend). | ||
#[inline] | ||
fn enqueue_many<I: IntoIterator<Item=T>>(&mut self, items: I) { | ||
self.extend(items); | ||
} | ||
|
||
/// Clones and appends all elements in a slice to the `Vec`. | ||
/// | ||
/// Iterates over the slice `other`, clones each element, and then appends | ||
/// it to this `RingBuffer`. The `other` slice is traversed in-order. | ||
/// | ||
/// Depending on the RingBuffer implementation, may be faster than inserting items in a loop | ||
/// | ||
/// Note that this function is same as [`extend`] except that it is | ||
/// specialized to work with slices instead. If and when Rust gets | ||
/// specialization this function will likely be deprecated (but still | ||
/// available). | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; | ||
/// | ||
/// let mut rb = ConstGenericRingBuffer::<_, 6>::new(); | ||
/// rb.push(1); | ||
/// | ||
/// rb.extend_from_slice(&[2, 3, 4]); | ||
/// assert_eq!(rb.to_vec(), vec![1, 2, 3, 4]); | ||
/// ``` | ||
/// | ||
/// [`extend`]: RingBuffer::extend | ||
fn extend_from_slice(&mut self, other: &[T]) where T: Clone { | ||
self.extend(other.into_iter().cloned()) | ||
} | ||
|
||
/// dequeues the top item off the ringbuffer, and moves this item out. | ||
fn dequeue(&mut self) -> Option<T>; | ||
|
||
|
@@ -119,21 +157,39 @@ pub unsafe trait RingBuffer<T>: | |
RingBufferDrainingIterator::new(self) | ||
} | ||
|
||
/// Moves all the elements of `other` into `self`, leaving `other` empty. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; | ||
/// | ||
/// let mut vec = ConstGenericRingBuffer::<_, 6>::from(vec![1, 2, 3]); | ||
/// let mut vec2 = ConstGenericRingBuffer::<_, 6>::from(vec![4, 5, 6]); | ||
/// | ||
/// vec.append(&mut vec2); | ||
/// assert_eq!(vec.to_vec(), &[1, 2, 3, 4, 5, 6]); | ||
/// assert_eq!(vec2.to_vec(), &[]); | ||
/// ``` | ||
fn append(&mut self, other: &mut Self) { | ||
self.extend(other.drain()); | ||
} | ||
|
||
/// Sets every element in the ringbuffer to the value returned by f. | ||
fn fill_with<F: FnMut() -> T>(&mut self, f: F); | ||
|
||
/// Sets every element in the ringbuffer to it's default value | ||
fn fill_default(&mut self) | ||
where | ||
T: Default, | ||
where | ||
T: Default, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rustfmt? |
||
{ | ||
self.fill_with(Default::default); | ||
} | ||
|
||
/// Sets every element in the ringbuffer to `value` | ||
fn fill(&mut self, value: T) | ||
where | ||
T: Clone, | ||
where | ||
T: Clone, | ||
{ | ||
self.fill_with(|| value.clone()); | ||
} | ||
|
@@ -233,16 +289,16 @@ pub unsafe trait RingBuffer<T>: | |
/// Converts the buffer to a vector. This Copies all elements in the ringbuffer. | ||
#[cfg(feature = "alloc")] | ||
fn to_vec(&self) -> Vec<T> | ||
where | ||
T: Clone, | ||
where | ||
T: Clone, | ||
{ | ||
self.iter().cloned().collect() | ||
} | ||
|
||
/// Returns true if elem is in the ringbuffer. | ||
fn contains(&self, elem: &T) -> bool | ||
where | ||
T: PartialEq, | ||
where | ||
T: PartialEq, | ||
{ | ||
self.iter().any(|i| i == elem) | ||
} | ||
|
@@ -339,7 +395,7 @@ mod iter { | |
impl<'rb, T: 'rb, RB: RingBuffer<T> + 'rb> ExactSizeIterator for RingBufferMutIterator<'rb, T, RB> {} | ||
|
||
impl<'rb, T: 'rb, RB: RingBuffer<T> + 'rb> DoubleEndedIterator | ||
for RingBufferMutIterator<'rb, T, RB> | ||
for RingBufferMutIterator<'rb, T, RB> | ||
{ | ||
#[inline] | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this actually specialized now or will it be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in constgeneric yes