Skip to content

Commit 8e11105

Browse files
committed
Make BorrowedBytes/BorrowedStr: Send + Sync
They are immutable and don't require holding a lock
1 parent e1c0aa8 commit 8e11105

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/state.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,15 @@ impl WeakLua {
18701870
pub(crate) fn try_lock(&self) -> Option<LuaGuard> {
18711871
Some(LuaGuard::new(self.0.upgrade()?))
18721872
}
1873+
1874+
#[track_caller]
1875+
#[inline(always)]
1876+
pub(crate) fn upgrade(&self) -> Lua {
1877+
Lua {
1878+
raw: self.0.upgrade().expect("Lua instance is destroyed"),
1879+
collect_garbage: false,
1880+
}
1881+
}
18731882
}
18741883

18751884
impl PartialEq for WeakLua {

src/string.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use {
1212
};
1313

1414
use crate::error::{Error, Result};
15-
use crate::state::LuaGuard;
15+
use crate::state::Lua;
1616
use crate::types::ValueRef;
1717

1818
/// Handle to an internal Lua string.
@@ -103,9 +103,10 @@ impl String {
103103
BorrowedBytes(bytes, guard)
104104
}
105105

106-
unsafe fn to_slice(&self) -> (&[u8], LuaGuard) {
107-
let lua = self.0.lua.lock();
108-
let ref_thread = lua.ref_thread();
106+
unsafe fn to_slice(&self) -> (&[u8], Lua) {
107+
let lua = self.0.lua.upgrade();
108+
let rawlua = lua.lock();
109+
let ref_thread = rawlua.ref_thread();
109110
unsafe {
110111
mlua_debug_assert!(
111112
ffi::lua_type(ref_thread, self.0.index) == ffi::LUA_TSTRING,
@@ -117,6 +118,7 @@ impl String {
117118
// string type
118119
let data = ffi::lua_tolstring(ref_thread, self.0.index, &mut size);
119120

121+
drop(rawlua);
120122
(slice::from_raw_parts(data as *const u8, size + 1), lua)
121123
}
122124
}
@@ -211,7 +213,7 @@ impl Serialize for String {
211213
}
212214

213215
/// A borrowed string (`&str`) that holds a strong reference to the Lua state.
214-
pub struct BorrowedStr<'a>(&'a str, #[allow(unused)] LuaGuard);
216+
pub struct BorrowedStr<'a>(&'a str, #[allow(unused)] Lua);
215217

216218
impl Deref for BorrowedStr<'_> {
217219
type Target = str;
@@ -267,7 +269,7 @@ where
267269
}
268270

269271
/// A borrowed byte slice (`&[u8]`) that holds a strong reference to the Lua state.
270-
pub struct BorrowedBytes<'a>(&'a [u8], #[allow(unused)] LuaGuard);
272+
pub struct BorrowedBytes<'a>(&'a [u8], #[allow(unused)] Lua);
271273

272274
impl Deref for BorrowedBytes<'_> {
273275
type Target = [u8];
@@ -333,4 +335,8 @@ mod assertions {
333335
static_assertions::assert_not_impl_any!(String: Send);
334336
#[cfg(feature = "send")]
335337
static_assertions::assert_impl_all!(String: Send, Sync);
338+
#[cfg(feature = "send")]
339+
static_assertions::assert_impl_all!(BorrowedBytes: Send, Sync);
340+
#[cfg(feature = "send")]
341+
static_assertions::assert_impl_all!(BorrowedStr: Send, Sync);
336342
}

0 commit comments

Comments
 (0)