Skip to content

Commit c1395ab

Browse files
committed
clippy
1 parent cd6d86a commit c1395ab

File tree

12 files changed

+61
-65
lines changed

12 files changed

+61
-65
lines changed

src/conversion.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::borrow::Cow;
22
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
33
use std::ffi::{CStr, CString};
44
use std::hash::{BuildHasher, Hash};
5-
use std::mem::transmute;
65
use std::os::raw::c_int;
76
use std::string::String as StdString;
87
use std::{slice, str};
@@ -23,14 +22,14 @@ use crate::value::{FromLua, IntoLua, Nil, Value};
2322
impl IntoLua for Value {
2423
#[inline]
2524
fn into_lua(self, _: &Lua) -> Result<Value> {
26-
unsafe { Ok(transmute(self)) }
25+
Ok(self)
2726
}
2827
}
2928

3029
impl IntoLua for &Value {
3130
#[inline]
3231
fn into_lua(self, _: &Lua) -> Result<Value> {
33-
unsafe { Ok(transmute(self.clone())) }
32+
Ok(self.clone())
3433
}
3534

3635
#[inline]
@@ -49,14 +48,14 @@ impl FromLua for Value {
4948
impl IntoLua for String {
5049
#[inline]
5150
fn into_lua(self, _: &Lua) -> Result<Value> {
52-
unsafe { Ok(Value::String(transmute(self))) }
51+
Ok(Value::String(self))
5352
}
5453
}
5554

5655
impl IntoLua for &String {
5756
#[inline]
5857
fn into_lua(self, _: &Lua) -> Result<Value> {
59-
unsafe { Ok(Value::String(transmute(self.clone()))) }
58+
Ok(Value::String(self.clone()))
6059
}
6160

6261
#[inline]
@@ -82,14 +81,14 @@ impl FromLua for String {
8281
impl IntoLua for Table {
8382
#[inline]
8483
fn into_lua(self, _: &Lua) -> Result<Value> {
85-
unsafe { Ok(Value::Table(transmute(self))) }
84+
Ok(Value::Table(self))
8685
}
8786
}
8887

8988
impl IntoLua for &Table {
9089
#[inline]
9190
fn into_lua(self, _: &Lua) -> Result<Value> {
92-
unsafe { Ok(Value::Table(transmute(self.clone()))) }
91+
Ok(Value::Table(self.clone()))
9392
}
9493

9594
#[inline]
@@ -116,14 +115,14 @@ impl FromLua for Table {
116115
impl IntoLua for Function {
117116
#[inline]
118117
fn into_lua(self, _: &Lua) -> Result<Value> {
119-
unsafe { Ok(Value::Function(transmute(self))) }
118+
Ok(Value::Function(self))
120119
}
121120
}
122121

123122
impl IntoLua for &Function {
124123
#[inline]
125124
fn into_lua(self, _: &Lua) -> Result<Value> {
126-
unsafe { Ok(Value::Function(transmute(self.clone()))) }
125+
Ok(Value::Function(self.clone()))
127126
}
128127

129128
#[inline]
@@ -150,14 +149,14 @@ impl FromLua for Function {
150149
impl IntoLua for Thread {
151150
#[inline]
152151
fn into_lua(self, _: &Lua) -> Result<Value> {
153-
unsafe { Ok(Value::Thread(transmute(self))) }
152+
Ok(Value::Thread(self))
154153
}
155154
}
156155

157156
impl IntoLua for &Thread {
158157
#[inline]
159158
fn into_lua(self, _: &Lua) -> Result<Value> {
160-
unsafe { Ok(Value::Thread(transmute(self.clone()))) }
159+
Ok(Value::Thread(self.clone()))
161160
}
162161

163162
#[inline]
@@ -184,14 +183,14 @@ impl FromLua for Thread {
184183
impl IntoLua for AnyUserData {
185184
#[inline]
186185
fn into_lua(self, _: &Lua) -> Result<Value> {
187-
unsafe { Ok(Value::UserData(transmute(self))) }
186+
Ok(Value::UserData(self))
188187
}
189188
}
190189

191190
impl IntoLua for &AnyUserData {
192191
#[inline]
193192
fn into_lua(self, _: &Lua) -> Result<Value> {
194-
unsafe { Ok(Value::UserData(transmute(self.clone()))) }
193+
Ok(Value::UserData(self.clone()))
195194
}
196195

197196
#[inline]
@@ -359,7 +358,7 @@ impl FromLua for crate::types::Vector {
359358
impl IntoLua for StdString {
360359
#[inline]
361360
fn into_lua(self, lua: &Lua) -> Result<Value> {
362-
Ok(Value::String(lua.create_string(&self)?))
361+
Ok(Value::String(lua.create_string(self)?))
363362
}
364363

365364
#[inline]
@@ -493,7 +492,7 @@ impl IntoLua for Cow<'_, CStr> {
493492
impl IntoLua for BString {
494493
#[inline]
495494
fn into_lua(self, lua: &Lua) -> Result<Value> {
496-
Ok(Value::String(lua.create_string(&self)?))
495+
Ok(Value::String(lua.create_string(self)?))
497496
}
498497
}
499498

src/hook.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct Debug<'a> {
2929

3030
enum EitherLua<'a> {
3131
Owned(ReentrantMutexGuard<'a, RawLua>),
32+
#[cfg(not(feature = "luau"))]
3233
Borrowed(&'a RawLua),
3334
}
3435

@@ -37,7 +38,8 @@ impl Deref for EitherLua<'_> {
3738

3839
fn deref(&self) -> &Self::Target {
3940
match self {
40-
EitherLua::Owned(guard) => &*guard,
41+
EitherLua::Owned(guard) => guard,
42+
#[cfg(not(feature = "luau"))]
4143
EitherLua::Borrowed(lua) => lua,
4244
}
4345
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ pub use crate::chunk::{AsChunk, Chunk, ChunkMode};
107107
pub use crate::error::{Error, ErrorContext, ExternalError, ExternalResult, Result};
108108
pub use crate::function::{Function, FunctionInfo};
109109
pub use crate::hook::{Debug, DebugEvent, DebugNames, DebugSource, DebugStack};
110-
pub use crate::state::{GCMode, Lua, LuaOptions};
111110
pub use crate::multi::Variadic;
111+
pub use crate::state::{GCMode, Lua, LuaOptions};
112112
// pub use crate::scope::Scope;
113113
pub use crate::stdlib::StdLib;
114114
pub use crate::string::String;

src/multi.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::iter::FromIterator;
2-
use std::mem::transmute;
32
use std::ops::{Deref, DerefMut};
43
use std::os::raw::c_int;
54
use std::result::Result as StdResult;
@@ -99,7 +98,7 @@ impl<T: FromLua> FromLuaMulti for T {
9998
impl IntoLuaMulti for MultiValue {
10099
#[inline]
101100
fn into_lua_multi(self, _: &Lua) -> Result<MultiValue> {
102-
unsafe { Ok(transmute(self)) }
101+
Ok(self)
103102
}
104103
}
105104

src/serde/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::os::raw::c_void;
55
use serde::{de::DeserializeOwned, ser::Serialize};
66

77
use crate::error::Result;
8-
use crate::state::Lua;
98
use crate::private::Sealed;
9+
use crate::state::Lua;
1010
use crate::table::Table;
1111
use crate::util::check_stack;
1212
use crate::value::Value;

src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ impl Deref for LuaGuard {
19141914
type Target = RawLua;
19151915

19161916
fn deref(&self) -> &Self::Target {
1917-
&*self.0
1917+
&self.0
19181918
}
19191919
}
19201920

src/state/extra.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::any::TypeId;
22
use std::cell::UnsafeCell;
3+
use std::rc::Rc;
34
// use std::collections::VecDeque;
45
use std::mem::{self, MaybeUninit};
56
use std::os::raw::{c_int, c_void};
@@ -106,7 +107,7 @@ impl ExtraData {
106107
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
107108
pub(super) const ERROR_TRACEBACK_IDX: c_int = 1;
108109

109-
pub(super) unsafe fn init(state: *mut ffi::lua_State) -> Arc<UnsafeCell<Self>> {
110+
pub(super) unsafe fn init(state: *mut ffi::lua_State) -> Rc<UnsafeCell<Self>> {
110111
// Create ref stack thread and place it in the registry to prevent it
111112
// from being garbage collected.
112113
let ref_thread = mlua_expect!(
@@ -132,7 +133,7 @@ impl ExtraData {
132133
assert_eq!(ffi::lua_gettop(ref_thread), Self::ERROR_TRACEBACK_IDX);
133134
}
134135

135-
let extra = Arc::new(UnsafeCell::new(ExtraData {
136+
let extra = Rc::new(UnsafeCell::new(ExtraData {
136137
lua: MaybeUninit::uninit(),
137138
weak: MaybeUninit::uninit(),
138139
registered_userdata: FxHashMap::default(),
@@ -200,19 +201,19 @@ impl ExtraData {
200201
ffi::lua_pop(state, 1);
201202
return ptr::null_mut();
202203
}
203-
let extra_ptr = ffi::lua_touserdata(state, -1) as *mut Arc<UnsafeCell<ExtraData>>;
204+
let extra_ptr = ffi::lua_touserdata(state, -1) as *mut Rc<UnsafeCell<ExtraData>>;
204205
ffi::lua_pop(state, 1);
205206
(*extra_ptr).get()
206207
}
207208

208-
unsafe fn store(extra: &Arc<UnsafeCell<Self>>, state: *mut ffi::lua_State) -> Result<()> {
209+
unsafe fn store(extra: &Rc<UnsafeCell<Self>>, state: *mut ffi::lua_State) -> Result<()> {
209210
#[cfg(feature = "luau")]
210211
if cfg!(not(feature = "module")) {
211212
(*ffi::lua_callbacks(state)).userdata = extra.get() as *mut _;
212213
return Ok(());
213214
}
214215

215-
push_gc_userdata(state, Arc::clone(extra), true)?;
216+
push_gc_userdata(state, Rc::clone(extra), true)?;
216217
protect_lua!(state, 1, 0, fn(state) {
217218
let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void;
218219
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key);

src/state/raw.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::cell::{Cell, UnsafeCell};
33
use std::ffi::{CStr, CString};
44
use std::os::raw::{c_char, c_int, c_void};
55
use std::panic::resume_unwind;
6+
use std::rc::Rc;
67
use std::result::Result as StdResult;
78
use std::sync::Arc;
89
use std::{mem, ptr};
@@ -49,7 +50,7 @@ pub struct RawLua {
4950
// The state is dynamic and depends on context
5051
pub(super) state: Cell<*mut ffi::lua_State>,
5152
pub(super) main_state: *mut ffi::lua_State,
52-
pub(super) extra: Arc<UnsafeCell<ExtraData>>,
53+
pub(super) extra: Rc<UnsafeCell<ExtraData>>,
5354
}
5455

5556
#[cfg(not(feature = "module"))]
@@ -169,7 +170,7 @@ impl RawLua {
169170
// Create the internal metatables and store them in the registry
170171
// to prevent from being garbage collected.
171172

172-
init_gc_metatable::<Arc<UnsafeCell<ExtraData>>>(state, None)?;
173+
init_gc_metatable::<Rc<UnsafeCell<ExtraData>>>(state, None)?;
173174
init_gc_metatable::<Callback>(state, None)?;
174175
init_gc_metatable::<CallbackUpvalue>(state, None)?;
175176
#[cfg(feature = "async")]
@@ -207,10 +208,11 @@ impl RawLua {
207208
);
208209
assert_stack(main_state, ffi::LUA_MINSTACK);
209210

211+
#[allow(clippy::arc_with_non_send_sync)]
210212
let rawlua = Arc::new(ReentrantMutex::new(RawLua {
211213
state: Cell::new(state),
212214
main_state,
213-
extra: Arc::clone(&extra),
215+
extra: Rc::clone(&extra),
214216
}));
215217
(*extra.get()).set_lua(&rawlua);
216218

@@ -1102,8 +1104,8 @@ impl RawLua {
11021104
let _sg = StackGuard::new(state);
11031105
check_stack(state, 4)?;
11041106

1105-
let func = mem::transmute(func);
1106-
let extra = Arc::clone(&self.extra);
1107+
let func = mem::transmute::<Callback, Callback<'static>>(func);
1108+
let extra = Rc::clone(&self.extra);
11071109
let protect = !self.unlikely_memory_error();
11081110
push_gc_userdata(state, CallbackUpvalue { data: func, extra }, protect)?;
11091111
if protect {
@@ -1147,7 +1149,7 @@ impl RawLua {
11471149
let args = MultiValue::from_stack_multi(nargs, rawlua)?;
11481150
let func = &*(*upvalue).data;
11491151
let fut = func(rawlua, args);
1150-
let extra = Arc::clone(&(*upvalue).extra);
1152+
let extra = Rc::clone(&(*upvalue).extra);
11511153
let protect = !rawlua.unlikely_memory_error();
11521154
push_gc_userdata(state, AsyncPollUpvalue { data: fut, extra }, protect)?;
11531155
if protect {
@@ -1169,7 +1171,7 @@ impl RawLua {
11691171
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
11701172
// The lock must be already held as the future is polled
11711173
let rawlua = (*extra).raw_lua();
1172-
let _guard = StateGuard::new(&rawlua, state);
1174+
let _guard = StateGuard::new(rawlua, state);
11731175

11741176
let fut = &mut (*upvalue).data;
11751177
let mut ctx = Context::from_waker(rawlua.waker());
@@ -1190,7 +1192,7 @@ impl RawLua {
11901192
Ok(nresults + 1)
11911193
}
11921194
nresults => {
1193-
let results = MultiValue::from_stack_multi(nresults, &rawlua)?;
1195+
let results = MultiValue::from_stack_multi(nresults, rawlua)?;
11941196
ffi::lua_pushinteger(state, nresults as _);
11951197
rawlua.push(rawlua.create_sequence_from(results)?)?;
11961198
Ok(2)
@@ -1206,8 +1208,8 @@ impl RawLua {
12061208
let _sg = StackGuard::new(state);
12071209
check_stack(state, 4)?;
12081210

1209-
let func = mem::transmute(func);
1210-
let extra = Arc::clone(&self.extra);
1211+
let func = mem::transmute::<AsyncCallback, AsyncCallback<'static>>(func);
1212+
let extra = Rc::clone(&self.extra);
12111213
let protect = !self.unlikely_memory_error();
12121214
let upvalue = AsyncCallbackUpvalue { data: func, extra };
12131215
push_gc_userdata(state, upvalue, protect)?;

src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
33
use std::hash::{Hash, Hasher};
44
use std::ops::{Deref, DerefMut};
55
use std::os::raw::{c_int, c_void};
6+
use std::rc::Rc;
67
use std::result::Result as StdResult;
78
use std::sync::atomic::{AtomicI32, Ordering};
89
use std::sync::Arc;
@@ -45,7 +46,7 @@ pub(crate) type Callback<'a> = Box<dyn Fn(&'a RawLua, c_int) -> Result<c_int> +
4546

4647
pub(crate) struct Upvalue<T> {
4748
pub(crate) data: T,
48-
pub(crate) extra: Arc<UnsafeCell<ExtraData>>,
49+
pub(crate) extra: Rc<UnsafeCell<ExtraData>>,
4950
}
5051

5152
pub(crate) type CallbackUpvalue = Upvalue<Callback<'static>>;

0 commit comments

Comments
 (0)