glsl-layout 0.3.0

Provides data types and traits to build structures ready to upload into UBO.
Documentation
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);