Skip to content

Commit

Permalink
IntoSamples trait
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Mar 9, 2024
1 parent f387778 commit 66db666
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod iter;
pub use buffer_view::{BufferView, Offset};

use collect::FromBuffers;
use iter::IntoSamples;

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum BufferType {
Expand Down Expand Up @@ -134,7 +135,7 @@ impl<'a, 'b> Buffers<'a, 'b> {

#[inline]
pub fn samples<'c>(&'c mut self) -> iter::SamplesIter<'a, 'c> {
iter::SamplesIter::new(self.reborrow())
self.reborrow().into_samples()
}
}

Expand Down Expand Up @@ -331,7 +332,7 @@ impl<'a, 'b> Buffer<'a, 'b> {

#[inline]
pub fn samples(&self) -> iter::SampleIter<'a, 'b> {
iter::SampleIter::new(*self)
self.into_samples()
}
}

Expand Down Expand Up @@ -498,7 +499,7 @@ impl<'a, 'b> BufferMut<'a, 'b> {

#[inline]
pub fn samples<'c>(&'c mut self) -> iter::SampleIterMut<'a, 'c> {
iter::SampleIterMut::new(self.reborrow())
self.reborrow().into_samples()
}
}

Expand Down
43 changes: 40 additions & 3 deletions src/buffers/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ use super::{
};
use crate::events::Events;

pub trait IntoSamples {
type Sample;
type SampleIter: Iterator<Item = Self::Sample>;

fn into_samples(self) -> Self::SampleIter;
}

impl<'a, 'b> IntoSamples for Buffers<'a, 'b> {
type Sample = Samples<'a, 'b>;
type SampleIter = SamplesIter<'a, 'b>;

#[inline]
fn into_samples(self) -> Self::SampleIter {
SamplesIter::new(self)
}
}

pub struct SamplesIter<'a, 'b> {
buffers: &'a [BufferData],
ptrs: &'a [*mut f32],
Expand All @@ -14,7 +31,7 @@ pub struct SamplesIter<'a, 'b> {
}

impl<'a, 'b> SamplesIter<'a, 'b> {
pub(crate) fn new(buffers: Buffers<'a, 'b>) -> SamplesIter<'a, 'b> {
fn new(buffers: Buffers<'a, 'b>) -> SamplesIter<'a, 'b> {
SamplesIter {
buffers: buffers.buffers,
ptrs: buffers.ptrs,
Expand All @@ -41,6 +58,16 @@ impl<'a, 'b> Iterator for SamplesIter<'a, 'b> {
}
}

impl<'a, 'b> IntoSamples for Buffer<'a, 'b> {
type Sample = Sample<'a, 'b>;
type SampleIter = SampleIter<'a, 'b>;

#[inline]
fn into_samples(self) -> Self::SampleIter {
SampleIter::new(self)
}
}

pub struct SampleIter<'a, 'b> {
ptrs: &'a [*mut f32],
offset: isize,
Expand All @@ -49,7 +76,7 @@ pub struct SampleIter<'a, 'b> {
}

impl<'a, 'b> SampleIter<'a, 'b> {
pub(crate) fn new(buffer: Buffer<'a, 'b>) -> SampleIter<'a, 'b> {
fn new(buffer: Buffer<'a, 'b>) -> SampleIter<'a, 'b> {
SampleIter {
ptrs: buffer.ptrs,
offset: buffer.offset,
Expand All @@ -75,6 +102,16 @@ impl<'a, 'b> Iterator for SampleIter<'a, 'b> {
}
}

impl<'a, 'b> IntoSamples for BufferMut<'a, 'b> {
type Sample = SampleMut<'a, 'b>;
type SampleIter = SampleIterMut<'a, 'b>;

#[inline]
fn into_samples(self) -> Self::SampleIter {
SampleIterMut::new(self)
}
}

pub struct SampleIterMut<'a, 'b> {
ptrs: &'a [*mut f32],
offset: isize,
Expand All @@ -83,7 +120,7 @@ pub struct SampleIterMut<'a, 'b> {
}

impl<'a, 'b> SampleIterMut<'a, 'b> {
pub(crate) fn new(buffer: BufferMut<'a, 'b>) -> SampleIterMut<'a, 'b> {
fn new(buffer: BufferMut<'a, 'b>) -> SampleIterMut<'a, 'b> {
SampleIterMut {
ptrs: buffer.ptrs,
offset: buffer.offset,
Expand Down

0 comments on commit 66db666

Please sign in to comment.