Skip to content

Commit

Permalink
Identifier API (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored Sep 14, 2022
1 parent 07930ad commit 2eeb5fc
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use crate::{
context::{Context, ContextRef},
string_ref::StringRef,
};
use mlir_sys::{mlirIdentifierGet, mlirIdentifierGetContext, MlirIdentifier};
use mlir_sys::{
mlirIdentifierEqual, mlirIdentifierGet, mlirIdentifierGetContext, mlirIdentifierStr,
MlirIdentifier,
};
use std::marker::PhantomData;

#[derive(Clone, Copy, Debug)]
pub struct Identifier<'c> {
raw: MlirIdentifier,
_context: PhantomData<&'c Context>,
Expand All @@ -22,11 +26,23 @@ impl<'c> Identifier<'c> {
unsafe { ContextRef::from_raw(mlirIdentifierGetContext(self.raw)) }
}

pub(crate) unsafe fn to_raw(&self) -> MlirIdentifier {
pub fn as_string_ref(&self) -> StringRef {
unsafe { StringRef::from_raw(mlirIdentifierStr(self.raw)) }
}

pub(crate) unsafe fn to_raw(self) -> MlirIdentifier {
self.raw
}
}

impl<'c> PartialEq for Identifier<'c> {
fn eq(&self, other: &Self) -> bool {
unsafe { mlirIdentifierEqual(self.raw, other.raw) }
}
}

impl<'c> Eq for Identifier<'c> {}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -40,4 +56,24 @@ mod tests {
fn context() {
Identifier::new(&Context::new(), "foo").context();
}

#[test]
fn equal() {
let context = Context::new();

assert_eq!(
Identifier::new(&context, "foo"),
Identifier::new(&context, "foo")
);
}

#[test]
fn not_equal() {
let context = Context::new();

assert_ne!(
Identifier::new(&context, "foo"),
Identifier::new(&context, "bar")
);
}
}

0 comments on commit 2eeb5fc

Please sign in to comment.