use core::fmt::Display;
use bytemuck::{Pod, Zeroable};
pub trait Element: Display + Copy + Clone + 'static {
const NATIVE_SIZE: usize = core::mem::size_of::<Self::Native>();
type Native: Default + Copy + Pod + Zeroable;
#[must_use]
fn wgsl_type() -> &'static str;
#[must_use]
fn wgsl_zero() -> &'static str;
#[must_use]
fn wgsl_one() -> &'static str;
#[must_use]
fn wgsl_max() -> &'static str;
#[must_use]
fn wgsl_min() -> &'static str;
#[must_use]
fn from_native(native: Self::Native) -> Self;
#[must_use]
fn to_native(self) -> Self::Native;
#[must_use]
fn zeroed() -> Self {
Self::from_native(Self::Native::zeroed())
}
}
impl Element for f32 {
type Native = f32;
#[inline]
fn wgsl_type() -> &'static str {
"f32"
}
#[inline]
fn wgsl_zero() -> &'static str {
"0.0"
}
#[inline]
fn wgsl_one() -> &'static str {
"1.0"
}
#[inline]
fn wgsl_max() -> &'static str {
"3.402823466e+38"
}
#[inline]
fn wgsl_min() -> &'static str {
"-3.402823466e+38"
}
#[inline]
fn from_native(native: Self) -> Self {
native
}
#[inline]
fn to_native(self) -> Self {
self
}
}
impl Element for i32 {
type Native = i32;
#[inline]
fn wgsl_type() -> &'static str {
"i32"
}
#[inline]
fn wgsl_zero() -> &'static str {
"0i"
}
#[inline]
fn wgsl_one() -> &'static str {
"1i"
}
#[inline]
fn wgsl_max() -> &'static str {
"0x7fffffffi"
}
#[inline]
fn wgsl_min() -> &'static str {
"(-0x7fffffffi - 1i)"
}
#[inline]
fn from_native(native: Self) -> Self {
native
}
#[inline]
fn to_native(self) -> Self {
self
}
}
impl Element for u32 {
type Native = u32;
#[inline]
fn wgsl_type() -> &'static str {
"u32"
}
#[inline]
fn wgsl_zero() -> &'static str {
"0u"
}
#[inline]
fn wgsl_one() -> &'static str {
"1u"
}
#[inline]
fn wgsl_max() -> &'static str {
"0xffffffffu"
}
#[inline]
fn wgsl_min() -> &'static str {
"0u"
}
#[inline]
fn from_native(native: Self) -> Self {
native
}
#[inline]
fn to_native(self) -> Self {
self
}
}
impl Element for bool {
type Native = u32;
#[inline]
fn wgsl_type() -> &'static str {
"u32"
}
#[inline]
fn wgsl_zero() -> &'static str {
"0u"
}
#[inline]
fn wgsl_one() -> &'static str {
"1u"
}
#[inline]
fn wgsl_max() -> &'static str {
"0xffffffffu"
}
#[inline]
fn wgsl_min() -> &'static str {
"0u"
}
#[inline]
fn from_native(native: u32) -> Self {
native != 0
}
#[inline]
fn to_native(self) -> u32 {
u32::from(self)
}
}
pub trait NumericElement: Element {}
impl NumericElement for f32 {}
impl NumericElement for i32 {}
impl NumericElement for u32 {}
pub trait SignedElement: Element {}
impl SignedElement for f32 {}
impl SignedElement for i32 {}
pub trait IntegerElement: Element {}
impl IntegerElement for i32 {}
impl IntegerElement for u32 {}
pub trait FloatElement: Element {}
impl FloatElement for f32 {}
pub trait LogicalElement: Element {}
impl LogicalElement for bool {}