elua/src/util.rs

27 lines
734 B
Rust
Raw Normal View History

use std::ffi::CStr;
use crate::State;
use mlua_sys as lua;
pub unsafe fn ensure_type(state: &State, typ: i32, idx: i32) -> crate::Result<()> {
let s = state.state_ptr();
let lua_type = lua::lua_type(s, idx);
if lua_type == typ {
Ok(())
} else {
let exp_tyname = lua::lua_typename(s, typ);
let exp_cstr = CStr::from_ptr(exp_tyname);
let exp_s = exp_cstr.to_str()
.expect("Lua type has invalid bytes!");
let tyname = lua::lua_typename(s, lua_type);
let cstr = CStr::from_ptr(tyname);
let s = cstr.to_str()
.expect("Lua type has invalid bytes!");
Err(crate::Error::UnexpectedType(exp_s.to_string(), s.to_string()))
}
}