spirv_std/
integer.rs

1//! Traits related to integers.
2
3/// Abstract trait representing any SPIR-V integer type.
4///
5/// # Safety
6/// Implementing this trait on non-primitive-integer types breaks assumptions of other unsafe code,
7/// and should not be done.
8pub unsafe trait Integer: num_traits::PrimInt + crate::scalar::Scalar {
9    /// Width of the integer, in bits.
10    const WIDTH: usize;
11    /// If the integer is signed: true means signed, false means unsigned.
12    const SIGNED: bool;
13}
14
15/// A trait for being generic over signed integer types.
16pub trait SignedInteger: Integer {}
17/// A trait for being generic over unsigned integer types.
18pub trait UnsignedInteger: Integer {}
19
20macro_rules! impl_numbers {
21    (impl UnsignedInteger for $typ:ty;) => {
22        unsafe impl Integer for $typ {
23            const WIDTH: usize = core::mem::size_of::<$typ>() * 8;
24            const SIGNED: bool = false;
25        }
26
27        impl UnsignedInteger for $typ {}
28    };
29    (impl SignedInteger for $typ:ty;) => {
30        unsafe impl Integer for $typ {
31            const WIDTH: usize = core::mem::size_of::<$typ>() * 8;
32            const SIGNED: bool = true;
33        }
34
35        impl SignedInteger for $typ {}
36    };
37    ($(impl $trait:ident for $typ:ty;)+) => {
38        $(impl_numbers!(impl $trait for $typ;);)+
39    };
40
41}
42
43impl_numbers! {
44    impl UnsignedInteger for u8;
45    impl UnsignedInteger for u16;
46    impl UnsignedInteger for u32;
47    impl UnsignedInteger for u64;
48    impl SignedInteger for i8;
49    impl SignedInteger for i16;
50    impl SignedInteger for i32;
51    impl SignedInteger for i64;
52}