From 3b94b4e86f2238ab0bc6c8b1d2b9dc8802eb5bef Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Wed, 16 Jun 2021 12:12:42 +0100 Subject: [PATCH] Implement Hash for RegistryKey. Closes #57 --- src/types.rs | 15 +++++++++++++++ tests/tests.rs | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/types.rs b/src/types.rs index b66ea8f..6e21af1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::hash::{Hash, Hasher}; use std::os::raw::{c_int, c_void}; use std::sync::{Arc, Mutex}; use std::{fmt, mem, ptr}; @@ -67,6 +68,20 @@ impl fmt::Debug for RegistryKey { } } +impl Hash for RegistryKey { + fn hash(&self, state: &mut H) { + self.registry_id.hash(state) + } +} + +impl PartialEq for RegistryKey { + fn eq(&self, other: &RegistryKey) -> bool { + self.registry_id == other.registry_id && Arc::ptr_eq(&self.unref_list, &other.unref_list) + } +} + +impl Eq for RegistryKey {} + impl Drop for RegistryKey { fn drop(&mut self) { let mut unref_list = mlua_expect!(self.unref_list.lock(), "unref list poisoned"); diff --git a/tests/tests.rs b/tests/tests.rs index 6925a08..6a786ee 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::iter::FromIterator; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::string::String as StdString; @@ -797,6 +798,23 @@ fn test_drop_registry_value() -> Result<()> { Ok(()) } +#[test] +fn test_lua_registry_hash() -> Result<()> { + let lua = Lua::new(); + + let r1 = Arc::new(lua.create_registry_value("value1")?); + let r2 = Arc::new(lua.create_registry_value("value2")?); + + let mut map = HashMap::new(); + map.insert(r1.clone(), "value1"); + map.insert(r2.clone(), "value2"); + + assert_eq!(map[&r1], "value1"); + assert_eq!(map[&r2], "value2"); + + Ok(()) +} + #[test] fn test_lua_registry_ownership() -> Result<()> { let lua1 = Lua::new();