Created
August 12, 2015 14:36
-
-
Save fkaa/5f6d15290d7dc4774a9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use ffi; | |
use LuaContext; | |
use std::ffi::CString; | |
use std::slice; | |
use std::str; | |
use std::mem; | |
pub trait Read { | |
fn read(cxt: &mut LuaContext, idx: i32) -> Self; | |
} | |
macro_rules! integer_read { | |
($ty:ident) => ( | |
impl Read for $ty { | |
fn read(cxt: &mut LuaContext, idx: i32) -> Self { | |
unsafe { ffi::lua_tointeger(cxt.handle, idx) as Self } | |
} | |
} | |
) | |
} | |
integer_read!(i8); | |
integer_read!(i16); | |
integer_read!(i32); | |
macro_rules! number_read { | |
($ty:ident) => ( | |
impl Read for $ty { | |
fn read(cxt: &mut LuaContext, idx: i32) -> Self { | |
unsafe { ffi::lua_tonumber(cxt.handle, idx) as Self } | |
} | |
} | |
) | |
} | |
number_read!(f32); | |
number_read!(f64); | |
impl<'a> Read for &'a str { | |
fn read(cxt: &mut LuaContext, idx: i32) -> Self { | |
unsafe { | |
let slice = { | |
let mut size = 0; | |
let cs = ffi::lua_tolstring(cxt.handle, idx, &mut size); | |
slice::from_raw_parts(cs, size as usize) | |
}; | |
str::from_utf8(mem::transmute(slice)).unwrap() | |
} | |
} | |
} | |
impl Read for String { | |
fn read(cxt: &mut LuaContext, idx: i32) -> Self { | |
unsafe { | |
let slice = { | |
let mut size = 0; | |
let cs = ffi::lua_tolstring(cxt.handle, idx, &mut size); | |
slice::from_raw_parts(cs, size as usize - 1) | |
}; | |
String::from_utf8_lossy(mem::transmute(slice)).into_owned() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment