From 2eeb5fcca6ee9ec924edd24782f319ec9ee84fbc Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Tue, 13 Sep 2022 22:19:38 -0700 Subject: [PATCH] Identifier API (#26) --- src/identifier.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/identifier.rs b/src/identifier.rs index b962251a6f..264c943111 100644 --- a/src/identifier.rs +++ b/src/identifier.rs @@ -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>, @@ -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::*; @@ -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") + ); + } }