1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/// Trait implemented for buffers that can be resized.
pub trait ResizableBuf {
/// Ensure that the audio buffer has space for at least the given `capacity`
/// of contiguous memory. The `capacity` is specified in number of
/// [Samples][crate::Buf::Sample].
///
/// This is a no-op unless the underlying buffer is contiguous in memory
/// which can be ensured by requiring traits such as
/// [InterleavedBufMut][crate::InterleavedBufMut].
///
/// This returns a boolean indicating if we could successfully reserve the
/// given amount of memory. The caller can only assume that a buffer is
/// present up to the given number of samples if it returns `true`. This is
/// important to observe if the code you're working on has safety
/// implications.
///
/// A typical approach in case a reservation fails is to either panic or
/// return an error indicating that the provided buffer is not supported.
fn try_reserve(&mut self, capacity: usize) -> bool;
/// Resize the number of per-channel frames in the buffer.
///
/// # Examples
///
/// ```
/// use audio::{Buf, ExactSizeBuf, ResizableBuf};
///
/// fn test(mut buffer: impl ResizableBuf) {
/// buffer.resize_frames(4);
/// }
///
/// let mut buf = audio::interleaved![[0; 0]; 2];
///
/// assert_eq!(buf.channels(), 2);
/// assert_eq!(buf.frames(), 0);
///
/// test(&mut buf);
///
/// assert_eq!(buf.channels(), 2);
/// assert_eq!(buf.frames(), 4);
/// ```
fn resize_frames(&mut self, frames: usize);
/// Resize the buffer to match the given topology.
///
/// # Examples
///
/// ```
/// use audio::ResizableBuf;
///
/// fn test(mut buf: impl ResizableBuf) {
/// buf.resize_topology(2, 4);
/// }
///
/// let mut buf = audio::interleaved![[0; 0]; 4];
///
/// test(&mut buf);
///
/// assert_eq!(buf.channels(), 2);
/// assert_eq!(buf.frames(), 4);
/// ```
fn resize_topology(&mut self, channels: usize, frames: usize);
}
impl<B> ResizableBuf for &mut B
where
B: ?Sized + ResizableBuf,
{
fn try_reserve(&mut self, capacity: usize) -> bool {
(**self).try_reserve(capacity)
}
fn resize_frames(&mut self, frames: usize) {
(**self).resize_frames(frames);
}
fn resize_topology(&mut self, channels: usize, frames: usize) {
(**self).resize_topology(channels, frames);
}
}