Skip to main content

dispatch_sample

Macro dispatch_sample 

Source
macro_rules! dispatch_sample {
    ($kind:expr, $ty:ident => $body:block) => { ... };
}
Expand description

Dispatch once on a SampleKind into a kernel generic over Sample.

This is the per-tile dispatch of ADR-0002: the match runs once, the body is instantiated once per sample type, and the inner loop is fully specialized.

The macro binds a type alias (conventionally S) inside the body:

use otf_pixels_core::{dispatch_sample, PixelFormat, Sample};

fn full_scale_as_f64(format: PixelFormat) -> f64 {
    dispatch_sample!(format.sample_kind(), S => {
        // `S` is a concrete type here: u8, u16, or f32.
        fn widen<T: Sample + Into<f64>>(v: T) -> f64 { v.into() }
        widen(<S as Sample>::FULL_SCALE)
    })
}

assert_eq!(full_scale_as_f64(PixelFormat::Rgb8), 255.0);
assert_eq!(full_scale_as_f64(PixelFormat::Rgb16), 65535.0);
assert_eq!(full_scale_as_f64(PixelFormat::RgbF32), 1.0);