mlua/types/
registry_key.rs1use std::hash::{Hash, Hasher};
2use std::os::raw::c_int;
3use std::sync::Arc;
4use std::{fmt, mem, ptr};
5
6use parking_lot::Mutex;
7
8pub struct RegistryKey {
26 pub(crate) registry_id: i32,
27 pub(crate) unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
28}
29
30impl fmt::Debug for RegistryKey {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 write!(f, "RegistryKey({})", self.id())
33 }
34}
35
36impl Hash for RegistryKey {
37 fn hash<H: Hasher>(&self, state: &mut H) {
38 self.id().hash(state)
39 }
40}
41
42impl PartialEq for RegistryKey {
43 fn eq(&self, other: &RegistryKey) -> bool {
44 self.id() == other.id() && Arc::ptr_eq(&self.unref_list, &other.unref_list)
45 }
46}
47
48impl Eq for RegistryKey {}
49
50impl Drop for RegistryKey {
51 fn drop(&mut self) {
52 let registry_id = self.id();
53 if registry_id > ffi::LUA_REFNIL {
55 let mut unref_list = self.unref_list.lock();
56 if let Some(list) = unref_list.as_mut() {
57 list.push(registry_id);
58 }
59 }
60 }
61}
62
63impl RegistryKey {
64 pub(crate) const fn new(id: c_int, unref_list: Arc<Mutex<Option<Vec<c_int>>>>) -> Self {
66 RegistryKey {
67 registry_id: id,
68 unref_list,
69 }
70 }
71
72 #[inline(always)]
74 pub fn id(&self) -> c_int {
75 self.registry_id
76 }
77
78 #[inline(always)]
80 pub(crate) fn set_id(&mut self, id: c_int) {
81 self.registry_id = id;
82 }
83
84 pub(crate) fn take(self) -> i32 {
86 let registry_id = self.id();
87 unsafe {
88 ptr::read(&self.unref_list);
89 mem::forget(self);
90 }
91 registry_id
92 }
93}
94
95#[cfg(test)]
96mod assertions {
97 use super::*;
98
99 static_assertions::assert_impl_all!(RegistryKey: Send, Sync);
100}