Skip to content

Commit

Permalink
Better lifetimes in PortPair, add missing Copy/Clone implementations …
Browse files Browse the repository at this point in the history
…for Port pairs and related types
  • Loading branch information
prokopyl committed Oct 26, 2024
1 parent a73d316 commit 5f3a3bd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions plugin/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ impl<'a> Audio<'a> {
/// channel buffers pointed to by `buffers`.
#[inline]
pub unsafe fn from_raw_buffers(
inputs: *const [clap_audio_buffer],
outputs: *const [clap_audio_buffer],
inputs: *mut [clap_audio_buffer],
outputs: *mut [clap_audio_buffer],
frames_count: u32,
) -> Self {
Self {
Expand Down
24 changes: 21 additions & 3 deletions plugin/src/process/audio/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'a> PortPair<'a> {
///
/// If the port layout is asymmetric and there is no input port, this returns [`None`].
#[inline]
pub fn input(&self) -> Option<Port> {
pub fn input(&self) -> Option<Port<'a>> {
self.input
// SAFETY: this type ensures the buffer is valid and matches frame_count
.map(|i| unsafe { Port::from_raw(i, self.frames_count) })
Expand All @@ -53,7 +53,7 @@ impl<'a> PortPair<'a> {
///
/// If the port layout is asymmetric and there is no output port, this returns [`None`].
#[inline]
pub fn output(&self) -> Option<Port> {
pub fn output(&self) -> Option<Port<'a>> {
self.output
// SAFETY: this type ensures the buffer is valid and matches frame_count
.map(|i| unsafe { Port::from_raw(i, self.frames_count) })
Expand Down Expand Up @@ -150,7 +150,6 @@ impl<'a> PortPair<'a> {
///
/// The sample type `S` is always going to be either [`f32`] or [`f64`], as returned by
/// [`PortPair::channels`].
#[derive(Copy, Clone)]
pub struct PairedChannels<'a, S> {
input_data: &'a [*mut S],
output_data: &'a [*mut S],
Expand Down Expand Up @@ -223,6 +222,13 @@ impl<'a, S> PairedChannels<'a, S> {
}
}

impl<'a, S> Copy for PairedChannels<'a, S> {}
impl<'a, S> Clone for PairedChannels<'a, S> {
fn clone(&self) -> Self {
*self
}
}

impl<'a, S> IntoIterator for PairedChannels<'a, S> {
type Item = ChannelPair<'a, S>;
type IntoIter = PairedChannelsIter<'a, S>;
Expand Down Expand Up @@ -282,7 +288,19 @@ impl<S> ExactSizeIterator for PairedChannelsIter<'_, S> {
}
}

impl<'a, S> Clone for PairedChannelsIter<'a, S> {
#[inline]
fn clone(&self) -> Self {
Self {
input_iter: self.input_iter.clone(),
output_iter: self.output_iter.clone(),
frames_count: self.frames_count,
}
}
}

/// An iterator of all of the available [`PortPair`]s from an [`Audio`] struct.
#[derive(Clone)]
pub struct PortPairsIter<'a> {
inputs: Iter<'a, CelledClapAudioBuffer>,
outputs: Iter<'a, CelledClapAudioBuffer>,
Expand Down

0 comments on commit 5f3a3bd

Please sign in to comment.