Skip to content

Commit

Permalink
rename Buffers::bind to collect; remove TryFrom impls for buffers
Browse files Browse the repository at this point in the history
Remove TryFrom impls for buffer types. Rename Buffers::bind to
Buffers::collect and BindBuffers to FromBuffers; make them return Option
instead of Result. Add Buffer(Mut)::collect methods to replace the
TryFrom impls for arrays of slices.
  • Loading branch information
micahrj committed Mar 9, 2024
1 parent 15de227 commit 6b88925
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 242 deletions.
2 changes: 1 addition & 1 deletion examples/gain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Processor for GainProcessor {
fn reset(&mut self) {}

fn process(&mut self, buffers: Buffers, events: Events) {
let buffer: BufferMut = buffers.try_into().unwrap();
let buffer = buffers.collect::<BufferMut>().unwrap();
for (buffer, events) in buffer.split_at_events(events) {
for event in events {
match event.data {
Expand Down
34 changes: 28 additions & 6 deletions src/buffers.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::marker::PhantomData;
use std::ops::{Index, IndexMut, Range};
use std::slice;
use std::{array, slice};

pub mod bind;
pub mod collect;
mod buffer_view;
pub mod iter;

pub use buffer_view::{BufferView, Offset, SampleView};

use bind::{BindBuffers, BindBuffersError};
use collect::FromBuffers;

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

#[inline]
pub fn bind<B: BindBuffers<'a, 'b>>(self) -> Result<B, BindBuffersError> {
pub fn collect<B: FromBuffers<'a, 'b>>(self) -> Option<B> {
let mut iter = self.into_iter();

let result = B::bind(&mut iter)?;

if iter.next().is_none() {
Ok(result)
Some(result)
} else {
Err(BindBuffersError(()))
None
}
}

Expand Down Expand Up @@ -279,6 +279,17 @@ impl<'a, 'b> Buffer<'a, 'b> {
pub fn channel_count(&self) -> usize {
self.raw.ptrs.len()
}

#[inline]
pub fn collect<const N: usize>(self) -> Option<[&'b [f32]; N]> {
if self.channel_count() != N {
return None;
}

Some(array::from_fn(|i| unsafe {
slice::from_raw_parts(self.raw.ptrs[i].offset(self.raw.offset), self.len)
}))
}
}

impl<'a, 'b> Index<usize> for Buffer<'a, 'b> {
Expand Down Expand Up @@ -404,6 +415,17 @@ impl<'a, 'b> BufferMut<'a, 'b> {
_marker: self._marker,
}
}

#[inline]
pub fn collect<const N: usize>(self) -> Option<[&'b mut [f32]; N]> {
if self.channel_count() != N {
return None;
}

Some(array::from_fn(|i| unsafe {
slice::from_raw_parts_mut(self.raw.ptrs[i].offset(self.raw.offset), self.len)
}))
}
}

impl<'a, 'b> Index<usize> for BufferMut<'a, 'b> {
Expand Down
235 changes: 0 additions & 235 deletions src/buffers/bind.rs

This file was deleted.

Loading

0 comments on commit 6b88925

Please sign in to comment.