Skip to content

Commit 30cf4be

Browse files
committed
Use RwLock directly instead of UserDataCell
1 parent 5776c72 commit 30cf4be

File tree

1 file changed

+11
-41
lines changed

1 file changed

+11
-41
lines changed

src/userdata/cell.rs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ pub(crate) enum UserDataStorage<T> {
2323
// A enum for storing userdata values.
2424
// It's stored inside a Lua VM and protected by the outer `ReentrantMutex`.
2525
pub(crate) enum UserDataVariant<T> {
26-
Default(XRc<UserDataCell<T>>),
26+
Default(XRc<RwLock<T>>),
2727
#[cfg(feature = "serde")]
28-
Serializable(XRc<UserDataCell<Box<DynSerialize>>>),
28+
Serializable(XRc<RwLock<Box<DynSerialize>>>),
2929
}
3030

3131
impl<T> Clone for UserDataVariant<T> {
@@ -80,12 +80,12 @@ impl<T> UserDataVariant<T> {
8080
return Err(Error::UserDataBorrowMutError);
8181
}
8282
Ok(match self {
83-
Self::Default(inner) => XRc::into_inner(inner).unwrap().into_value(),
83+
Self::Default(inner) => XRc::into_inner(inner).unwrap().into_inner(),
8484
#[cfg(feature = "serde")]
8585
Self::Serializable(inner) => unsafe {
8686
// The serde variant erases `T` to `Box<DynSerialize>`, so we
8787
// must cast the raw pointer back to recover the concrete type.
88-
let raw = Box::into_raw(XRc::into_inner(inner).unwrap().into_value());
88+
let raw = Box::into_raw(XRc::into_inner(inner).unwrap().into_inner());
8989
*Box::from_raw(raw as *mut T)
9090
},
9191
})
@@ -103,18 +103,18 @@ impl<T> UserDataVariant<T> {
103103
#[inline(always)]
104104
pub(super) fn raw_lock(&self) -> &RawLock {
105105
match self {
106-
Self::Default(inner) => unsafe { inner.raw_lock() },
106+
Self::Default(inner) => unsafe { inner.raw() },
107107
#[cfg(feature = "serde")]
108-
Self::Serializable(inner) => unsafe { inner.raw_lock() },
108+
Self::Serializable(inner) => unsafe { inner.raw() },
109109
}
110110
}
111111

112112
#[inline(always)]
113113
pub(super) fn as_ptr(&self) -> *mut T {
114114
match self {
115-
Self::Default(inner) => inner.as_ptr(),
115+
Self::Default(inner) => inner.data_ptr(),
116116
#[cfg(feature = "serde")]
117-
Self::Serializable(inner) => unsafe { (&mut **inner.as_ptr()) as *mut DynSerialize as *mut T },
117+
Self::Serializable(inner) => unsafe { (&mut **inner.data_ptr()) as *mut DynSerialize as *mut T },
118118
}
119119
}
120120
}
@@ -126,43 +126,13 @@ impl Serialize for UserDataStorage<()> {
126126
Self::Owned(variant @ UserDataVariant::Serializable(inner)) => unsafe {
127127
let _guard = (variant.raw_lock().try_lock_shared_guarded())
128128
.map_err(|_| serde::ser::Error::custom(Error::UserDataBorrowError))?;
129-
(*inner.as_ptr()).serialize(serializer)
129+
(*inner.data_ptr()).serialize(serializer)
130130
},
131131
_ => Err(serde::ser::Error::custom("cannot serialize <userdata>")),
132132
}
133133
}
134134
}
135135

136-
/// A type that provides interior mutability for a userdata value (thread-safe).
137-
pub(crate) struct UserDataCell<T>(RwLock<T>);
138-
139-
impl<T> UserDataCell<T> {
140-
#[inline(always)]
141-
fn new(value: T) -> Self {
142-
UserDataCell(RwLock::new(value))
143-
}
144-
145-
/// Returns a reference to the underlying raw lock.
146-
#[inline(always)]
147-
pub(super) unsafe fn raw_lock(&self) -> &RawLock {
148-
self.0.raw()
149-
}
150-
151-
/// Returns a raw pointer to the wrapped value.
152-
///
153-
/// The caller is responsible for ensuring the appropriate lock is held.
154-
#[inline(always)]
155-
pub(super) fn as_ptr(&self) -> *mut T {
156-
self.0.data_ptr()
157-
}
158-
159-
/// Consumes the cell and returns the inner value.
160-
#[inline(always)]
161-
pub(super) fn into_value(self) -> T {
162-
self.0.into_inner()
163-
}
164-
}
165-
166136
pub(crate) enum ScopedUserDataVariant<T> {
167137
Ref(*const T),
168138
RefMut(RefCell<*mut T>),
@@ -183,7 +153,7 @@ impl<T> Drop for ScopedUserDataVariant<T> {
183153
impl<T: 'static> UserDataStorage<T> {
184154
#[inline(always)]
185155
pub(crate) fn new(data: T) -> Self {
186-
Self::Owned(UserDataVariant::Default(XRc::new(UserDataCell::new(data))))
156+
Self::Owned(UserDataVariant::Default(XRc::new(RwLock::new(data))))
187157
}
188158

189159
#[inline(always)]
@@ -203,7 +173,7 @@ impl<T: 'static> UserDataStorage<T> {
203173
T: Serialize + crate::types::MaybeSend + crate::types::MaybeSync,
204174
{
205175
let data = Box::new(data) as Box<DynSerialize>;
206-
let variant = UserDataVariant::Serializable(XRc::new(UserDataCell::new(data)));
176+
let variant = UserDataVariant::Serializable(XRc::new(RwLock::new(data)));
207177
Self::Owned(variant)
208178
}
209179

0 commit comments

Comments
 (0)