Skip to content

Commit

Permalink
remove Buffers::collect; reinstate TryFrom impls for non-nested tuples
Browse files Browse the repository at this point in the history
Remove Buffers::collect, Buffer(Mut)::collect, and FromBuffers trait in favor
of the standard TryFrom trait again. However, only impl TryFrom<Buffers> for
non-nested tuples; do not implement it for singular Buffer(Mut) types or for
arbitrarily nested tuples.
  • Loading branch information
micahrj committed Jul 15, 2024
1 parent 3cc473f commit b7ac717
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 144 deletions.
40 changes: 2 additions & 38 deletions src/buffers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::marker::PhantomData;
use std::ops::{Index, IndexMut, Range};
use std::{array, slice};
use std::slice;

pub mod collect;
pub mod convert;
pub mod iter;

use crate::events::Events;
use collect::FromBuffers;
use iter::{BlockIterator, IntoBlocks, IntoSamples};

#[derive(Copy, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -108,19 +107,6 @@ impl<'a, 'b> Buffers<'a, 'b> {
}
}

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

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

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

#[inline]
pub fn slice(&mut self, range: Range<usize>) -> Option<Buffers> {
if range.start > range.end || range.end > self.len {
Expand Down Expand Up @@ -335,17 +321,6 @@ impl<'a, 'b> Buffer<'a, 'b> {
self.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.ptrs[i].offset(self.offset), self.len)
}))
}

#[inline]
pub fn samples(&self) -> iter::SampleIter<'a, 'b> {
self.into_samples()
Expand Down Expand Up @@ -515,17 +490,6 @@ impl<'a, 'b> BufferMut<'a, 'b> {
}
}

#[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.ptrs[i].offset(self.offset), self.len)
}))
}

#[inline]
pub fn samples<'c>(&'c mut self) -> iter::SampleIterMut<'a, 'c> {
self.reborrow().into_samples()
Expand Down
106 changes: 0 additions & 106 deletions src/buffers/collect.rs

This file was deleted.

110 changes: 110 additions & 0 deletions src/buffers/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use std::error::Error;
use std::{array, fmt, slice};

use super::{AnyBuffer, Buffer, BufferMut, Buffers};

impl<'a, 'b> TryFrom<AnyBuffer<'a, 'b>> for Buffer<'a, 'b> {
type Error = AnyBuffer<'a, 'b>;

#[inline]
fn try_from(value: AnyBuffer<'a, 'b>) -> Result<Buffer<'a, 'b>, Self::Error> {
match value {
AnyBuffer::Const(buffer) => Ok(buffer),
_ => Err(value),
}
}
}

impl<'a, 'b> TryFrom<AnyBuffer<'a, 'b>> for BufferMut<'a, 'b> {
type Error = AnyBuffer<'a, 'b>;

#[inline]
fn try_from(value: AnyBuffer<'a, 'b>) -> Result<BufferMut<'a, 'b>, Self::Error> {
match value {
AnyBuffer::Mut(buffer) => Ok(buffer),
_ => Err(value),
}
}
}

impl<'a, 'b, const N: usize> TryFrom<Buffer<'a, 'b>> for [&'b [f32]; N] {
type Error = Buffer<'a, 'b>;

#[inline]
fn try_from(value: Buffer<'a, 'b>) -> Result<[&'b [f32]; N], Self::Error> {
if value.channel_count() == N {
Ok(array::from_fn(|i| unsafe {
slice::from_raw_parts(value.ptrs[i].offset(value.offset), value.len)
}))
} else {
Err(value)
}
}
}

impl<'a, 'b, const N: usize> TryFrom<BufferMut<'a, 'b>> for [&'b mut [f32]; N] {
type Error = BufferMut<'a, 'b>;

#[inline]
fn try_from(value: BufferMut<'a, 'b>) -> Result<[&'b mut [f32]; N], Self::Error> {
if value.channel_count() == N {
Ok(array::from_fn(|i| unsafe {
slice::from_raw_parts_mut(value.ptrs[i].offset(value.offset), value.len)
}))
} else {
Err(value)
}
}
}

#[derive(Debug)]
pub struct TryFromBuffersError;

impl fmt::Display for TryFromBuffersError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
"buffer layout does not match".fmt(fmt)
}
}

impl Error for TryFromBuffersError {}

macro_rules! try_from_buffers {
($($buffer:ident),*) => {
impl<'a, 'b, $($buffer),*> TryFrom<Buffers<'a, 'b>> for ($($buffer,)*)
where
$($buffer: TryFrom<AnyBuffer<'a, 'b>>),*
{
type Error = TryFromBuffersError;

#[inline]
fn try_from(value: Buffers<'a, 'b>) -> Result<Self, Self::Error> {
let mut iter = value.into_iter();

let result = (
$({
let next = iter.next().ok_or(TryFromBuffersError)?;
$buffer::try_from(next).map_err(|_| TryFromBuffersError)?
},)*
);

if iter.next().is_none() {
Ok(result)
} else {
Err(TryFromBuffersError)
}
}
}
}
}

try_from_buffers!();
try_from_buffers!(B0);
try_from_buffers!(B0, B1);
try_from_buffers!(B0, B1, B2);
try_from_buffers!(B0, B1, B2, B3);
try_from_buffers!(B0, B1, B2, B3, B4);
try_from_buffers!(B0, B1, B2, B3, B4, B5);
try_from_buffers!(B0, B1, B2, B3, B4, B5, B6);
try_from_buffers!(B0, B1, B2, B3, B4, B5, B6, B7);
try_from_buffers!(B0, B1, B2, B3, B4, B5, B6, B7, B8);
try_from_buffers!(B0, B1, B2, B3, B4, B5, B6, B7, B8, B9);

0 comments on commit b7ac717

Please sign in to comment.