|
1 | 1 | use std::cell::UnsafeCell; |
2 | | -use std::hash::{Hash, Hasher}; |
| 2 | +use std::fmt; |
3 | 3 | use std::os::raw::{c_int, c_void}; |
4 | 4 | use std::rc::Rc; |
5 | | -use std::sync::Arc; |
6 | | -use std::{fmt, mem, ptr}; |
7 | | - |
8 | | -use parking_lot::Mutex; |
9 | 5 |
|
10 | 6 | use crate::error::Result; |
11 | 7 | #[cfg(not(feature = "luau"))] |
12 | 8 | use crate::hook::Debug; |
13 | 9 | use crate::state::{ExtraData, Lua, RawLua, WeakLua}; |
14 | 10 |
|
| 11 | +// Re-export mutex wrappers |
| 12 | +pub(crate) use sync::{ArcReentrantMutexGuard, ReentrantMutex, ReentrantMutexGuard, XRc, XWeak}; |
| 13 | + |
15 | 14 | #[cfg(all(feature = "async", feature = "send"))] |
16 | 15 | pub(crate) type BoxFuture<'a, T> = futures_util::future::BoxFuture<'a, T>; |
17 | 16 |
|
18 | 17 | #[cfg(all(feature = "async", not(feature = "send")))] |
19 | 18 | pub(crate) type BoxFuture<'a, T> = futures_util::future::LocalBoxFuture<'a, T>; |
20 | 19 |
|
21 | | -#[cfg(all(feature = "luau", feature = "serialize"))] |
22 | | -use serde::ser::{Serialize, SerializeTupleStruct, Serializer}; |
23 | | - |
24 | | -// Re-export mutex wrappers |
25 | 20 | 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; |
27 | 24 |
|
28 | 25 | /// Type of Lua integer numbers. |
29 | 26 | pub type Integer = ffi::lua_Integer; |
@@ -105,182 +102,8 @@ pub trait MaybeSend {} |
105 | 102 | #[cfg(not(feature = "send"))] |
106 | 103 | impl<T> MaybeSend for T {} |
107 | 104 |
|
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 | | - |
194 | 105 | pub(crate) struct DestructedUserdata; |
195 | 106 |
|
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 | | - |
284 | 107 | pub(crate) struct ValueRef { |
285 | 108 | pub(crate) lua: WeakLua, |
286 | 109 | pub(crate) index: c_int, |
@@ -338,14 +161,16 @@ impl PartialEq for ValueRef { |
338 | 161 | } |
339 | 162 |
|
340 | 163 | mod app_data; |
| 164 | +mod registry_key; |
341 | 165 | mod sync; |
342 | 166 |
|
| 167 | +#[cfg(any(feature = "luau", doc))] |
| 168 | +mod vector; |
| 169 | + |
343 | 170 | #[cfg(test)] |
344 | 171 | mod assertions { |
345 | 172 | use super::*; |
346 | 173 |
|
347 | | - static_assertions::assert_impl_all!(RegistryKey: Send, Sync); |
348 | | - |
349 | 174 | #[cfg(not(feature = "send"))] |
350 | 175 | static_assertions::assert_not_impl_any!(ValueRef: Send); |
351 | 176 | #[cfg(feature = "send")] |
|
0 commit comments