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
use align::{Align4, Align8};
use std140::{Std140, AsStd140};

macro_rules! impl_scalar {
    ($type:ty : $align:tt) => {
        unsafe impl Std140 for $type {}

        unsafe impl AsStd140 for $type {
            type Align = $align;
            type Std140 = $type;

            fn std140(&self) -> $type {
                *self
            }
        }
    }
}

/// Boolean value.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialOrd, PartialEq, Ord, Eq, Hash)]
pub struct boolean(u32);
impl_scalar!(boolean : Align4);

impl boolean {
    /// Create `boolean` from `bool`.
    pub fn new(value: bool) -> Self {
        value.into()
    }
}

impl From<bool> for boolean {
    fn from(value: bool) -> Self {
        boolean(value as u32)
    }
}

impl From<boolean> for bool {
    fn from(value: boolean) -> Self {
        if value.0 == 0 {
            false
        } else {
            true
        }
    }
}

/// Signed integer value.
pub type int = i32;
impl_scalar!(int : Align4);

/// Unsigned integer value.
pub type uint = u32;
impl_scalar!(uint : Align4);

/// floating-point value.
pub type float = f32;
impl_scalar!(float : Align4);

/// Double-precision floating-point value.
pub type double = f64;
impl_scalar!(double : Align8);