use ::{State, Integer, Number, Function, Index};
pub trait ToLua {
fn to_lua(&self, state: &mut State);
}
impl<'a> ToLua for &'a str {
fn to_lua(&self, state: &mut State) {
state.push_string(*self);
}
}
impl<'a> ToLua for &'a [u8] {
fn to_lua(&self, state: &mut State) {
state.push_bytes(*self);
}
}
impl ToLua for String {
fn to_lua(&self, state: &mut State) {
state.push_string(&self);
}
}
impl ToLua for Integer {
fn to_lua(&self, state: &mut State) {
state.push_integer(*self)
}
}
impl ToLua for Number {
fn to_lua(&self, state: &mut State) {
state.push_number(*self)
}
}
impl ToLua for bool {
fn to_lua(&self, state: &mut State) {
state.push_bool(*self)
}
}
impl ToLua for Function {
fn to_lua(&self, state: &mut State) {
state.push_fn(*self)
}
}
impl<T> ToLua for *mut T {
fn to_lua(&self, state: &mut State) {
unsafe { state.push_light_userdata(*self) }
}
}
impl<T: ToLua> ToLua for Option<T> {
fn to_lua(&self, state: &mut State) {
match *self {
Some(ref value) => value.to_lua(state),
None => state.push_nil(),
}
}
}
pub trait FromLua: Sized {
fn from_lua(state: &mut State, index: Index) -> Option<Self>;
}
impl FromLua for String {
fn from_lua(state: &mut State, index: Index) -> Option<String> {
state.to_str(index).map(ToOwned::to_owned)
}
}
impl FromLua for Vec<u8> {
fn from_lua(state: &mut State, index: Index) -> Option<Vec<u8>> {
state.to_bytes_in_place(index).map(ToOwned::to_owned)
}
}
impl FromLua for Integer {
fn from_lua(state: &mut State, index: Index) -> Option<Integer> {
if state.is_integer(index) {
Some(state.to_integer(index))
} else {
None
}
}
}
impl FromLua for Number {
fn from_lua(state: &mut State, index: Index) -> Option<Number> {
if state.is_number(index) {
Some(state.to_number(index))
} else {
None
}
}
}
impl FromLua for bool {
fn from_lua(state: &mut State, index: Index) -> Option<bool> {
if state.is_bool(index) {
Some(state.to_bool(index))
} else {
None
}
}
}
impl FromLua for Function {
fn from_lua(state: &mut State, index: Index) -> Option<Function> {
if state.is_native_fn(index) {
Some(state.to_native_fn(index))
} else {
None
}
}
}