use core::ptr;
use crate::ffi::{self, lua_State};
#[repr(transparent)]
#[derive(Debug, PartialEq, Eq)]
pub struct LuaState(pub(super) *mut ffi::lua_State);
impl LuaState {
#[must_use]
pub const unsafe fn from_ptr(ptr: *mut lua_State) -> Self {
Self(ptr)
}
#[must_use]
pub const fn as_ptr(&self) -> *mut lua_State {
self.0
}
pub unsafe fn access_mut<T>(&self, f: impl FnOnce(&mut Self) -> T) -> T {
#[cfg(debug_assertions)]
let top = self.get_top();
let mut lua = unsafe { Self::from_ptr(self.0) };
let result = f(&mut lua);
#[cfg(debug_assertions)]
debug_assert!(top == self.get_top(), "mutably accessed the Lua state as the stack held {top} elements, but left it with {} elements instead", self.get_top());
result
}
#[inline]
pub unsafe fn close(self) {
unsafe { ffi::lua_close(self.0); }
}
#[inline]
pub fn version() -> &'static f64 {
unsafe { &*ffi::lua_version(ptr::null_mut()) }
}
}