low-luajit 0.5.0

Low level but safe bindings to the LuaJIT C API with no dependencies and no-std support to create Lua modules using Rust.
Documentation
use crate::{AmountReturned, LuaState, ffi};

/*
#[repr(i32)]
#[derive(Debug)]
pub enum ThreadStatus {
	/// This thread is operational.
	Ok = ffi::LUA_OK,

	/// This thread is suspended.
	Yielded = ffi::LUA_YIELD,

	/// This thread encountered an error while running.
	RuntimeError = ffi::LUA_ERRRUN,


}*/

/// Thread related functions.
impl LuaState {
	/// Returns `true` if the value at the given acceptable `index` is a thread, `false` otherwise.
	#[inline]
	#[must_use]
	pub fn is_thread(&self, index: i32) -> bool {
		// SAFETY: self.as_ptr() is always valid.
		unsafe { ffi::lua_isthread(self.as_ptr(), index) != 0 }
	}

	/// Returns `true` if the thread represented by this `LuaState` is the main thread of its state, `false` otherwise.
	#[inline]
	#[must_use]
	pub fn is_main_thread(&self) -> bool {
		// SAFETY: No elements are popped except the one element that is pushed by this function.
		unsafe { self.access_mut(|lua| {
			let main = lua.push_thread();
			lua.pop_unchecked(1);
			main
		}) }
	}

	/// Pushes the thread represented by this `LuaState` onto the stack.
	///
	/// Returns `true` if this thread is the main thread of its state, `false` otherwise.
	#[inline]
	pub fn push_thread(&mut self) -> bool {
		// SAFETY: self.as_ptr() is always valid.
		unsafe { ffi::lua_pushthread(self.as_ptr()) != 0 }
	}

	/// Finishes the currently called [`CFunction`] similarly to [`LuaState::return_any`], while also suspending the running coroutine.
	/// Once the coroutine is resumed again, it will resume from the [`CFunction`]'s caller.
	///
	/// The returned values will be returned to whoever had resumed the coroutine rather than the caller.
	///
	/// The `LuaState` wrapper is consumed to prevent invalidating the `AmountReturned` (the Lua state itself is left intact).
	///
	/// Unlike [`LuaState::return_any`],
	/// `amount` may be negative or higher than the amount of elements currently held in the stack without panics.
	///
	/// # Lua Errors
	///
	/// Errors if called from the main thread *(and thus not a coroutine)*.
	///
	/// [`CFunction`]: ./type.CFunction.html
	#[inline]
	pub fn yield_any(self, amount: i32) -> AmountReturned {
		// SAFETY: self.as_ptr() is always valid and when yielding you are supposed to return -1.
		unsafe { AmountReturned::unchecked(ffi::lua_yield(self.as_ptr(), amount)) }
	}
}