cargo fmt and other minor fixes
This commit is contained in:
parent
ced808d5ab
commit
c10169a380
35
src/scope.rs
35
src/scope.rs
|
@ -34,13 +34,14 @@ use {
|
||||||
/// See [`Lua::scope`] for more details.
|
/// See [`Lua::scope`] for more details.
|
||||||
///
|
///
|
||||||
/// [`Lua::scope`]: struct.Lua.html#method.scope
|
/// [`Lua::scope`]: struct.Lua.html#method.scope
|
||||||
#[allow(clippy::type_complexity)]
|
|
||||||
pub struct Scope<'lua, 'scope> {
|
pub struct Scope<'lua, 'scope> {
|
||||||
lua: &'lua Lua,
|
lua: &'lua Lua,
|
||||||
destructors: RefCell<Vec<(LuaRef<'lua>, Box<dyn Fn(LuaRef<'lua>) -> Vec<Box<dyn Any>> + 'lua>)>>,
|
destructors: RefCell<Vec<(LuaRef<'lua>, DestructorCallback<'lua>)>>,
|
||||||
_scope_invariant: PhantomData<Cell<&'scope ()>>,
|
_scope_invariant: PhantomData<Cell<&'scope ()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DestructorCallback<'lua> = Box<dyn Fn(LuaRef<'lua>) -> Vec<Box<dyn Any>> + 'lua>;
|
||||||
|
|
||||||
impl<'lua, 'scope> Scope<'lua, 'scope> {
|
impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
pub(crate) fn new(lua: &'lua Lua) -> Scope<'lua, 'scope> {
|
pub(crate) fn new(lua: &'lua Lua) -> Scope<'lua, 'scope> {
|
||||||
Scope {
|
Scope {
|
||||||
|
@ -184,7 +185,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
|
|
||||||
#[cfg(any(feature = "lua51", feature = "luajit"))]
|
#[cfg(any(feature = "lua51", feature = "luajit"))]
|
||||||
let newtable = self.lua.create_table()?;
|
let newtable = self.lua.create_table()?;
|
||||||
self.destructors.borrow_mut().push((ud.0.clone(), Box::new(move |u| {
|
let destructor: DestructorCallback = Box::new(move |u| {
|
||||||
let state = u.lua.state;
|
let state = u.lua.state;
|
||||||
assert_stack(state, 2);
|
assert_stack(state, 2);
|
||||||
u.lua.push_ref(&u);
|
u.lua.push_ref(&u);
|
||||||
|
@ -199,7 +200,10 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
// We know the destructor has not run yet because we hold a reference to the
|
// We know the destructor has not run yet because we hold a reference to the
|
||||||
// userdata.
|
// userdata.
|
||||||
vec![Box::new(take_userdata::<UserDataCell<T>>(state))]
|
vec![Box::new(take_userdata::<UserDataCell<T>>(state))]
|
||||||
})));
|
});
|
||||||
|
self.destructors
|
||||||
|
.borrow_mut()
|
||||||
|
.push((ud.0.clone(), destructor));
|
||||||
|
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
}
|
}
|
||||||
|
@ -399,7 +403,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
|
|
||||||
#[cfg(any(feature = "lua51", feature = "luajit"))]
|
#[cfg(any(feature = "lua51", feature = "luajit"))]
|
||||||
let newtable = lua.create_table()?;
|
let newtable = lua.create_table()?;
|
||||||
self.destructors.borrow_mut().push((ud.0.clone(), Box::new(move |ud| {
|
let destructor: DestructorCallback = Box::new(move |ud| {
|
||||||
// We know the destructor has not run yet because we hold a reference to the userdata.
|
// We know the destructor has not run yet because we hold a reference to the userdata.
|
||||||
let state = ud.lua.state;
|
let state = ud.lua.state;
|
||||||
assert_stack(state, 2);
|
assert_stack(state, 2);
|
||||||
|
@ -419,7 +423,10 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
ffi::lua_setuservalue(state, -2);
|
ffi::lua_setuservalue(state, -2);
|
||||||
|
|
||||||
vec![Box::new(take_userdata::<UserDataCell<()>>(state))]
|
vec![Box::new(take_userdata::<UserDataCell<()>>(state))]
|
||||||
})));
|
});
|
||||||
|
self.destructors
|
||||||
|
.borrow_mut()
|
||||||
|
.push((ud.0.clone(), destructor));
|
||||||
|
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
}
|
}
|
||||||
|
@ -437,7 +444,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
let f = mem::transmute::<Callback<'callback, 'scope>, Callback<'lua, 'static>>(f);
|
let f = mem::transmute::<Callback<'callback, 'scope>, Callback<'lua, 'static>>(f);
|
||||||
let f = self.lua.create_callback(f)?;
|
let f = self.lua.create_callback(f)?;
|
||||||
|
|
||||||
self.destructors.borrow_mut().push((f.0.clone(), Box::new(|f| {
|
let destructor: DestructorCallback = Box::new(|f| {
|
||||||
let state = f.lua.state;
|
let state = f.lua.state;
|
||||||
assert_stack(state, 3);
|
assert_stack(state, 3);
|
||||||
f.lua.push_ref(&f);
|
f.lua.push_ref(&f);
|
||||||
|
@ -456,7 +463,11 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
|
|
||||||
ffi::lua_pop(state, 1);
|
ffi::lua_pop(state, 1);
|
||||||
vec![Box::new(ud1), Box::new(ud2)]
|
vec![Box::new(ud1), Box::new(ud2)]
|
||||||
})));
|
});
|
||||||
|
self.destructors
|
||||||
|
.borrow_mut()
|
||||||
|
.push((f.0.clone(), destructor));
|
||||||
|
|
||||||
Ok(f)
|
Ok(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -471,8 +482,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
// We need to pre-allocate strings to avoid failures in destructor.
|
// We need to pre-allocate strings to avoid failures in destructor.
|
||||||
let get_poll_str = self.lua.create_string("get_poll")?;
|
let get_poll_str = self.lua.create_string("get_poll")?;
|
||||||
let poll_str = self.lua.create_string("poll")?;
|
let poll_str = self.lua.create_string("poll")?;
|
||||||
|
let destructor: DestructorCallback = Box::new(move |f| {
|
||||||
self.destructors.borrow_mut().push((f.0.clone(), Box::new(move |f| {
|
|
||||||
let state = f.lua.state;
|
let state = f.lua.state;
|
||||||
assert_stack(state, 4);
|
assert_stack(state, 4);
|
||||||
f.lua.push_ref(&f);
|
f.lua.push_ref(&f);
|
||||||
|
@ -520,7 +530,10 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||||
}
|
}
|
||||||
|
|
||||||
data
|
data
|
||||||
})));
|
});
|
||||||
|
self.destructors
|
||||||
|
.borrow_mut()
|
||||||
|
.push((f.0.clone(), destructor));
|
||||||
|
|
||||||
Ok(f)
|
Ok(f)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
// OK
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::os::raw::{c_int, c_void};
|
use std::os::raw::{c_int, c_void};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
// OK
|
|
||||||
use std::cell::{Ref, RefMut};
|
use std::cell::{Ref, RefMut};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
|
@ -385,14 +385,6 @@ pub unsafe fn get_wrapped_error(state: *mut ffi::lua_State, index: c_int) -> *co
|
||||||
&(*ud).0
|
&(*ud).0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern "C" fn mlua_get_wrapped_error(
|
|
||||||
state: *mut ffi::lua_State,
|
|
||||||
index: c_int,
|
|
||||||
) -> *const c_void {
|
|
||||||
get_wrapped_error(state, index) as *const c_void
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize the internal (with __gc) metatable for a type T
|
// Initialize the internal (with __gc) metatable for a type T
|
||||||
pub unsafe fn init_gc_metatable_for<T: Any>(
|
pub unsafe fn init_gc_metatable_for<T: Any>(
|
||||||
state: *mut ffi::lua_State,
|
state: *mut ffi::lua_State,
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
// OK
|
|
||||||
use std::iter::{self, FromIterator};
|
use std::iter::{self, FromIterator};
|
||||||
use std::{slice, str, vec};
|
use std::{slice, str, vec};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue