Do several more Lua prefix renames, add prelude module

Rename the following:

LuaNil => Nil
LuaExternalError => ExternalError
LuaExternalResult => ExternalResult
LuaCallback => Callback (internal only)

Use qualified re-exports at the top of the module.

Add a new public 'prelude' module which re-exports everything with a
non-conflicting name (Adds back the Lua prefix), and is meant to be imported
unqualified.
This commit is contained in:
kyren 2017-07-24 07:12:52 -04:00
parent 8f044755dc
commit c7d2311c6a
7 changed files with 37 additions and 28 deletions

View File

@ -288,7 +288,7 @@ impl<'lua, T: ToLua<'lua>> ToLua<'lua> for Option<T> {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> { fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
match self { match self {
Some(val) => val.to_lua(lua), Some(val) => val.to_lua(lua),
None => Ok(LuaNil), None => Ok(Nil),
} }
} }
} }
@ -296,7 +296,7 @@ impl<'lua, T: ToLua<'lua>> ToLua<'lua> for Option<T> {
impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Option<T> { impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Option<T> {
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> { fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
match value { match value {
LuaNil => Ok(None), Nil => Ok(None),
value => Ok(Some(T::from_lua(value, lua)?)), value => Ok(Some(T::from_lua(value, lua)?)),
} }
} }

View File

@ -94,11 +94,11 @@ impl Error {
} }
} }
pub trait LuaExternalError { pub trait ExternalError {
fn to_lua_err(self) -> Error; fn to_lua_err(self) -> Error;
} }
impl<E> LuaExternalError for E impl<E> ExternalError for E
where where
E: Into<Box<error::Error + Send + Sync>>, E: Into<Box<error::Error + Send + Sync>>,
{ {
@ -122,13 +122,13 @@ where
} }
} }
pub trait LuaExternalResult<T> { pub trait ExternalResult<T> {
fn to_lua_err(self) -> Result<T>; fn to_lua_err(self) -> Result<T>;
} }
impl<T, E> LuaExternalResult<T> for ::std::result::Result<T, E> impl<T, E> ExternalResult<T> for ::std::result::Result<T, E>
where where
E: LuaExternalError, E: ExternalError,
{ {
fn to_lua_err(self) -> Result<T> { fn to_lua_err(self) -> Result<T> {
self.map_err(|e| e.to_lua_err()) self.map_err(|e| e.to_lua_err())

View File

@ -12,6 +12,10 @@ mod multi;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub use error::*; pub use error::{Error, Result, ExternalError, ExternalResult};
pub use lua::*; pub use lua::{Value, Nil, ToLua, FromLua, MultiValue, ToLuaMulti, FromLuaMulti, Integer, Number,
pub use multi::*; LightUserData, String, Table, TablePairs, TableSequence, Function, ThreadStatus,
Thread, MetaMethod, UserDataMethods, UserData, AnyUserData, Lua};
pub use multi::Variadic;
pub mod prelude;

View File

@ -48,7 +48,7 @@ pub enum Value<'lua> {
/// it is implicitly cloned. /// it is implicitly cloned.
Error(Error), Error(Error),
} }
pub use self::Value::Nil as LuaNil; pub use self::Value::Nil as Nil;
/// Trait for types convertible to `Value`. /// Trait for types convertible to `Value`.
pub trait ToLua<'a> { pub trait ToLua<'a> {
@ -124,10 +124,7 @@ pub trait FromLuaMulti<'a>: Sized {
fn from_lua_multi(values: MultiValue<'a>, lua: &'a Lua) -> Result<Self>; fn from_lua_multi(values: MultiValue<'a>, lua: &'a Lua) -> Result<Self>;
} }
type LuaCallback<'lua> = Box< type Callback<'lua> = Box<FnMut(&'lua Lua, MultiValue<'lua>) -> Result<MultiValue<'lua>> + 'lua>;
FnMut(&'lua Lua, MultiValue<'lua>) -> Result<MultiValue<'lua>>
+ 'lua,
>;
struct LuaRef<'lua> { struct LuaRef<'lua> {
lua: &'lua Lua, lua: &'lua Lua,
@ -755,8 +752,8 @@ pub enum MetaMethod {
/// `Index` metamethod is given, it will be called as a *fallback* if the index doesn't match an /// `Index` metamethod is given, it will be called as a *fallback* if the index doesn't match an
/// existing regular method. /// existing regular method.
pub struct UserDataMethods<'lua, T> { pub struct UserDataMethods<'lua, T> {
methods: HashMap<StdString, LuaCallback<'lua>>, methods: HashMap<StdString, Callback<'lua>>,
meta_methods: HashMap<MetaMethod, LuaCallback<'lua>>, meta_methods: HashMap<MetaMethod, Callback<'lua>>,
_type: PhantomData<T>, _type: PhantomData<T>,
} }
@ -823,7 +820,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
self.meta_methods.insert(meta, Box::new(function)); self.meta_methods.insert(meta, Box::new(function));
} }
fn box_method<M>(mut method: M) -> LuaCallback<'lua> fn box_method<M>(mut method: M) -> Callback<'lua>
where where
M: 'lua + for<'a> FnMut(&'lua Lua, &'a T, MultiValue<'lua>) -> Result<MultiValue<'lua>>, M: 'lua + for<'a> FnMut(&'lua Lua, &'a T, MultiValue<'lua>) -> Result<MultiValue<'lua>>,
{ {
@ -840,7 +837,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
} }
fn box_method_mut<M>(mut method: M) -> LuaCallback<'lua> fn box_method_mut<M>(mut method: M) -> Callback<'lua>
where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>)
-> Result<MultiValue<'lua>> -> Result<MultiValue<'lua>>
{ {
@ -993,7 +990,7 @@ impl Lua {
ffi::lua_newtable(state); ffi::lua_newtable(state);
push_string(state, "__gc"); push_string(state, "__gc");
ffi::lua_pushcfunction(state, userdata_destructor::<LuaCallback>); ffi::lua_pushcfunction(state, userdata_destructor::<Callback>);
ffi::lua_rawset(state, -3); ffi::lua_rawset(state, -3);
push_string(state, "__metatable"); push_string(state, "__metatable");
@ -1304,7 +1301,7 @@ impl Lua {
T::from_lua_multi(value, self) T::from_lua_multi(value, self)
} }
fn create_callback_function<'lua>(&'lua self, func: LuaCallback<'lua>) -> Function<'lua> { fn create_callback_function<'lua>(&'lua self, func: Callback<'lua>) -> Function<'lua> {
unsafe extern "C" fn callback_call_impl(state: *mut ffi::lua_State) -> c_int { unsafe extern "C" fn callback_call_impl(state: *mut ffi::lua_State) -> c_int {
callback_error(state, || { callback_error(state, || {
let lua = Lua { let lua = Lua {
@ -1313,7 +1310,7 @@ impl Lua {
ephemeral: true, ephemeral: true,
}; };
let func = &mut *get_userdata::<LuaCallback>(state, ffi::lua_upvalueindex(1)); let func = &mut *get_userdata::<Callback>(state, ffi::lua_upvalueindex(1));
let nargs = ffi::lua_gettop(state); let nargs = ffi::lua_gettop(state);
let mut args = MultiValue::new(); let mut args = MultiValue::new();
@ -1336,7 +1333,7 @@ impl Lua {
stack_guard(self.state, 0, move || { stack_guard(self.state, 0, move || {
check_stack(self.state, 2); check_stack(self.state, 2);
push_userdata::<LuaCallback>(self.state, func); push_userdata::<Callback>(self.state, func);
ffi::lua_pushlightuserdata( ffi::lua_pushlightuserdata(
self.state, self.state,
@ -1404,7 +1401,7 @@ impl Lua {
match ffi::lua_type(state, -1) { match ffi::lua_type(state, -1) {
ffi::LUA_TNIL => { ffi::LUA_TNIL => {
ffi::lua_pop(state, 1); ffi::lua_pop(state, 1);
LuaNil Nil
} }
ffi::LUA_TBOOLEAN => { ffi::LUA_TBOOLEAN => {

View File

@ -24,7 +24,7 @@ impl<'lua, T: ToLua<'lua>, E: ToLua<'lua>> ToLuaMulti<'lua> for ::std::result::R
match self { match self {
Ok(v) => result.push_back(v.to_lua(lua)?), Ok(v) => result.push_back(v.to_lua(lua)?),
Err(e) => { Err(e) => {
result.push_back(LuaNil); result.push_back(Nil);
result.push_back(e.to_lua(lua)?); result.push_back(e.to_lua(lua)?);
} }
} }
@ -43,7 +43,7 @@ impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for T {
impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for T { impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for T {
fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> { fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
Ok(T::from_lua(values.pop_front().unwrap_or(LuaNil), lua)?) Ok(T::from_lua(values.pop_front().unwrap_or(Nil), lua)?)
} }
} }
@ -119,7 +119,7 @@ impl<'lua, H: FromLua<'lua>, A, B> FromLuaMulti<'lua> for HCons<H, HCons<A, B>>
where HCons<A, B>: FromLuaMulti<'lua> where HCons<A, B>: FromLuaMulti<'lua>
{ {
fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> { fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
let val = H::from_lua(values.pop_front().unwrap_or(LuaNil), lua)?; let val = H::from_lua(values.pop_front().unwrap_or(Nil), lua)?;
let res = HCons::<A, B>::from_lua_multi(values, lua)?; let res = HCons::<A, B>::from_lua_multi(values, lua)?;
Ok(HCons(val, res)) Ok(HCons(val, res))
} }

8
src/prelude.rs Normal file
View File

@ -0,0 +1,8 @@
pub use {Error as LuaError, Result as LuaResult, ExternalError as LuaExternalError,
ExternalResult as LuaExternalResult, Value as LuaValue, Nil as LuaNil, ToLua, FromLua,
MultiValue as LuaMultiValue, ToLuaMulti, FromLuaMulti, Integer as LuaInteger,
Number as LuaNumber, LightUserData as LuaLightUserData, String as LuaString,
Table as LuaTable, TablePairs as LuaTablePairs, TableSequence as LuaTableSequence,
Function as LuaFunction, ThreadStatus as LuaThreadStatus, Thread as LuaThread,
MetaMethod as LuaMetaMethod, UserDataMethods as LuaUserDataMethods,
UserData as LuaUserData, AnyUserData as LuaAnyUserData, Lua};

View File

@ -5,7 +5,7 @@ use std::os::raw::c_void;
use String as LuaString; use String as LuaString;
use { use {
Lua, Result, LuaExternalError, LightUserData, UserDataMethods, UserData, Table, Thread, Lua, Result, ExternalError, LightUserData, UserDataMethods, UserData, Table, Thread,
ThreadStatus, Error, Function, Value, Variadic, MetaMethod ThreadStatus, Error, Function, Value, Variadic, MetaMethod
}; };