Skip to main content

mindus/utils/image/
holder.rs

1use super::Image;
2
3#[derive(Clone, Debug)]
4pub enum Cow {
5    Ref(&'static [u8]),
6    Own(Box<[u8]>),
7}
8impl From<&'static [u8]> for Cow {
9    fn from(value: &'static [u8]) -> Self {
10        Self::Ref(value)
11    }
12}
13impl From<Box<[u8]>> for Cow {
14    fn from(value: Box<[u8]>) -> Self {
15        Self::Own(value)
16    }
17}
18
19impl Cow {
20    pub fn own(self) -> Box<[u8]> {
21        match self {
22            Self::Own(x) => x,
23            Self::Ref(x) => x.into(),
24        }
25    }
26}
27impl AsRef<[u8]> for Cow {
28    fn as_ref(&self) -> &[u8] {
29        match self {
30            Cow::Ref(x) => x,
31            Cow::Own(x) => &x,
32        }
33    }
34}
35
36impl AsMut<[u8]> for Cow {
37    fn as_mut(&mut self) -> &mut [u8] {
38        match self {
39            Cow::Ref(x) => {
40                *self = Self::Own((*x).into());
41                self.as_mut()
42            }
43            Cow::Own(x) => x,
44        }
45    }
46}
47
48pub type ImageHolder<const CHANNELS: usize> = Image<Cow, CHANNELS>;