Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ContextRef #352

Merged
merged 8 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions melior/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ impl Context {
unsafe { mlirContextDetachDiagnosticHandler(self.to_raw(), id.to_raw()) }
}

pub(crate) fn to_ref(&self) -> ContextRef {
unsafe { ContextRef::from_raw(self.to_raw()) }
}

pub(crate) fn string_cache(&self) -> &DashMap<CString, ()> {
&self.string_cache
}
Expand All @@ -150,6 +154,12 @@ impl PartialEq for Context {
}
}

impl<'a> PartialEq<ContextRef<'a>> for Context {
fn eq(&self, &other: &ContextRef<'a>) -> bool {
self.to_ref() == other
}
}

impl Eq for Context {}

/// A reference to a context.
Expand All @@ -173,13 +183,19 @@ impl<'c> ContextRef<'c> {
}
}

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

impl<'a> Eq for ContextRef<'a> {}
impl<'c> PartialEq<Context> for ContextRef<'c> {
fn eq(&self, other: &Context) -> bool {
self == &other.to_ref()
}
}

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

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -266,4 +282,36 @@ mod tests {

context.detach_diagnostic_handler(id);
}

#[test]
fn compare_contexts() {
let one = Context::new();
let other = Context::new();

assert_eq!(&one, &one);
assert_ne!(&one, &other);
assert_ne!(&other, &one);
assert_eq!(&other, &other);
}

#[test]
fn compare_context_refs() {
let one = Context::new();
let other = Context::new();

let one_ref = one.to_ref();
let other_ref = other.to_ref();

assert_eq!(&one, &one_ref);
assert_eq!(&one_ref, &one);

assert_eq!(&other, &other_ref);
assert_eq!(&other_ref, &other);

assert_ne!(&one, &other_ref);
assert_ne!(&other_ref, &one);

assert_ne!(&other, &one_ref);
assert_ne!(&one_ref, &other);
}
}