1mod allocator;
4
5pub use allocator::Allocator;
6use mlir_sys::{mlirTypeIDCreate, mlirTypeIDEqual, mlirTypeIDHashValue, MlirTypeID};
7use std::{
8 hash::{Hash, Hasher},
9 marker::PhantomData,
10};
11
12#[derive(Clone, Copy, Debug)]
14pub struct TypeId<'c> {
15 raw: MlirTypeID,
16 _owner: PhantomData<&'c ()>,
17}
18
19impl TypeId<'_> {
20 pub const unsafe fn from_raw(raw: MlirTypeID) -> Self {
26 Self {
27 raw,
28 _owner: PhantomData,
29 }
30 }
31
32 pub const fn to_raw(self) -> MlirTypeID {
34 self.raw
35 }
36
37 pub fn create<T>(reference: &T) -> Self {
44 let ptr = reference as *const _ as *const std::ffi::c_void;
45
46 assert_eq!(
47 ptr.align_offset(8),
48 0,
49 "type ID pointer must be 8-byte aligned"
50 );
51
52 unsafe { Self::from_raw(mlirTypeIDCreate(ptr)) }
53 }
54}
55
56impl PartialEq for TypeId<'_> {
57 fn eq(&self, other: &Self) -> bool {
58 unsafe { mlirTypeIDEqual(self.raw, other.raw) }
59 }
60}
61
62impl Eq for TypeId<'_> {}
63
64impl Hash for TypeId<'_> {
65 fn hash<H: Hasher>(&self, hasher: &mut H) {
66 unsafe {
67 mlirTypeIDHashValue(self.raw).hash(hasher);
68 }
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn create_from_reference() {
78 static VALUE: u64 = 0;
79
80 TypeId::create(&VALUE);
81 }
82
83 #[test]
84 #[should_panic]
85 fn reject_invalid_alignment() {
86 static VALUES: [u8; 2] = [1u8; 2];
87
88 TypeId::create(&VALUES[1]);
89 }
90}