-
Notifications
You must be signed in to change notification settings - Fork 11
Add range methods to Channel and Buf #28
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
Open
Be-ing
wants to merge
1
commit into
udoprog:main
Choose a base branch
from
Be-ing:range
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.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,213 @@ | ||
use crate::{Buf, BufMut, Channel, ChannelMut, ExactSizeBuf, ReadBuf}; | ||
|
||
use core::ops; | ||
use core::ops::Bound; | ||
|
||
/// A range of a buffer | ||
/// | ||
/// See [Buf::range]. | ||
pub struct Range<B> { | ||
buf: B, | ||
range: ops::Range<usize>, | ||
} | ||
|
||
impl<B> Range<B> { | ||
/// Construct a new limited buffer. | ||
pub(crate) fn new(buf: B, range: impl ops::RangeBounds<usize>) -> Self | ||
where B: Buf { | ||
let start = match range.start_bound() { | ||
Bound::Unbounded => 0, | ||
Bound::Included(&i) => i, | ||
Bound::Excluded(&i) => i + 1, | ||
}; | ||
let max = buf.frames_hint().expect("Unable to check bounds of range for Buf because frames_hint returned None"); | ||
let end = match range.end_bound() { | ||
Bound::Unbounded => max, | ||
Bound::Included(&i) => i + 1, | ||
Bound::Excluded(&i) => i, | ||
}; | ||
assert!(end <= max, "End index {} out of bounds, maximum {}", end, max); | ||
assert!(start <= end); | ||
let range = core::ops::Range{ start, end }; | ||
Self { buf, range } | ||
} | ||
} | ||
|
||
impl<B> Buf for Range<B> | ||
where | ||
B: Buf, | ||
{ | ||
type Sample = B::Sample; | ||
|
||
type Channel<'this> = B::Channel<'this> | ||
where | ||
Self: 'this; | ||
|
||
type IterChannels<'this> = IterChannels<B::IterChannels<'this>> | ||
where | ||
Self: 'this; | ||
|
||
fn frames_hint(&self) -> Option<usize> { | ||
Some(self.range.len()) | ||
} | ||
|
||
fn channels(&self) -> usize { | ||
self.buf.channels() | ||
} | ||
|
||
fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>> { | ||
Some(self.buf.get_channel(channel)?.range(self.range.clone())) | ||
} | ||
|
||
fn iter_channels(&self) -> Self::IterChannels<'_> { | ||
IterChannels { | ||
iter: self.buf.iter_channels(), | ||
range: self.range.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<B> BufMut for Range<B> | ||
where | ||
B: BufMut, | ||
{ | ||
type ChannelMut<'this> = B::ChannelMut<'this> | ||
where | ||
Self: 'this; | ||
|
||
type IterChannelsMut<'this> = IterChannelsMut<B::IterChannelsMut<'this>> | ||
where | ||
Self: 'this; | ||
|
||
fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>> { | ||
Some(self.buf.get_channel_mut(channel)?.range(self.range.clone())) | ||
} | ||
|
||
fn copy_channel(&mut self, from: usize, to: usize) | ||
where | ||
Self::Sample: Copy, | ||
{ | ||
self.buf.copy_channel(from, to); | ||
} | ||
|
||
fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_> { | ||
IterChannelsMut { | ||
iter: self.buf.iter_channels_mut(), | ||
range: self.range.clone(), | ||
} | ||
} | ||
} | ||
|
||
/// [Range] adjusts the implementation of [ExactSizeBuf] to take the frame | ||
/// limiting into account. | ||
/// | ||
/// ``` | ||
/// use audio::{Buf, ExactSizeBuf}; | ||
/// | ||
/// let buf = audio::interleaved![[0; 4]; 2]; | ||
/// | ||
/// assert_eq!((&buf).limit(0).frames(), 0); | ||
/// assert_eq!((&buf).limit(1).frames(), 1); | ||
/// assert_eq!((&buf).limit(5).frames(), 4); | ||
/// ``` | ||
impl<B> ExactSizeBuf for Range<B> | ||
where | ||
B: ExactSizeBuf, | ||
{ | ||
fn frames(&self) -> usize { | ||
self.range.len() | ||
} | ||
} | ||
|
||
impl<B> ReadBuf for Range<B> | ||
where | ||
B: ReadBuf, | ||
{ | ||
fn remaining(&self) -> usize { | ||
usize::min(self.buf.remaining(), self.range.len()) | ||
} | ||
|
||
fn advance(&mut self, n: usize) { | ||
self.buf.advance(usize::min(n, self.range.len())); | ||
} | ||
} | ||
|
||
// TODO: fix macro | ||
// iterators!(range: core::ops::Range<usize> => self.range(range.clone(()); | ||
|
||
pub struct IterChannels<I> { | ||
iter: I, | ||
range: core::ops::Range<usize>, | ||
} | ||
impl<I> Iterator for IterChannels<I> | ||
where | ||
I: Iterator, | ||
I::Item: Channel, | ||
{ | ||
type Item = I::Item; | ||
fn next(&mut self) -> Option<Self::Item> { | ||
Some(self.iter.next()?.range(self.range.clone())) | ||
} | ||
fn nth(&mut self, n: usize) -> Option<Self::Item> { | ||
Some(self.iter.nth(n)?.range(self.range.clone())) | ||
} | ||
} | ||
impl<I> DoubleEndedIterator for IterChannels<I> | ||
where | ||
I: DoubleEndedIterator, | ||
I::Item: Channel, | ||
{ | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
Some(self.iter.next_back()?.range(self.range.clone())) | ||
} | ||
fn nth_back(&mut self, n: usize) -> Option<Self::Item> { | ||
Some(self.iter.nth_back(n)?.range(self.range.clone())) | ||
} | ||
} | ||
impl<I> ExactSizeIterator for IterChannels<I> | ||
where | ||
I: ExactSizeIterator, | ||
I::Item: ChannelMut, | ||
{ | ||
fn len(&self) -> usize { | ||
self.iter.len() | ||
} | ||
} | ||
pub struct IterChannelsMut<I> { | ||
iter: I, | ||
range: core::ops::Range<usize>, | ||
} | ||
impl<I> Iterator for IterChannelsMut<I> | ||
where | ||
I: Iterator, | ||
I::Item: ChannelMut, | ||
{ | ||
type Item = I::Item; | ||
fn next(&mut self) -> Option<Self::Item> { | ||
Some(self.iter.next()?.range(self.range.clone())) | ||
} | ||
fn nth(&mut self, n: usize) -> Option<Self::Item> { | ||
Some(self.iter.nth(n)?.range(self.range.clone())) | ||
} | ||
} | ||
impl<I> DoubleEndedIterator for IterChannelsMut<I> | ||
where | ||
I: DoubleEndedIterator, | ||
I::Item: ChannelMut, | ||
{ | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
Some(self.iter.next_back()?.range(self.range.clone())) | ||
} | ||
fn nth_back(&mut self, n: usize) -> Option<Self::Item> { | ||
Some(self.iter.nth_back(n)?.range(self.range.clone())) | ||
} | ||
} | ||
impl<I> ExactSizeIterator for IterChannelsMut<I> | ||
where | ||
I: ExactSizeIterator, | ||
I::Item: ChannelMut, | ||
{ | ||
fn len(&self) -> usize { | ||
self.iter.len() | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -367,6 +367,49 @@ macro_rules! interleaved_channel { | |
self | ||
} | ||
|
||
fn range(self, range: impl core::ops::RangeBounds<usize>) -> Self { | ||
// Hack around trait method taking `self` rather than `mut self`. | ||
// This method returns a Self by value, so it is okay to move self | ||
// into a mut variable. | ||
let mut new = self; | ||
Comment on lines
+370
to
+374
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. Alternatively the trait method could take |
||
|
||
let start_index = match range.start_bound() { | ||
core::ops::Bound::Unbounded => 0, | ||
core::ops::Bound::Included(&i) => i, | ||
core::ops::Bound::Excluded(&i) => i + 1, | ||
}; | ||
let original_length = len!(new); | ||
let end_index = match range.end_bound() { | ||
core::ops::Bound::Unbounded => original_length, | ||
core::ops::Bound::Included(&i) => { | ||
let end_index = i + 1; | ||
assert!(end_index <= original_length); | ||
end_index | ||
}, | ||
core::ops::Bound::Excluded(&i) => { | ||
assert!(i <= original_length); | ||
i | ||
}, | ||
}; | ||
|
||
if mem::size_of::<T>() == 0 { | ||
let len = usize::min(original_length, end_index - start_index); | ||
zst_set_len!(new, len); | ||
} else { | ||
let start_ptr_offset = usize::min(original_length, start_index).saturating_mul(new.step); | ||
// Safety: internal invariants in this structure ensures it | ||
// doesn't go out of bounds. | ||
new.ptr = unsafe { ptr::NonNull::new_unchecked(new.ptr.as_ptr().wrapping_add(start_ptr_offset)) }; | ||
|
||
let end_ptr_offset = original_length.saturating_sub(end_index).saturating_mul(new.step); | ||
// Safety: internal invariants in this structure ensures it | ||
// doesn't go out of bounds. | ||
new.end = new.end.wrapping_sub(end_ptr_offset); | ||
} | ||
|
||
new | ||
} | ||
|
||
fn try_as_linear(&self) -> Option<&[T]> { | ||
None | ||
} | ||
|
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.
If you can suggest how to get the macro to accept the
range.clone()
, that would allow getting rid of the boilerplate below.