air-macros 0.3.0

Macros for the TNJ Assembly Intermediate Representation (AIR)
Documentation
#![deny(missing_docs)]
//! A utility crate defining macros for other crates to use.

/// A macro to define an index struct, i.e. a struct that is a integer referencing the actual value
/// in a pool.
#[macro_export]
macro_rules! index_struct {
    {$(#[doc = $doc:expr])* $name:ident($index_ty:ty)} => {
        #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
        $(#[doc = $doc])*
        pub struct $name(pub $index_ty);

        impl From<$name> for usize {
            fn from(value: $name) -> Self {
                value.0 as usize
            }
        }

        impl TryFrom<usize> for $name {
            type Error = ();

            fn try_from(value: usize) -> Result<Self, Self::Error> {
                if value > <$index_ty>::MAX as usize {
                    Err(())
                } else {
                    Ok(Self(value as $index_ty))
                }
            }
        }
    };
}