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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use super::TypeId;
use mlir_sys::{
    mlirTypeIDAllocatorAllocateTypeID, mlirTypeIDAllocatorCreate, mlirTypeIDAllocatorDestroy,
    MlirTypeIDAllocator,
};

/// A type ID allocator.
#[derive(Debug)]
pub struct Allocator {
    raw: MlirTypeIDAllocator,
}

impl Allocator {
    pub fn new() -> Self {
        Self {
            raw: unsafe { mlirTypeIDAllocatorCreate() },
        }
    }

    pub fn allocate_type_id(&mut self) -> TypeId {
        unsafe { TypeId::from_raw(mlirTypeIDAllocatorAllocateTypeID(self.raw)) }
    }
}

impl Drop for Allocator {
    fn drop(&mut self) {
        unsafe { mlirTypeIDAllocatorDestroy(self.raw) }
    }
}

impl Default for Allocator {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new() {
        Allocator::new();
    }

    #[test]
    fn allocate_type_id() {
        Allocator::new().allocate_type_id();
    }
}