Skip to content

Commit

Permalink
use separate lifetimes for ptr slices vs. the buffers themselves in b…
Browse files Browse the repository at this point in the history
…uffer types
  • Loading branch information
micahrj committed Feb 12, 2024
1 parent 027e762 commit 51d1d6c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 53 deletions.
70 changes: 39 additions & 31 deletions src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ pub mod iter;
use bind::BindBuffers;
use iter::SplitAtEvents;

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

impl<'a> BufferDir<'a> {
impl<'a, 'b> BufferDir<'a, 'b> {
#[inline]
pub unsafe fn from_raw_parts(
dir: BusDir,
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
) -> BufferDir<'a> {
) -> BufferDir<'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)),
Expand All @@ -39,22 +39,22 @@ pub struct BusData {
pub end: usize,
}

pub struct Buffers<'a> {
pub struct Buffers<'a, 'b> {
buses: &'a [BusData],
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
_marker: PhantomData<&'a mut f32>,
_marker: PhantomData<&'b mut f32>,
}

impl<'a> Buffers<'a> {
impl<'a, 'b> Buffers<'a, 'b> {
#[inline]
pub unsafe fn from_raw_parts(
buses: &'a [BusData],
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
) -> Buffers<'a> {
) -> Buffers<'a, 'b> {
Buffers {
buses,
ptrs,
Expand All @@ -75,7 +75,7 @@ impl<'a> Buffers<'a> {
}

#[inline]
pub fn reborrow(&mut self) -> Buffers {
pub fn reborrow<'c>(&'c mut self) -> Buffers<'a, 'c> {
Buffers {
buses: self.buses,
ptrs: self.ptrs,
Expand All @@ -102,7 +102,7 @@ impl<'a> Buffers<'a> {
}

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

let result = B::bind(&mut iter)?;
Expand Down Expand Up @@ -130,14 +130,14 @@ impl<'a> Buffers<'a> {
}

#[inline]
pub fn split_at_events<'b>(self, events: Events<'b>) -> SplitAtEvents<'a, 'b> {
pub fn split_at_events<'e>(self, events: Events<'e>) -> SplitAtEvents<'a, 'b, 'e> {
SplitAtEvents::new(self, events)
}
}

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

#[inline]
fn into_iter(self) -> Self::IntoIter {
Expand All @@ -151,16 +151,16 @@ impl<'a> IntoIterator for Buffers<'a> {
}
}

pub struct Buses<'a> {
pub struct Buses<'a, 'b> {
iter: slice::Iter<'a, BusData>,
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
_marker: PhantomData<&'a mut f32>,
_marker: PhantomData<&'b mut f32>,
}

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

#[inline]
fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -179,16 +179,20 @@ impl<'a> Iterator for Buses<'a> {
}
}

pub struct Buffer<'a> {
pub struct Buffer<'a, 'b> {
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
_marker: PhantomData<&'a f32>,
_marker: PhantomData<&'b f32>,
}

impl<'a> Buffer<'a> {
impl<'a, 'b> Buffer<'a, 'b> {
#[inline]
pub unsafe fn from_raw_parts(ptrs: &'a [*mut f32], offset: usize, len: usize) -> Buffer<'a> {
pub unsafe fn from_raw_parts(
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
) -> Buffer<'a, 'b> {
Buffer {
ptrs,
offset,
Expand All @@ -208,7 +212,7 @@ impl<'a> Buffer<'a> {
}
}

impl<'a> Index<usize> for Buffer<'a> {
impl<'a, 'b> Index<usize> for Buffer<'a, 'b> {
type Output = [f32];

#[inline]
Expand All @@ -217,16 +221,20 @@ impl<'a> Index<usize> for Buffer<'a> {
}
}

pub struct BufferMut<'a> {
pub struct BufferMut<'a, 'b> {
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
_marker: PhantomData<&'a mut f32>,
_marker: PhantomData<&'b mut f32>,
}

impl<'a> BufferMut<'a> {
impl<'a, 'b> BufferMut<'a, 'b> {
#[inline]
pub unsafe fn from_raw_parts(ptrs: &'a [*mut f32], offset: usize, len: usize) -> BufferMut<'a> {
pub unsafe fn from_raw_parts(
ptrs: &'a [*mut f32],
offset: usize,
len: usize,
) -> BufferMut<'a, 'b> {
BufferMut {
ptrs,
offset,
Expand All @@ -246,7 +254,7 @@ impl<'a> BufferMut<'a> {
}
}

impl<'a> Index<usize> for BufferMut<'a> {
impl<'a, 'b> Index<usize> for BufferMut<'a, 'b> {
type Output = [f32];

#[inline]
Expand All @@ -255,7 +263,7 @@ impl<'a> Index<usize> for BufferMut<'a> {
}
}

impl<'a> IndexMut<usize> for BufferMut<'a> {
impl<'a, 'b> IndexMut<usize> for BufferMut<'a, 'b> {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut [f32] {
unsafe { slice::from_raw_parts_mut(self.ptrs[index].add(self.offset), self.len) }
Expand Down
28 changes: 14 additions & 14 deletions src/buffers/bind.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use super::{Buffer, BufferDir, BufferMut};

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

pub struct In<'a>(pub Buffer<'a>);
pub struct In<'a, 'b>(pub Buffer<'a, 'b>);

impl<'a> BindBuffers<'a> for In<'a> {
impl<'a, 'b> BindBuffers<'a, 'b> for In<'a, 'b> {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>,
I: Iterator<Item = BufferDir<'a, 'b>>,
{
match buffers.next() {
Some(BufferDir::In(buffer)) => Some(In(buffer)),
Expand All @@ -21,13 +21,13 @@ impl<'a> BindBuffers<'a> for In<'a> {
}
}

pub struct Out<'a>(pub BufferMut<'a>);
pub struct Out<'a, 'b>(pub BufferMut<'a, 'b>);

impl<'a> BindBuffers<'a> for Out<'a> {
impl<'a, 'b> BindBuffers<'a, 'b> for Out<'a, 'b> {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>,
I: Iterator<Item = BufferDir<'a, 'b>>,
{
match buffers.next() {
Some(BufferDir::Out(buffer)) => Some(Out(buffer)),
Expand All @@ -36,13 +36,13 @@ impl<'a> BindBuffers<'a> for Out<'a> {
}
}

pub struct InOut<'a>(pub BufferMut<'a>);
pub struct InOut<'a, 'b>(pub BufferMut<'a, 'b>);

impl<'a> BindBuffers<'a> for InOut<'a> {
impl<'a, 'b> BindBuffers<'a, 'b> for InOut<'a, 'b> {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>,
I: Iterator<Item = BufferDir<'a, 'b>>,
{
match buffers.next() {
Some(BufferDir::InOut(buffer)) => Some(InOut(buffer)),
Expand All @@ -53,14 +53,14 @@ impl<'a> BindBuffers<'a> for InOut<'a> {

macro_rules! bind_buffers {
($($binding:ident),*) => {
impl<'a, $($binding),*> BindBuffers<'a> for ($($binding,)*)
impl<'a, 'b, $($binding),*> BindBuffers<'a, 'b> for ($($binding,)*)
where
$($binding: BindBuffers<'a>),*
$($binding: BindBuffers<'a, 'b>),*
{
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>,
I: Iterator<Item = BufferDir<'a, 'b>>,
{
Some((
$(
Expand Down
16 changes: 8 additions & 8 deletions src/buffers/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use std::marker::PhantomData;
use super::Buffers;
use crate::events::Events;

pub struct SplitAtEvents<'a, 'b> {
buffers: Buffers<'a>,
events: Events<'b>,
pub struct SplitAtEvents<'a, 'b, 'e> {
buffers: Buffers<'a, 'b>,
events: Events<'e>,
offset: i64,
}

impl<'a, 'b> SplitAtEvents<'a, 'b> {
impl<'a, 'b, 'e> SplitAtEvents<'a, 'b, 'e> {
#[inline]
pub(crate) fn new(buffers: Buffers<'a>, events: Events<'b>) -> SplitAtEvents<'a, 'b> {
pub(crate) fn new(buffers: Buffers<'a, 'b>, events: Events<'e>) -> SplitAtEvents<'a, 'b, 'e> {
SplitAtEvents {
buffers: buffers,
events: events,
Expand All @@ -20,11 +20,11 @@ impl<'a, 'b> SplitAtEvents<'a, 'b> {
}
}

impl<'a, 'b> Iterator for SplitAtEvents<'a, 'b> {
type Item = (Buffers<'a>, Events<'b>);
impl<'a, 'b, 'e> Iterator for SplitAtEvents<'a, 'b, 'e> {
type Item = (Buffers<'a, 'b>, Events<'e>);

#[inline]
fn next(&mut self) -> Option<(Buffers<'a>, Events<'b>)> {
fn next(&mut self) -> Option<(Buffers<'a, 'b>, Events<'e>)> {
if self.buffers.len() == 0 && self.events.len() == 0 {
return None;
}
Expand Down

0 comments on commit 51d1d6c

Please sign in to comment.