From c5a4dfd7ebed829109a320ae803cefb8d5168fc7 Mon Sep 17 00:00:00 2001 From: kyren Date: Sat, 30 Sep 2017 01:27:18 -0400 Subject: [PATCH] more reorganization, move simple type defines to types.rs module --- src/conversion.rs | 3 +- src/lib.rs | 8 +++-- src/lua.rs | 43 ++------------------------- src/string.rs | 2 +- src/table.rs | 5 ++-- src/types.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++++ src/userdata.rs | 32 ++------------------ 7 files changed, 93 insertions(+), 76 deletions(-) create mode 100644 src/types.rs diff --git a/src/conversion.rs b/src/conversion.rs index 78dbc82..556c736 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -3,10 +3,11 @@ use std::hash::Hash; use std::string::String as StdString; use error::*; +use types::{Integer, Number, LightUserData}; use lua::*; use string::String; use table::Table; -use userdata::{LightUserData, UserData, AnyUserData}; +use userdata::{UserData, AnyUserData}; impl<'lua> ToLua<'lua> for Value<'lua> { fn to_lua(self, _: &'lua Lua) -> Result> { diff --git a/src/lib.rs b/src/lib.rs index f07756e..052bcf3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,6 +46,7 @@ pub mod ffi; #[macro_use] mod util; mod error; +mod types; mod lua; mod conversion; mod multi; @@ -57,11 +58,12 @@ mod userdata; mod tests; pub use error::{Error, Result, ExternalError, ExternalResult}; -pub use lua::{Value, Nil, ToLua, FromLua, MultiValue, ToLuaMulti, FromLuaMulti, Integer, Number, - Function, ThreadStatus, Thread, Lua}; +pub use types::{Integer, Number, LightUserData}; pub use multi::Variadic; pub use string::String; pub use table::{Table, TablePairs, TableSequence}; -pub use userdata::{LightUserData, MetaMethod, UserDataMethods, UserData, AnyUserData}; +pub use userdata::{MetaMethod, UserDataMethods, UserData, AnyUserData}; +pub use lua::{Value, Nil, ToLua, FromLua, MultiValue, ToLuaMulti, FromLuaMulti, Function, + ThreadStatus, Thread, Lua}; pub mod prelude; diff --git a/src/lua.rs b/src/lua.rs index a6b8074..b8e0744 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -1,4 +1,4 @@ -use std::{fmt, ptr, str}; +use std::{ptr, str}; use std::ops::{Deref, DerefMut}; use std::iter::FromIterator; use std::cell::RefCell; @@ -15,9 +15,10 @@ use libc; use ffi; use error::*; use util::*; +use types::{Integer, Number, LightUserData, Callback, LuaRef}; use string::String; use table::Table; -use userdata::{LightUserData, UserDataMethods, MetaMethod, UserData, AnyUserData}; +use userdata::{UserDataMethods, MetaMethod, UserData, AnyUserData}; /// A dynamically typed Lua value. #[derive(Debug, Clone)] @@ -145,44 +146,6 @@ pub trait FromLuaMulti<'lua>: Sized { fn from_lua_multi(values: MultiValue<'lua>, lua: &'lua Lua) -> Result; } -pub(crate) type Callback<'lua> = Box< - FnMut(&'lua Lua, MultiValue<'lua>) -> Result> - + 'lua, ->; - -pub(crate) struct LuaRef<'lua> { - pub lua: &'lua Lua, - pub registry_id: c_int, -} - -impl<'lua> fmt::Debug for LuaRef<'lua> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "LuaRef({})", self.registry_id) - } -} - -impl<'lua> Clone for LuaRef<'lua> { - fn clone(&self) -> Self { - unsafe { - self.lua.push_ref(self.lua.state, self); - self.lua.pop_ref(self.lua.state) - } - } -} - -impl<'lua> Drop for LuaRef<'lua> { - fn drop(&mut self) { - unsafe { - ffi::luaL_unref(self.lua.state, ffi::LUA_REGISTRYINDEX, self.registry_id); - } - } -} - -/// Type of Lua integer numbers. -pub type Integer = ffi::lua_Integer; -/// Type of Lua floating point numbers. -pub type Number = ffi::lua_Number; - /// Handle to an internal Lua function. #[derive(Clone, Debug)] pub struct Function<'lua>(LuaRef<'lua>); diff --git a/src/string.rs b/src/string.rs index 55da706..bef7d2d 100644 --- a/src/string.rs +++ b/src/string.rs @@ -1,9 +1,9 @@ use std::{slice, str}; use ffi; -use lua::LuaRef; use error::{Error, Result}; use util::{check_stack, stack_guard}; +use types::LuaRef; /// Handle to an internal Lua string. /// diff --git a/src/table.rs b/src/table.rs index f82db87..d74f04d 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,9 +1,10 @@ use std::marker::PhantomData; use ffi; -use lua::{LuaRef, ToLua, FromLua, Integer}; -use util::*; use error::Result; +use util::*; +use types::{Integer, LuaRef}; +use lua::{ToLua, FromLua}; /// Handle to an internal Lua table. #[derive(Clone, Debug)] diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..6385ec8 --- /dev/null +++ b/src/types.rs @@ -0,0 +1,76 @@ +use std::fmt; +use std::os::raw::{c_int, c_void}; + +use ffi; +use error::Result; +use lua::{MultiValue, Lua}; + +/// Type of Lua integer numbers. +pub type Integer = ffi::lua_Integer; +/// Type of Lua floating point numbers. +pub type Number = ffi::lua_Number; + +/// A "light" userdata value. Equivalent to an unmanaged raw pointer. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub struct LightUserData(pub *mut c_void); + +pub(crate) type Callback<'lua> = Box< + FnMut(&'lua Lua, MultiValue<'lua>) -> Result> + + 'lua, +>; + +pub(crate) struct LuaRef<'lua> { + pub lua: &'lua Lua, + pub registry_id: c_int, +} + +impl<'lua> fmt::Debug for LuaRef<'lua> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "LuaRef({})", self.registry_id) + } +} + +impl<'lua> Clone for LuaRef<'lua> { + fn clone(&self) -> Self { + unsafe { + self.lua.push_ref(self.lua.state, self); + self.lua.pop_ref(self.lua.state) + } + } +} + +impl<'lua> Drop for LuaRef<'lua> { + fn drop(&mut self) { + unsafe { + ffi::luaL_unref(self.lua.state, ffi::LUA_REGISTRYINDEX, self.registry_id); + } + } +} + +#[cfg(test)] +mod tests { + use super::LightUserData; + use lua::{Function, Lua}; + + use std::os::raw::c_void; + + #[test] + fn test_lightuserdata() { + let lua = Lua::new(); + let globals = lua.globals(); + lua.exec::<()>( + r#" + function id(a) + return a + end + "#, + None, + ).unwrap(); + let res = globals + .get::<_, Function>("id") + .unwrap() + .call::<_, LightUserData>(LightUserData(42 as *mut c_void)) + .unwrap(); + assert_eq!(res, LightUserData(42 as *mut c_void)); + } +} diff --git a/src/userdata.rs b/src/userdata.rs index 7b0d40b..3aee9bc 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -1,17 +1,13 @@ use std::cell::{RefCell, Ref, RefMut}; use std::marker::PhantomData; use std::collections::HashMap; -use std::os::raw::c_void; use std::string::String as StdString; use ffi; use error::*; use util::*; -use lua::{FromLua, FromLuaMulti, ToLuaMulti, Callback, LuaRef, Lua}; - -/// A "light" userdata value. Equivalent to an unmanaged raw pointer. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct LightUserData(pub *mut c_void); +use types::{Callback, LuaRef}; +use lua::{FromLua, FromLuaMulti, ToLuaMulti, Lua}; /// Kinds of metamethods that can be overridden. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] @@ -400,33 +396,11 @@ impl<'lua> AnyUserData<'lua> { #[cfg(test)] mod tests { - use super::{LightUserData, UserData, MetaMethod, UserDataMethods}; + use super::{UserData, MetaMethod, UserDataMethods}; use error::ExternalError; use string::String; use lua::{Function, Lua}; - use std::os::raw::c_void; - - #[test] - fn test_lightuserdata() { - let lua = Lua::new(); - let globals = lua.globals(); - lua.exec::<()>( - r#" - function id(a) - return a - end - "#, - None, - ).unwrap(); - let res = globals - .get::<_, Function>("id") - .unwrap() - .call::<_, LightUserData>(LightUserData(42 as *mut c_void)) - .unwrap(); - assert_eq!(res, LightUserData(42 as *mut c_void)); - } - #[test] fn test_user_data() { struct UserData1(i64);