pub unsafe trait Adapter<T> {
// Required methods
unsafe fn read_sample_unchecked(&self, channel: usize, frame: usize) -> T;
fn channels(&self) -> usize;
fn frames(&self) -> usize;
// Provided methods
fn read_sample(&self, channel: usize, frame: usize) -> Option<T> { ... }
fn copy_from_channel_to_slice(
&self,
channel: usize,
skip: usize,
slice: &mut [T],
) -> usize { ... }
fn copy_from_frame_to_slice(
&self,
frame: usize,
skip: usize,
slice: &mut [T],
) -> usize { ... }
}Expand description
A trait for reading samples from a buffer.
Samples are accessed indirectly by a read_sample method.
Implementations may perform any needed transformation
of the sample value before returning it.
§Safety
This trait is unsafe because it relies on correct implementations of the
frames and channels methods.
Users of an Adapter trait object must be able to rely on these values
being constant while the buffer is in use.
The values returned by these methods are not allowed to change spontaneously,
for example due to some internal logic or because of some action in another thread.
The provided safe helper methods (such as read_sample and the copy methods)
use these bounds before calling read_sample_unchecked internally.
If the reported bounds are wrong, those internal unchecked accesses can become
out of bounds and cause undefined behavior.
Required Methods§
Sourceunsafe fn read_sample_unchecked(&self, channel: usize, frame: usize) -> T
unsafe fn read_sample_unchecked(&self, channel: usize, frame: usize) -> T
Read the sample at a given combination of frame and channel.
§Safety
This method performs no bounds checking.
The caller must ensure that channel < self.channels() and
frame < self.frames().
Violating these preconditions causes undefined behavior.
Provided Methods§
Sourcefn read_sample(&self, channel: usize, frame: usize) -> Option<T>
fn read_sample(&self, channel: usize, frame: usize) -> Option<T>
Read the sample at
a given combination of frame and channel.
Returns None if the frame or channel is
out of bounds of the buffer.
Sourcefn copy_from_channel_to_slice(
&self,
channel: usize,
skip: usize,
slice: &mut [T],
) -> usize
fn copy_from_channel_to_slice( &self, channel: usize, skip: usize, slice: &mut [T], ) -> usize
Copy values from a channel of self to a slice.
The skip argument is the offset in samples from
where the first value will be copied.
If the target slice is longer than the available number of values in the channel,
then only the available number of samples will be copied.
Returns the number of values copied.
If an invalid channel number is given,
or if skip is larger than the length of the channel,
no samples will be copied and zero is returned.
Sourcefn copy_from_frame_to_slice(
&self,
frame: usize,
skip: usize,
slice: &mut [T],
) -> usize
fn copy_from_frame_to_slice( &self, frame: usize, skip: usize, slice: &mut [T], ) -> usize
Copy values from a frame of self to a slice.
The skip argument is the offset in samples from
where the first value will be copied.
If the slice is longer than the available number of values in the frame,
then only the available number of samples will be copied.
Returns the number of values copied.
If an invalid frame number is given,
or if skip is larger than the length of the frame,
no samples will be copied and zero is returned.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".