Skip to main content

melior/ir/attribute/
string.rs

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