Skip to content

Commit e3c5cfd

Browse files
committed
Extract registry_key and vector modules from types
1 parent 4977b91 commit e3c5cfd

3 files changed

Lines changed: 198 additions & 186 deletions

File tree

src/types.rs

Lines changed: 11 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
use std::cell::UnsafeCell;
2-
use std::hash::{Hash, Hasher};
2+
use std::fmt;
33
use std::os::raw::{c_int, c_void};
44
use std::rc::Rc;
5-
use std::sync::Arc;
6-
use std::{fmt, mem, ptr};
7-
8-
use parking_lot::Mutex;
95

106
use crate::error::Result;
117
#[cfg(not(feature = "luau"))]
128
use crate::hook::Debug;
139
use crate::state::{ExtraData, Lua, RawLua, WeakLua};
1410

11+
// Re-export mutex wrappers
12+
pub(crate) use sync::{ArcReentrantMutexGuard, ReentrantMutex, ReentrantMutexGuard, XRc, XWeak};
13+
1514
#[cfg(all(feature = "async", feature = "send"))]
1615
pub(crate) type BoxFuture<'a, T> = futures_util::future::BoxFuture<'a, T>;
1716

1817
#[cfg(all(feature = "async", not(feature = "send")))]
1918
pub(crate) type BoxFuture<'a, T> = futures_util::future::LocalBoxFuture<'a, T>;
2019

21-
#[cfg(all(feature = "luau", feature = "serialize"))]
22-
use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
23-
24-
// Re-export mutex wrappers
2520
pub use app_data::{AppData, AppDataRef, AppDataRefMut};
26-
pub(crate) use sync::{ArcReentrantMutexGuard, ReentrantMutex, ReentrantMutexGuard, XRc, XWeak};
21+
pub use registry_key::RegistryKey;
22+
#[cfg(any(feature = "luau", doc))]
23+
pub use vector::Vector;
2724

2825
/// Type of Lua integer numbers.
2926
pub type Integer = ffi::lua_Integer;
@@ -105,182 +102,8 @@ pub trait MaybeSend {}
105102
#[cfg(not(feature = "send"))]
106103
impl<T> MaybeSend for T {}
107104

108-
/// A Luau vector type.
109-
///
110-
/// By default vectors are 3-dimensional, but can be 4-dimensional
111-
/// if the `luau-vector4` feature is enabled.
112-
#[cfg(any(feature = "luau", doc))]
113-
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
114-
#[derive(Debug, Default, Clone, Copy, PartialEq)]
115-
pub struct Vector(pub(crate) [f32; Self::SIZE]);
116-
117-
#[cfg(any(feature = "luau", doc))]
118-
impl fmt::Display for Vector {
119-
#[rustfmt::skip]
120-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121-
#[cfg(not(feature = "luau-vector4"))]
122-
return write!(f, "vector({}, {}, {})", self.x(), self.y(), self.z());
123-
#[cfg(feature = "luau-vector4")]
124-
return write!(f, "vector({}, {}, {}, {})", self.x(), self.y(), self.z(), self.w());
125-
}
126-
}
127-
128-
#[cfg(any(feature = "luau", doc))]
129-
impl Vector {
130-
pub(crate) const SIZE: usize = if cfg!(feature = "luau-vector4") { 4 } else { 3 };
131-
132-
/// Creates a new vector.
133-
#[cfg(not(feature = "luau-vector4"))]
134-
pub const fn new(x: f32, y: f32, z: f32) -> Self {
135-
Self([x, y, z])
136-
}
137-
138-
/// Creates a new vector.
139-
#[cfg(feature = "luau-vector4")]
140-
pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
141-
Self([x, y, z, w])
142-
}
143-
144-
/// Creates a new vector with all components set to `0.0`.
145-
#[doc(hidden)]
146-
pub const fn zero() -> Self {
147-
Self([0.0; Self::SIZE])
148-
}
149-
150-
/// Returns 1st component of the vector.
151-
pub const fn x(&self) -> f32 {
152-
self.0[0]
153-
}
154-
155-
/// Returns 2nd component of the vector.
156-
pub const fn y(&self) -> f32 {
157-
self.0[1]
158-
}
159-
160-
/// Returns 3rd component of the vector.
161-
pub const fn z(&self) -> f32 {
162-
self.0[2]
163-
}
164-
165-
/// Returns 4th component of the vector.
166-
#[cfg(any(feature = "luau-vector4", doc))]
167-
#[cfg_attr(docsrs, doc(cfg(feature = "luau-vector4")))]
168-
pub const fn w(&self) -> f32 {
169-
self.0[3]
170-
}
171-
}
172-
173-
#[cfg(all(feature = "luau", feature = "serialize"))]
174-
impl Serialize for Vector {
175-
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
176-
let mut ts = serializer.serialize_tuple_struct("Vector", Self::SIZE)?;
177-
ts.serialize_field(&self.x())?;
178-
ts.serialize_field(&self.y())?;
179-
ts.serialize_field(&self.z())?;
180-
#[cfg(feature = "luau-vector4")]
181-
ts.serialize_field(&self.w())?;
182-
ts.end()
183-
}
184-
}
185-
186-
#[cfg(any(feature = "luau", doc))]
187-
impl PartialEq<[f32; Self::SIZE]> for Vector {
188-
#[inline]
189-
fn eq(&self, other: &[f32; Self::SIZE]) -> bool {
190-
self.0 == *other
191-
}
192-
}
193-
194105
pub(crate) struct DestructedUserdata;
195106

196-
/// An auto generated key into the Lua registry.
197-
///
198-
/// This is a handle to a value stored inside the Lua registry. It is not automatically
199-
/// garbage collected on Drop, but it can be removed with [`Lua::remove_registry_value`],
200-
/// and instances not manually removed can be garbage collected with
201-
/// [`Lua::expire_registry_values`].
202-
///
203-
/// Be warned, If you place this into Lua via a [`UserData`] type or a rust callback, it is *very
204-
/// easy* to accidentally cause reference cycles that the Lua garbage collector cannot resolve.
205-
/// Instead of placing a [`RegistryKey`] into a [`UserData`] type, prefer instead to use
206-
/// [`AnyUserData::set_user_value`] / [`AnyUserData::user_value`].
207-
///
208-
/// [`UserData`]: crate::UserData
209-
/// [`RegistryKey`]: crate::RegistryKey
210-
/// [`Lua::remove_registry_value`]: crate::Lua::remove_registry_value
211-
/// [`Lua::expire_registry_values`]: crate::Lua::expire_registry_values
212-
/// [`AnyUserData::set_user_value`]: crate::AnyUserData::set_user_value
213-
/// [`AnyUserData::user_value`]: crate::AnyUserData::user_value
214-
pub struct RegistryKey {
215-
pub(crate) registry_id: i32,
216-
pub(crate) unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
217-
}
218-
219-
impl fmt::Debug for RegistryKey {
220-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
221-
write!(f, "RegistryKey({})", self.id())
222-
}
223-
}
224-
225-
impl Hash for RegistryKey {
226-
fn hash<H: Hasher>(&self, state: &mut H) {
227-
self.id().hash(state)
228-
}
229-
}
230-
231-
impl PartialEq for RegistryKey {
232-
fn eq(&self, other: &RegistryKey) -> bool {
233-
self.id() == other.id() && Arc::ptr_eq(&self.unref_list, &other.unref_list)
234-
}
235-
}
236-
237-
impl Eq for RegistryKey {}
238-
239-
impl Drop for RegistryKey {
240-
fn drop(&mut self) {
241-
let registry_id = self.id();
242-
// We don't need to collect nil slot
243-
if registry_id > ffi::LUA_REFNIL {
244-
let mut unref_list = self.unref_list.lock();
245-
if let Some(list) = unref_list.as_mut() {
246-
list.push(registry_id);
247-
}
248-
}
249-
}
250-
}
251-
252-
impl RegistryKey {
253-
/// Creates a new instance of `RegistryKey`
254-
pub(crate) const fn new(id: c_int, unref_list: Arc<Mutex<Option<Vec<c_int>>>>) -> Self {
255-
RegistryKey {
256-
registry_id: id,
257-
unref_list,
258-
}
259-
}
260-
261-
/// Returns the underlying Lua reference of this `RegistryKey`
262-
#[inline(always)]
263-
pub fn id(&self) -> c_int {
264-
self.registry_id
265-
}
266-
267-
/// Sets the unique Lua reference key of this `RegistryKey`
268-
#[inline(always)]
269-
pub(crate) fn set_id(&mut self, id: c_int) {
270-
self.registry_id = id;
271-
}
272-
273-
/// Destroys the `RegistryKey` without adding to the unref list
274-
pub(crate) fn take(self) -> i32 {
275-
let registry_id = self.id();
276-
unsafe {
277-
ptr::read(&self.unref_list);
278-
mem::forget(self);
279-
}
280-
registry_id
281-
}
282-
}
283-
284107
pub(crate) struct ValueRef {
285108
pub(crate) lua: WeakLua,
286109
pub(crate) index: c_int,
@@ -338,14 +161,16 @@ impl PartialEq for ValueRef {
338161
}
339162

340163
mod app_data;
164+
mod registry_key;
341165
mod sync;
342166

167+
#[cfg(any(feature = "luau", doc))]
168+
mod vector;
169+
343170
#[cfg(test)]
344171
mod assertions {
345172
use super::*;
346173

347-
static_assertions::assert_impl_all!(RegistryKey: Send, Sync);
348-
349174
#[cfg(not(feature = "send"))]
350175
static_assertions::assert_not_impl_any!(ValueRef: Send);
351176
#[cfg(feature = "send")]

src/types/registry_key.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use std::hash::{Hash, Hasher};
2+
use std::os::raw::c_int;
3+
use std::sync::Arc;
4+
use std::{fmt, mem, ptr};
5+
6+
use parking_lot::Mutex;
7+
8+
/// An auto generated key into the Lua registry.
9+
///
10+
/// This is a handle to a value stored inside the Lua registry. It is not automatically
11+
/// garbage collected on Drop, but it can be removed with [`Lua::remove_registry_value`],
12+
/// and instances not manually removed can be garbage collected with
13+
/// [`Lua::expire_registry_values`].
14+
///
15+
/// Be warned, If you place this into Lua via a [`UserData`] type or a rust callback, it is *very
16+
/// easy* to accidentally cause reference cycles that the Lua garbage collector cannot resolve.
17+
/// Instead of placing a [`RegistryKey`] into a [`UserData`] type, prefer instead to use
18+
/// [`AnyUserData::set_user_value`] / [`AnyUserData::user_value`].
19+
///
20+
/// [`UserData`]: crate::UserData
21+
/// [`RegistryKey`]: crate::RegistryKey
22+
/// [`Lua::remove_registry_value`]: crate::Lua::remove_registry_value
23+
/// [`Lua::expire_registry_values`]: crate::Lua::expire_registry_values
24+
/// [`AnyUserData::set_user_value`]: crate::AnyUserData::set_user_value
25+
/// [`AnyUserData::user_value`]: crate::AnyUserData::user_value
26+
pub struct RegistryKey {
27+
pub(crate) registry_id: i32,
28+
pub(crate) unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
29+
}
30+
31+
impl fmt::Debug for RegistryKey {
32+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33+
write!(f, "RegistryKey({})", self.id())
34+
}
35+
}
36+
37+
impl Hash for RegistryKey {
38+
fn hash<H: Hasher>(&self, state: &mut H) {
39+
self.id().hash(state)
40+
}
41+
}
42+
43+
impl PartialEq for RegistryKey {
44+
fn eq(&self, other: &RegistryKey) -> bool {
45+
self.id() == other.id() && Arc::ptr_eq(&self.unref_list, &other.unref_list)
46+
}
47+
}
48+
49+
impl Eq for RegistryKey {}
50+
51+
impl Drop for RegistryKey {
52+
fn drop(&mut self) {
53+
let registry_id = self.id();
54+
// We don't need to collect nil slot
55+
if registry_id > ffi::LUA_REFNIL {
56+
let mut unref_list = self.unref_list.lock();
57+
if let Some(list) = unref_list.as_mut() {
58+
list.push(registry_id);
59+
}
60+
}
61+
}
62+
}
63+
64+
impl RegistryKey {
65+
/// Creates a new instance of `RegistryKey`
66+
pub(crate) const fn new(id: c_int, unref_list: Arc<Mutex<Option<Vec<c_int>>>>) -> Self {
67+
RegistryKey {
68+
registry_id: id,
69+
unref_list,
70+
}
71+
}
72+
73+
/// Returns the underlying Lua reference of this `RegistryKey`
74+
#[inline(always)]
75+
pub fn id(&self) -> c_int {
76+
self.registry_id
77+
}
78+
79+
/// Sets the unique Lua reference key of this `RegistryKey`
80+
#[inline(always)]
81+
pub(crate) fn set_id(&mut self, id: c_int) {
82+
self.registry_id = id;
83+
}
84+
85+
/// Destroys the `RegistryKey` without adding to the unref list
86+
pub(crate) fn take(self) -> i32 {
87+
let registry_id = self.id();
88+
unsafe {
89+
ptr::read(&self.unref_list);
90+
mem::forget(self);
91+
}
92+
registry_id
93+
}
94+
}
95+
96+
#[cfg(test)]
97+
mod assertions {
98+
use super::*;
99+
100+
static_assertions::assert_impl_all!(RegistryKey: Send, Sync);
101+
}

0 commit comments

Comments
 (0)