Skip to content

Commit 999177f

Browse files
authored
Merge pull request #200 from yjhmelody/derive-debug
derive debug for some rlua types
2 parents 75df507 + 66590a6 commit 999177f

File tree

8 files changed

+69
-20
lines changed

8 files changed

+69
-20
lines changed

src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::util::{
2424
};
2525
use crate::value::{FromLua, FromLuaMulti, MultiValue, Nil, ToLua, ToLuaMulti, Value};
2626

27-
#[derive(Copy, Clone)]
27+
#[derive(Copy, Clone, Debug)]
2828
pub struct Context<'lua> {
2929
pub(crate) state: *mut ffi::lua_State,
3030
_lua_invariant: Invariant<'lua>,

src/ffi.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,35 @@
33
#![allow(unused)]
44

55
use std::mem;
6-
use std::os::raw::{c_char, c_double, c_int, c_longlong, c_uchar, c_uint, c_ulonglong, c_ushort, c_void};
6+
use std::os::raw::{
7+
c_char, c_double, c_int, c_longlong, c_uchar, c_uint, c_ulonglong, c_ushort, c_void,
8+
};
79
use std::ptr;
810

911
pub type lua_Integer = c_longlong;
1012
pub type lua_Unsigned = c_ulonglong;
1113
pub type lua_Number = c_double;
1214

1315
pub enum lua_State {}
16+
1417
pub type lua_Alloc = unsafe extern "C" fn(
1518
ud: *mut c_void,
1619
ptr: *mut c_void,
1720
osize: usize,
1821
nsize: usize,
1922
) -> *mut c_void;
23+
2024
pub type lua_KContext = *mut c_void;
25+
2126
pub type lua_KFunction =
2227
unsafe extern "C" fn(state: *mut lua_State, status: c_int, ctx: lua_KContext) -> c_int;
28+
2329
pub type lua_CFunction = unsafe extern "C" fn(state: *mut lua_State) -> c_int;
30+
2431
pub type lua_Hook = unsafe extern "C" fn(state: *mut lua_State, ar: *mut lua_Debug);
2532

2633
#[repr(C)]
34+
#[derive(Debug)]
2735
pub struct lua_Debug {
2836
pub event: c_int,
2937
pub name: *const c_char,
@@ -81,9 +89,9 @@ pub const LUA_GCCOLLECT: c_int = 2;
8189
pub const LUA_GCCOUNT: c_int = 3;
8290
pub const LUA_GCCOUNTB: c_int = 4;
8391
pub const LUA_GCSTEP: c_int = 5;
84-
#[deprecated(note="please use `LUA_GCINC` instead")]
92+
#[deprecated(note = "please use `LUA_GCINC` instead")]
8593
pub const LUA_GCSETPAUSE: c_int = 6;
86-
#[deprecated(note="please use `LUA_GCINC` instead")]
94+
#[deprecated(note = "please use `LUA_GCINC` instead")]
8795
pub const LUA_GCSETSTEPMUL: c_int = 7;
8896
pub const LUA_GCISRUNNING: c_int = 9;
8997
pub const LUA_GCGEN: c_int = 10;
@@ -113,7 +121,12 @@ extern "C" {
113121
ctx: lua_KContext,
114122
k: Option<lua_KFunction>,
115123
) -> c_int;
116-
pub fn lua_resume(state: *mut lua_State, from: *mut lua_State, nargs: c_int, nresults: *mut c_int) -> c_int;
124+
pub fn lua_resume(
125+
state: *mut lua_State,
126+
from: *mut lua_State,
127+
nargs: c_int,
128+
nresults: *mut c_int,
129+
) -> c_int;
117130
pub fn lua_status(state: *mut lua_State) -> c_int;
118131

119132
pub fn lua_pushnil(state: *mut lua_State);

src/hook.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::util::callback_error;
1616
///
1717
/// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#lua_Debug
1818
/// [`Lua::set_hook`]: struct.Lua.html#method.set_hook
19-
#[derive(Clone)]
19+
#[derive(Clone, Debug)]
2020
pub struct Debug<'a> {
2121
ar: *mut lua_Debug,
2222
state: *mut lua_State,

src/lua.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ bitflags! {
6666
const SAFE_CSTACK_SIZE: c_uint = 700;
6767

6868
/// Top level Lua struct which holds the Lua state itself.
69+
#[derive(Debug)]
6970
pub struct Lua {
7071
main_state: *mut ffi::lua_State,
7172
_no_ref_unwind_safe: NoRefUnwindSafe,
@@ -357,7 +358,15 @@ impl Lua {
357358
///
358359
/// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#2.5
359360
pub fn gc_set_inc(&self, pause: c_int, step_multiplier: c_int, step_size: c_int) -> c_int {
360-
unsafe { ffi::lua_gc(self.main_state, ffi::LUA_GCINC, pause, step_multiplier, step_size) }
361+
unsafe {
362+
ffi::lua_gc(
363+
self.main_state,
364+
ffi::LUA_GCINC,
365+
pause,
366+
step_multiplier,
367+
step_size,
368+
)
369+
}
361370
}
362371

363372
/// Sets the garbage collector to generational mode.
@@ -367,7 +376,14 @@ impl Lua {
367376
///
368377
/// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#2.5
369378
pub fn gc_set_gen(&self, minor_multiplier: c_int, major_multiplier: c_int) -> c_int {
370-
unsafe { ffi::lua_gc(self.main_state, ffi::LUA_GCGEN, minor_multiplier, major_multiplier) }
379+
unsafe {
380+
ffi::lua_gc(
381+
self.main_state,
382+
ffi::LUA_GCGEN,
383+
minor_multiplier,
384+
major_multiplier,
385+
)
386+
}
371387
}
372388

373389
/// Sets the 'pause' value of the incremental collector.
@@ -376,7 +392,7 @@ impl Lua {
376392
/// documentation][lua_doc].
377393
///
378394
/// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#2.5
379-
#[deprecated(note="please use `gc_set_inc` instead")]
395+
#[deprecated(note = "please use `gc_set_inc` instead")]
380396
#[allow(deprecated)]
381397
pub fn gc_set_pause(&self, pause: c_int) -> c_int {
382398
unsafe { ffi::lua_gc(self.main_state, ffi::LUA_GCSETPAUSE, pause) }
@@ -388,7 +404,7 @@ impl Lua {
388404
/// [Lua 5.4 documentation][lua_doc].
389405
///
390406
/// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#2.5
391-
#[deprecated(note="please use `gc_set_inc` instead")]
407+
#[deprecated(note = "please use `gc_set_inc` instead")]
392408
#[allow(deprecated)]
393409
pub fn gc_set_step_multiplier(&self, step_multiplier: c_int) -> c_int {
394410
unsafe { ffi::lua_gc(self.main_state, ffi::LUA_GCSETSTEPMUL, step_multiplier) }

src/scope.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::value::{FromLuaMulti, MultiValue, ToLuaMulti, Value};
2424
/// See [`Context::scope`] for more details.
2525
///
2626
/// [`Context::scope`]: struct.Context.html#method.scope
27+
#[derive(Debug)]
2728
pub struct Scope<'lua, 'scope> {
2829
lua: Context<'lua>,
2930
destructors: RefCell<Vec<(LuaRef<'lua>, fn(LuaRef<'lua>) -> Box<dyn Any>)>>,

src/userdata.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::cell::{Ref, RefCell, RefMut};
2-
use std::os::raw::{c_int};
2+
use std::os::raw::c_int;
33

44
use crate::context::Context;
55
use crate::error::{Error, Result};
@@ -282,7 +282,9 @@ pub trait UserData: Sized {
282282

283283
/// Used to determine how many user values this userdata should have.
284284
/// Defaults to a single user value.
285-
fn get_uvalues_count(&self) -> c_int {1}
285+
fn get_uvalues_count(&self) -> c_int {
286+
1
287+
}
286288
}
287289

288290
/// Handle to an internal Lua userdata for any type that implements [`UserData`].
@@ -352,7 +354,10 @@ impl<'lua> AnyUserData<'lua> {
352354
lua.push_ref(&self.0);
353355
lua.push_value(v)?;
354356
if ffi::lua_setiuservalue(lua.state, -2, n) == 0 {
355-
Err(Error::RuntimeError(format!("userdata does not have user value {}", n)))
357+
Err(Error::RuntimeError(format!(
358+
"userdata does not have user value {}",
359+
n
360+
)))
356361
} else {
357362
Ok(())
358363
}
@@ -370,7 +375,10 @@ impl<'lua> AnyUserData<'lua> {
370375
lua.push_ref(&self.0);
371376
if ffi::lua_getiuservalue(lua.state, -1, n) == ffi::LUA_TNIL {
372377
lua.pop_value(); // remove the nil from the stack
373-
return Err(Error::RuntimeError(format!("userdata does not have user value {}", n)))
378+
return Err(Error::RuntimeError(format!(
379+
"userdata does not have user value {}",
380+
n
381+
)));
374382
}
375383
lua.pop_value()
376384
};

src/util.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,15 @@ pub unsafe fn push_string<S: ?Sized + AsRef<[u8]>>(
221221
}
222222

223223
// Internally uses 4 stack spaces, does not call checkstack
224-
pub unsafe fn push_userdata_uv<T>(state: *mut ffi::lua_State, t: T, uvalues_count: c_int) -> Result<()> {
225-
rlua_debug_assert!(uvalues_count > 0, "userdata user values cannot be below zero");
224+
pub unsafe fn push_userdata_uv<T>(
225+
state: *mut ffi::lua_State,
226+
t: T,
227+
uvalues_count: c_int,
228+
) -> Result<()> {
229+
rlua_debug_assert!(
230+
uvalues_count > 0,
231+
"userdata user values cannot be below zero"
232+
);
226233
let ud = protect_lua_closure(state, 0, 1, move |state| {
227234
ffi::lua_newuserdatauv(state, mem::size_of::<T>(), uvalues_count) as *mut T
228235
})?;
@@ -412,7 +419,8 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
412419
// lua_newuserdatauv and luaL_traceback may error, but nothing that implements Drop should be
413420
// on the rust stack at this time.
414421
// We don't need any user values in this userdata
415-
let ud = ffi::lua_newuserdatauv(state, mem::size_of::<WrappedError>(), 0) as *mut WrappedError;
422+
let ud =
423+
ffi::lua_newuserdatauv(state, mem::size_of::<WrappedError>(), 0) as *mut WrappedError;
416424
let traceback = if ffi::lua_checkstack(state, LUA_TRACEBACK_STACK) != 0 {
417425
ffi::luaL_traceback(state, state, ptr::null(), 0);
418426

@@ -629,7 +637,8 @@ pub unsafe fn init_error_registry(state: *mut ffi::lua_State) {
629637
unsafe extern "C" fn destructed_error(state: *mut ffi::lua_State) -> c_int {
630638
ffi::luaL_checkstack(state, 2, ptr::null());
631639
// We don't need any user values in this userdata
632-
let ud = ffi::lua_newuserdatauv(state, mem::size_of::<WrappedError>(), 0) as *mut WrappedError;
640+
let ud =
641+
ffi::lua_newuserdatauv(state, mem::size_of::<WrappedError>(), 0) as *mut WrappedError;
633642

634643
ptr::write(ud, WrappedError(Error::CallbackDestructed));
635644
get_error_metatable(state);

tests/userdata.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use std::os::raw::c_int;
12
use std::sync::Arc;
2-
use std::os::raw::{c_int};
33

44
use rlua::{
55
AnyUserData, ExternalError, Function, Lua, MetaMethod, String, UserData, UserDataMethods,
@@ -183,7 +183,9 @@ fn detroys_userdata() {
183183
fn user_value() {
184184
struct MyUserData;
185185
impl UserData for MyUserData {
186-
fn get_uvalues_count(&self) -> c_int {2}
186+
fn get_uvalues_count(&self) -> c_int {
187+
2
188+
}
187189
}
188190

189191
Lua::new().context(|lua| {

0 commit comments

Comments
 (0)