Skip to main content

melior/ir/attribute/
flat_symbol_ref.rs

1use super::{Attribute, AttributeLike};
2use crate::{Context, Error, StringRef};
3use mlir_sys::{mlirFlatSymbolRefAttrGet, mlirFlatSymbolRefAttrGetValue, MlirAttribute};
4
5/// A flat symbol ref attribute.
6#[derive(Clone, Copy)]
7pub struct FlatSymbolRefAttribute<'c> {
8    attribute: Attribute<'c>,
9}
10
11impl<'c> FlatSymbolRefAttribute<'c> {
12    /// Creates a flat symbol ref attribute.
13    pub fn new(context: &'c Context, symbol: &str) -> Self {
14        unsafe {
15            Self::from_raw(mlirFlatSymbolRefAttrGet(
16                context.to_raw(),
17                StringRef::new(symbol).to_raw(),
18            ))
19        }
20    }
21
22    /// Returns a value.
23    pub fn value(&self) -> &'c str {
24        unsafe { StringRef::from_raw(mlirFlatSymbolRefAttrGetValue(self.to_raw())) }
25            .as_str()
26            .unwrap()
27    }
28}
29
30attribute_traits!(
31    FlatSymbolRefAttribute,
32    is_flat_symbol_ref,
33    "flat symbol ref"
34);
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::test::create_test_context;
40
41    #[test]
42    fn new() {
43        let context = create_test_context();
44
45        FlatSymbolRefAttribute::new(&context, "foo");
46    }
47
48    #[test]
49    fn value() {
50        let context = create_test_context();
51        let value = FlatSymbolRefAttribute::new(&context, "foo").value();
52
53        assert_eq!(value, "foo");
54    }
55}