Implement Hash for RegistryKey. Closes #57

This commit is contained in:
Alex Orlenko 2021-06-16 12:12:42 +01:00
parent 9f0378b77e
commit 3b94b4e86f
2 changed files with 33 additions and 0 deletions

View File

@ -1,4 +1,5 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::hash::{Hash, Hasher};
use std::os::raw::{c_int, c_void}; use std::os::raw::{c_int, c_void};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::{fmt, mem, ptr}; use std::{fmt, mem, ptr};
@ -67,6 +68,20 @@ impl fmt::Debug for RegistryKey {
} }
} }
impl Hash for RegistryKey {
fn hash<H: Hasher>(&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 { impl Drop for RegistryKey {
fn drop(&mut self) { fn drop(&mut self) {
let mut unref_list = mlua_expect!(self.unref_list.lock(), "unref list poisoned"); let mut unref_list = mlua_expect!(self.unref_list.lock(), "unref list poisoned");

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::panic::{catch_unwind, AssertUnwindSafe}; use std::panic::{catch_unwind, AssertUnwindSafe};
use std::string::String as StdString; use std::string::String as StdString;
@ -797,6 +798,23 @@ fn test_drop_registry_value() -> Result<()> {
Ok(()) 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] #[test]
fn test_lua_registry_ownership() -> Result<()> { fn test_lua_registry_ownership() -> Result<()> {
let lua1 = Lua::new(); let lua1 = Lua::new();