Skip to content

Commit 16951e3

Browse files
committed
Move ValueRef to a new module
1 parent 8bb2b44 commit 16951e3

File tree

2 files changed

+74
-68
lines changed

2 files changed

+74
-68
lines changed

src/types.rs

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::cell::UnsafeCell;
2-
use std::fmt;
32
use std::os::raw::{c_int, c_void};
43
use std::rc::Rc;
54

65
use crate::error::Result;
76
#[cfg(not(feature = "luau"))]
87
use crate::hook::Debug;
9-
use crate::state::{ExtraData, Lua, RawLua, WeakLua};
8+
use crate::state::{ExtraData, Lua, RawLua};
109

1110
// Re-export mutex wrappers
1211
pub(crate) use sync::{ArcReentrantMutexGuard, ReentrantMutex, ReentrantMutexGuard, XRc, XWeak};
@@ -19,6 +18,7 @@ pub(crate) type BoxFuture<'a, T> = futures_util::future::LocalBoxFuture<'a, T>;
1918

2019
pub use app_data::{AppData, AppDataRef, AppDataRefMut};
2120
pub use registry_key::RegistryKey;
21+
pub(crate) use value_ref::ValueRef;
2222
#[cfg(any(feature = "luau", doc))]
2323
pub use vector::Vector;
2424

@@ -115,75 +115,10 @@ impl<T> MaybeSend for T {}
115115

116116
pub(crate) struct DestructedUserdata;
117117

118-
pub(crate) struct ValueRef {
119-
pub(crate) lua: WeakLua,
120-
pub(crate) index: c_int,
121-
pub(crate) drop: bool,
122-
}
123-
124-
impl ValueRef {
125-
#[inline]
126-
pub(crate) fn new(lua: &RawLua, index: c_int) -> Self {
127-
ValueRef {
128-
lua: lua.weak().clone(),
129-
index,
130-
drop: true,
131-
}
132-
}
133-
134-
#[inline]
135-
pub(crate) fn to_pointer(&self) -> *const c_void {
136-
let lua = self.lua.lock();
137-
unsafe { ffi::lua_topointer(lua.ref_thread(), self.index) }
138-
}
139-
140-
/// Returns a copy of the value, which is valid as long as the original value is held.
141-
#[inline]
142-
pub(crate) fn copy(&self) -> Self {
143-
ValueRef {
144-
lua: self.lua.clone(),
145-
index: self.index,
146-
drop: false,
147-
}
148-
}
149-
}
150-
151-
impl fmt::Debug for ValueRef {
152-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153-
write!(f, "Ref({:p})", self.to_pointer())
154-
}
155-
}
156-
157-
impl Clone for ValueRef {
158-
fn clone(&self) -> Self {
159-
unsafe { self.lua.lock().clone_ref(self) }
160-
}
161-
}
162-
163-
impl Drop for ValueRef {
164-
fn drop(&mut self) {
165-
if self.drop {
166-
if let Some(lua) = self.lua.try_lock() {
167-
unsafe { lua.drop_ref(self) };
168-
}
169-
}
170-
}
171-
}
172-
173-
impl PartialEq for ValueRef {
174-
fn eq(&self, other: &Self) -> bool {
175-
assert!(
176-
self.lua == other.lua,
177-
"Lua instance passed Value created from a different main Lua state"
178-
);
179-
let lua = self.lua.lock();
180-
unsafe { ffi::lua_rawequal(lua.ref_thread(), self.index, other.index) == 1 }
181-
}
182-
}
183-
184118
mod app_data;
185119
mod registry_key;
186120
mod sync;
121+
mod value_ref;
187122

188123
#[cfg(any(feature = "luau", doc))]
189124
mod vector;

src/types/value_ref.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::fmt;
2+
use std::os::raw::{c_int, c_void};
3+
4+
use crate::state::{RawLua, WeakLua};
5+
6+
/// A reference to a Lua (complex) value stored in the Lua auxiliary thread.
7+
pub(crate) struct ValueRef {
8+
pub(crate) lua: WeakLua,
9+
pub(crate) index: c_int,
10+
pub(crate) drop: bool,
11+
}
12+
13+
impl ValueRef {
14+
#[inline]
15+
pub(crate) fn new(lua: &RawLua, index: c_int) -> Self {
16+
ValueRef {
17+
lua: lua.weak().clone(),
18+
index,
19+
drop: true,
20+
}
21+
}
22+
23+
#[inline]
24+
pub(crate) fn to_pointer(&self) -> *const c_void {
25+
let lua = self.lua.lock();
26+
unsafe { ffi::lua_topointer(lua.ref_thread(), self.index) }
27+
}
28+
29+
/// Returns a copy of the value, which is valid as long as the original value is held.
30+
#[inline]
31+
pub(crate) fn copy(&self) -> Self {
32+
ValueRef {
33+
lua: self.lua.clone(),
34+
index: self.index,
35+
drop: false,
36+
}
37+
}
38+
}
39+
40+
impl fmt::Debug for ValueRef {
41+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42+
write!(f, "Ref({:p})", self.to_pointer())
43+
}
44+
}
45+
46+
impl Clone for ValueRef {
47+
fn clone(&self) -> Self {
48+
unsafe { self.lua.lock().clone_ref(self) }
49+
}
50+
}
51+
52+
impl Drop for ValueRef {
53+
fn drop(&mut self) {
54+
if self.drop {
55+
if let Some(lua) = self.lua.try_lock() {
56+
unsafe { lua.drop_ref(self) };
57+
}
58+
}
59+
}
60+
}
61+
62+
impl PartialEq for ValueRef {
63+
fn eq(&self, other: &Self) -> bool {
64+
assert!(
65+
self.lua == other.lua,
66+
"Lua instance passed Value created from a different main Lua state"
67+
);
68+
let lua = self.lua.lock();
69+
unsafe { ffi::lua_rawequal(lua.ref_thread(), self.index, other.index) == 1 }
70+
}
71+
}

0 commit comments

Comments
 (0)