Skip to content

Commit

Permalink
replace BufferDir with AnyBuffer
Browse files Browse the repository at this point in the history
Replace the BufferDir enum (with In, Out, and InOut variants) with an
AnyBuffer enum (with Const and Mut variants).
  • Loading branch information
micahrj committed Feb 13, 2024
1 parent 39ab989 commit 8fc6009
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
28 changes: 14 additions & 14 deletions src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ pub mod iter;
use bind::BindBuffers;
use iter::SplitAtEvents;

pub enum BufferDir<'a, 'b> {
In(Buffer<'a, 'b>),
Out(BufferMut<'a, 'b>),
InOut(BufferMut<'a, 'b>),
pub enum AnyBuffer<'a, 'b> {
Const(Buffer<'a, 'b>),
Mut(BufferMut<'a, 'b>),
}

impl<'a, 'b> BufferDir<'a, 'b> {
impl<'a, 'b> AnyBuffer<'a, 'b> {
#[inline]
pub unsafe fn from_raw_parts(
dir: BusDir,
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
) -> BufferDir<'a, 'b> {
) -> AnyBuffer<'a, 'b> {
match dir {
BusDir::In => BufferDir::In(Buffer::from_raw_parts(ptrs, offset, len)),
BusDir::Out => BufferDir::Out(BufferMut::from_raw_parts(ptrs, offset, len)),
BusDir::InOut => BufferDir::InOut(BufferMut::from_raw_parts(ptrs, offset, len)),
BusDir::In => AnyBuffer::Const(Buffer::from_raw_parts(ptrs, offset, len)),
BusDir::Out | BusDir::InOut => {
AnyBuffer::Mut(BufferMut::from_raw_parts(ptrs, offset, len))
}
}
}
}
Expand Down Expand Up @@ -86,10 +86,10 @@ impl<'a, 'b> Buffers<'a, 'b> {
}

#[inline]
pub fn get(&mut self, index: usize) -> Option<BufferDir> {
pub fn get(&mut self, index: usize) -> Option<AnyBuffer> {
if let Some(bus) = self.buses.get(index) {
unsafe {
Some(BufferDir::from_raw_parts(
Some(AnyBuffer::from_raw_parts(
bus.dir,
&self.ptrs[bus.start..bus.end],
self.offset,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl<'a, 'b> Buffers<'a, 'b> {
}

impl<'a, 'b> IntoIterator for Buffers<'a, 'b> {
type Item = BufferDir<'a, 'b>;
type Item = AnyBuffer<'a, 'b>;
type IntoIter = Buses<'a, 'b>;

#[inline]
Expand All @@ -160,13 +160,13 @@ pub struct Buses<'a, 'b> {
}

impl<'a, 'b> Iterator for Buses<'a, 'b> {
type Item = BufferDir<'a, 'b>;
type Item = AnyBuffer<'a, 'b>;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some(bus) = self.iter.next() {
unsafe {
Some(BufferDir::from_raw_parts(
Some(AnyBuffer::from_raw_parts(
bus.dir,
&self.ptrs[bus.start..bus.end],
self.offset,
Expand Down
15 changes: 7 additions & 8 deletions src/buffers/bind.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use super::{Buffer, BufferDir, BufferMut};
use super::{AnyBuffer, Buffer, BufferMut};

pub trait BindBuffers<'a, 'b>: Sized {
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a, 'b>>;
I: Iterator<Item = AnyBuffer<'a, 'b>>;
}

impl<'a, 'b> BindBuffers<'a, 'b> for Buffer<'a, 'b> {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a, 'b>>,
I: Iterator<Item = AnyBuffer<'a, 'b>>,
{
match buffers.next() {
Some(BufferDir::In(buffer)) => Some(buffer),
Some(AnyBuffer::Const(buffer)) => Some(buffer),
_ => None,
}
}
Expand All @@ -23,11 +23,10 @@ impl<'a, 'b> BindBuffers<'a, 'b> for BufferMut<'a, 'b> {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a, 'b>>,
I: Iterator<Item = AnyBuffer<'a, 'b>>,
{
match buffers.next() {
Some(BufferDir::Out(buffer)) => Some(buffer),
Some(BufferDir::InOut(buffer)) => Some(buffer),
Some(AnyBuffer::Mut(buffer)) => Some(buffer),
_ => None,
}
}
Expand All @@ -42,7 +41,7 @@ macro_rules! bind_buffers {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a, 'b>>,
I: Iterator<Item = AnyBuffer<'a, 'b>>,
{
Some((
$(
Expand Down

0 comments on commit 8fc6009

Please sign in to comment.