Skip to main content

melior/ir/attribute/
bool.rs

1use super::{Attribute, AttributeLike};
2use crate::{Context, Error};
3use mlir_sys::{mlirBoolAttrGet, mlirBoolAttrGetValue, MlirAttribute};
4
5/// A bool attribute.
6#[derive(Clone, Copy)]
7pub struct BoolAttribute<'c> {
8    attribute: Attribute<'c>,
9}
10
11impl<'c> BoolAttribute<'c> {
12    /// Creates a bool attribute.
13    pub fn new(context: &'c Context, boolean: bool) -> Self {
14        unsafe {
15            Self::from_raw(mlirBoolAttrGet(
16                context.to_raw(),
17                if boolean { 1 } else { 0 },
18            ))
19        }
20    }
21
22    /// Returns a value.
23    pub fn value(&self) -> bool {
24        unsafe { mlirBoolAttrGetValue(self.to_raw()) }
25    }
26}
27
28attribute_traits!(BoolAttribute, is_string, "string");
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::test::create_test_context;
34
35    #[test]
36    fn value() {
37        let context = create_test_context();
38        let value = BoolAttribute::new(&context, true).value();
39
40        assert!(value);
41    }
42}