1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2024 Google LLC
// SPDX-License-Identifier: MIT

use super::formats;
use std::{ffi, fmt, io, num, ptr, result};

#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
    #[error("no support")]
    NoSupport,
    #[error("invalid parameter")]
    InvalidParam,
    #[error("device io")]
    DeviceIo(#[from] io::Error),
    #[error("loading error")]
    LoadingError,
}

impl From<ffi::NulError> for Error {
    fn from(err: ffi::NulError) -> Self {
        Self::from(io::Error::from(err))
    }
}

impl From<num::TryFromIntError> for Error {
    fn from(_err: num::TryFromIntError) -> Self {
        Self::InvalidParam
    }
}

impl From<nix::Error> for Error {
    fn from(err: nix::Error) -> Self {
        Self::from(io::Error::from(err))
    }
}

impl From<ash::LoadingError> for Error {
    fn from(_err: ash::LoadingError) -> Self {
        Self::LoadingError
    }
}

impl From<ash::vk::Result> for Error {
    fn from(_err: ash::vk::Result) -> Self {
        Self::LoadingError
    }
}

pub(crate) type Result<T> = result::Result<T, Error>;

pub type Size = u64;

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct Format(pub u32);

impl Format {
    pub fn new(val: u32) -> Self {
        Self(val)
    }

    pub fn is_invalid(&self) -> bool {
        *self == formats::INVALID
    }
}

impl Default for Format {
    fn default() -> Self {
        formats::INVALID
    }
}

impl<T> From<T> for Format
where
    T: Into<u32>,
{
    fn from(val: T) -> Self {
        Self::new(val.into())
    }
}

impl fmt::Display for Format {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(name) = formats::name(*self) {
            write!(f, "{}", name)
        } else {
            write!(f, "{}", formats::fourcc(*self))
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct Modifier(pub u64);

impl Modifier {
    pub fn new(val: u64) -> Self {
        Self(val)
    }

    pub(crate) fn is_invalid(&self) -> bool {
        *self == formats::MOD_INVALID
    }

    pub(crate) fn is_linear(&self) -> bool {
        *self == formats::MOD_LINEAR
    }
}

impl Default for Modifier {
    fn default() -> Self {
        formats::MOD_INVALID
    }
}

impl<T> From<T> for Modifier
where
    T: Into<u64>,
{
    fn from(val: T) -> Self {
        Self::new(val.into())
    }
}

#[non_exhaustive]
pub enum Access {
    Read,
    Write,
    ReadWrite,
}

#[derive(Clone, Copy)]
pub struct Mapping {
    pub ptr: ptr::NonNull<ffi::c_void>,
    pub len: num::NonZeroUsize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format() {
        assert_eq!(Format::default(), formats::INVALID);
    }

    #[test]
    fn modifier() {
        assert_eq!(Modifier::default(), formats::MOD_INVALID);
    }
}