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
macro_rules! type_traits {
    ($name: ident, $is_type: ident, $string: expr) => {
        impl<'c> $name<'c> {
            unsafe fn from_raw(raw: MlirType) -> Self {
                Self {
                    r#type: Type::from_raw(raw),
                }
            }
        }

        impl<'c> TryFrom<crate::ir::r#type::Type<'c>> for $name<'c> {
            type Error = crate::Error;

            fn try_from(r#type: crate::ir::r#type::Type<'c>) -> Result<Self, Self::Error> {
                if r#type.$is_type() {
                    Ok(unsafe { Self::from_raw(r#type.to_raw()) })
                } else {
                    Err(Error::TypeExpected($string, r#type.to_string()))
                }
            }
        }

        impl<'c> crate::ir::r#type::TypeLike<'c> for $name<'c> {
            fn to_raw(&self) -> mlir_sys::MlirType {
                self.r#type.to_raw()
            }
        }

        impl<'c> std::fmt::Display for $name<'c> {
            fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                std::fmt::Display::fmt(&self.r#type, formatter)
            }
        }
    };
}