pub trait Sample:
Copy
+ Default
+ Send
+ Sync
+ 'static
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ PartialOrd {
const ZERO: Self;
const ONE: Self;
// Required methods
fn from_f32(value: f32) -> Self;
fn to_f32(self) -> f32;
fn from_f64(value: f64) -> Self;
fn to_f64(self) -> f64;
fn abs(self) -> Self;
fn sqrt(self) -> Self;
fn sin(self) -> Self;
fn cos(self) -> Self;
fn min(self, other: Self) -> Self;
fn max(self, other: Self) -> Self;
// Provided method
fn clamp(self, min: Self, max: Self) -> Self { ... }
}Expand description
Trait for audio sample types (f32, f64).
Designed for zero-cost abstraction - all methods inline for monomorphization. Only includes operations commonly needed in audio DSP inner loops.
§Design Philosophy
- Minimal but complete: Only essential operations
- Inline everything: Trust LLVM to optimize
- Includes Div: For cleaner RMS/average calculations
§Example: Generic Gain Plugin
ⓘ
fn process_generic<S: Sample>(&mut self, buffer: &mut Buffer<S>) {
let gain = S::from_f32(self.params.gain_linear());
for (input, output) in buffer.zip_channels() {
for (i, o) in input.iter().zip(output.iter_mut()) {
*o = *i * gain;
}
}
}Required Associated Constants§
Required Methods§
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.