Paste ChBMj

Submitted on 10 March 2023 at 23:45 UTC
803 views

New Paste 1 (rust)

use std::marker::PhantomData;

pub struct RgbFormat;
pub struct RgbaFormat;

pub struct Image<Format = RgbFormat> {
    pub data: Vec<u8>,
    pub width: usize,
    pub height: usize,
    format: PhantomData<Format>,
}

trait ComponentsCount {
    const COMPONENTS_COUNT: usize;
}

impl ComponentsCount for Image<RgbFormat> {
    const COMPONENTS_COUNT: usize = 3;
}

impl ComponentsCount for Image<RgbaFormat> {
    const COMPONENTS_COUNT: usize = 4;
}

trait Test {
    fn get_components_count() -> usize;
    // fn get_pixel(&self, x: usize, y: usize) -> &[u8; Self::COMPONENTS_COUNT];
    // fn set_pixel(&self, x: usize, y: usize, pixel: [u8; Self::COMPONENTS_COUNT]);
}

impl<T: ComponentsCount> Test for T {
    fn get_components_count() -> usize {
        Self::COMPONENTS_COUNT
    }
}

Raw · Download

Pastee