Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Jul 8, 2024
1 parent 4e61719 commit 3a8091c
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 63 deletions.
29 changes: 14 additions & 15 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ffi::{CStr, CString};
use std::hash::{BuildHasher, Hash};
use std::mem::transmute;
use std::os::raw::c_int;
use std::string::String as StdString;
use std::{slice, str};
Expand All @@ -23,14 +22,14 @@ use crate::value::{FromLua, IntoLua, Nil, Value};
impl IntoLua for Value {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(transmute(self)) }
Ok(self)
}
}

impl IntoLua for &Value {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(transmute(self.clone())) }
Ok(self.clone())
}

#[inline]
Expand All @@ -49,14 +48,14 @@ impl FromLua for Value {
impl IntoLua for String {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::String(transmute(self))) }
Ok(Value::String(self))
}
}

impl IntoLua for &String {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::String(transmute(self.clone()))) }
Ok(Value::String(self.clone()))
}

#[inline]
Expand All @@ -82,14 +81,14 @@ impl FromLua for String {
impl IntoLua for Table {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::Table(transmute(self))) }
Ok(Value::Table(self))
}
}

impl IntoLua for &Table {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::Table(transmute(self.clone()))) }
Ok(Value::Table(self.clone()))
}

#[inline]
Expand All @@ -116,14 +115,14 @@ impl FromLua for Table {
impl IntoLua for Function {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::Function(transmute(self))) }
Ok(Value::Function(self))
}
}

impl IntoLua for &Function {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::Function(transmute(self.clone()))) }
Ok(Value::Function(self.clone()))
}

#[inline]
Expand All @@ -150,14 +149,14 @@ impl FromLua for Function {
impl IntoLua for Thread {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::Thread(transmute(self))) }
Ok(Value::Thread(self))
}
}

impl IntoLua for &Thread {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::Thread(transmute(self.clone()))) }
Ok(Value::Thread(self.clone()))
}

#[inline]
Expand All @@ -184,14 +183,14 @@ impl FromLua for Thread {
impl IntoLua for AnyUserData {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::UserData(transmute(self))) }
Ok(Value::UserData(self))
}
}

impl IntoLua for &AnyUserData {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
unsafe { Ok(Value::UserData(transmute(self.clone()))) }
Ok(Value::UserData(self.clone()))
}

#[inline]
Expand Down Expand Up @@ -359,7 +358,7 @@ impl FromLua for crate::types::Vector {
impl IntoLua for StdString {
#[inline]
fn into_lua(self, lua: &Lua) -> Result<Value> {
Ok(Value::String(lua.create_string(&self)?))
Ok(Value::String(lua.create_string(self)?))
}

#[inline]
Expand Down Expand Up @@ -493,7 +492,7 @@ impl IntoLua for Cow<'_, CStr> {
impl IntoLua for BString {
#[inline]
fn into_lua(self, lua: &Lua) -> Result<Value> {
Ok(Value::String(lua.create_string(&self)?))
Ok(Value::String(lua.create_string(self)?))
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Debug<'a> {

enum EitherLua<'a> {
Owned(ReentrantMutexGuard<'a, RawLua>),
#[cfg(not(feature = "luau"))]
Borrowed(&'a RawLua),
}

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

fn deref(&self) -> &Self::Target {
match self {
EitherLua::Owned(guard) => &*guard,
EitherLua::Owned(guard) => guard,
#[cfg(not(feature = "luau"))]
EitherLua::Borrowed(lua) => lua,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ pub use crate::chunk::{AsChunk, Chunk, ChunkMode};
pub use crate::error::{Error, ErrorContext, ExternalError, ExternalResult, Result};
pub use crate::function::{Function, FunctionInfo};
pub use crate::hook::{Debug, DebugEvent, DebugNames, DebugSource, DebugStack};
pub use crate::state::{GCMode, Lua, LuaOptions};
pub use crate::multi::Variadic;
pub use crate::state::{GCMode, Lua, LuaOptions};
// pub use crate::scope::Scope;
pub use crate::stdlib::StdLib;
pub use crate::string::String;
Expand Down
3 changes: 1 addition & 2 deletions src/multi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::iter::FromIterator;
use std::mem::transmute;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_int;
use std::result::Result as StdResult;
Expand Down Expand Up @@ -99,7 +98,7 @@ impl<T: FromLua> FromLuaMulti for T {
impl IntoLuaMulti for MultiValue {
#[inline]
fn into_lua_multi(self, _: &Lua) -> Result<MultiValue> {
unsafe { Ok(transmute(self)) }
Ok(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::os::raw::c_void;
use serde::{de::DeserializeOwned, ser::Serialize};

use crate::error::Result;
use crate::state::Lua;
use crate::private::Sealed;
use crate::state::Lua;
use crate::table::Table;
use crate::util::check_stack;
use crate::value::Value;
Expand Down
2 changes: 1 addition & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ impl Deref for LuaGuard {
type Target = RawLua;

fn deref(&self) -> &Self::Target {
&*self.0
&self.0
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/state/extra.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::any::TypeId;
use std::cell::UnsafeCell;
use std::rc::Rc;
// use std::collections::VecDeque;
use std::mem::{self, MaybeUninit};
use std::os::raw::{c_int, c_void};
Expand Down Expand Up @@ -106,7 +107,7 @@ impl ExtraData {
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
pub(super) const ERROR_TRACEBACK_IDX: c_int = 1;

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

let extra = Arc::new(UnsafeCell::new(ExtraData {
let extra = Rc::new(UnsafeCell::new(ExtraData {
lua: MaybeUninit::uninit(),
weak: MaybeUninit::uninit(),
registered_userdata: FxHashMap::default(),
Expand Down Expand Up @@ -200,19 +201,19 @@ impl ExtraData {
ffi::lua_pop(state, 1);
return ptr::null_mut();
}
let extra_ptr = ffi::lua_touserdata(state, -1) as *mut Arc<UnsafeCell<ExtraData>>;
let extra_ptr = ffi::lua_touserdata(state, -1) as *mut Rc<UnsafeCell<ExtraData>>;
ffi::lua_pop(state, 1);
(*extra_ptr).get()
}

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

push_gc_userdata(state, Arc::clone(extra), true)?;
push_gc_userdata(state, Rc::clone(extra), true)?;
protect_lua!(state, 1, 0, fn(state) {
let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void;
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key);
Expand Down
20 changes: 11 additions & 9 deletions src/state/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cell::{Cell, UnsafeCell};
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_void};
use std::panic::resume_unwind;
use std::rc::Rc;
use std::result::Result as StdResult;
use std::sync::Arc;
use std::{mem, ptr};
Expand Down Expand Up @@ -49,7 +50,7 @@ pub struct RawLua {
// The state is dynamic and depends on context
pub(super) state: Cell<*mut ffi::lua_State>,
pub(super) main_state: *mut ffi::lua_State,
pub(super) extra: Arc<UnsafeCell<ExtraData>>,
pub(super) extra: Rc<UnsafeCell<ExtraData>>,
}

#[cfg(not(feature = "module"))]
Expand Down Expand Up @@ -207,10 +208,11 @@ impl RawLua {
);
assert_stack(main_state, ffi::LUA_MINSTACK);

#[allow(clippy::arc_with_non_send_sync)]
let rawlua = Arc::new(ReentrantMutex::new(RawLua {
state: Cell::new(state),
main_state,
extra: Arc::clone(&extra),
extra: Rc::clone(&extra),
}));
(*extra.get()).set_lua(&rawlua);

Expand Down Expand Up @@ -1102,8 +1104,8 @@ impl RawLua {
let _sg = StackGuard::new(state);
check_stack(state, 4)?;

let func = mem::transmute(func);
let extra = Arc::clone(&self.extra);
let func = mem::transmute::<Callback, Callback<'static>>(func);
let extra = Rc::clone(&self.extra);
let protect = !self.unlikely_memory_error();
push_gc_userdata(state, CallbackUpvalue { data: func, extra }, protect)?;
if protect {
Expand Down Expand Up @@ -1147,7 +1149,7 @@ impl RawLua {
let args = MultiValue::from_stack_multi(nargs, rawlua)?;
let func = &*(*upvalue).data;
let fut = func(rawlua, args);
let extra = Arc::clone(&(*upvalue).extra);
let extra = Rc::clone(&(*upvalue).extra);
let protect = !rawlua.unlikely_memory_error();
push_gc_userdata(state, AsyncPollUpvalue { data: fut, extra }, protect)?;
if protect {
Expand All @@ -1169,7 +1171,7 @@ impl RawLua {
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
// The lock must be already held as the future is polled
let rawlua = (*extra).raw_lua();
let _guard = StateGuard::new(&rawlua, state);
let _guard = StateGuard::new(rawlua, state);

let fut = &mut (*upvalue).data;
let mut ctx = Context::from_waker(rawlua.waker());
Expand All @@ -1190,7 +1192,7 @@ impl RawLua {
Ok(nresults + 1)
}
nresults => {
let results = MultiValue::from_stack_multi(nresults, &rawlua)?;
let results = MultiValue::from_stack_multi(nresults, rawlua)?;
ffi::lua_pushinteger(state, nresults as _);
rawlua.push(rawlua.create_sequence_from(results)?)?;
Ok(2)
Expand All @@ -1206,8 +1208,8 @@ impl RawLua {
let _sg = StackGuard::new(state);
check_stack(state, 4)?;

let func = mem::transmute(func);
let extra = Arc::clone(&self.extra);
let func = mem::transmute::<AsyncCallback, AsyncCallback<'static>>(func);
let extra = Rc::clone(&self.extra);
let protect = !self.unlikely_memory_error();
let upvalue = AsyncCallbackUpvalue { data: func, extra };
push_gc_userdata(state, upvalue, protect)?;
Expand Down
3 changes: 2 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::os::raw::{c_int, c_void};
use std::rc::Rc;
use std::result::Result as StdResult;
use std::sync::atomic::{AtomicI32, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -45,7 +46,7 @@ pub(crate) type Callback<'a> = Box<dyn Fn(&'a RawLua, c_int) -> Result<c_int> +

pub(crate) struct Upvalue<T> {
pub(crate) data: T,
pub(crate) extra: Arc<UnsafeCell<ExtraData>>,
pub(crate) extra: Rc<UnsafeCell<ExtraData>>,
}

pub(crate) type CallbackUpvalue = Upvalue<Callback<'static>>;
Expand Down
Loading

0 comments on commit 3a8091c

Please sign in to comment.