Implement Hash for RegistryKey. Closes #57
This commit is contained in:
parent
9f0378b77e
commit
3b94b4e86f
15
src/types.rs
15
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<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 {
|
||||
fn drop(&mut self) {
|
||||
let mut unref_list = mlua_expect!(self.unref_list.lock(), "unref list poisoned");
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue