low-luajit 0.2.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::{LuaState, ffi::{self, lua_gc}};

/// Garbage collector handling functions.
///
/// See [ยง2.10](https://www.lua.org/manual/5.1/manual.html#2.10) for more information on the garbage collector.
impl LuaState {
	/// Generic garbage collector controller function.
	///
	/// behavior changes based on `what`, use the `LUA_GC*` constants found in the [`ffi`] module.
	/// Invalid `what` values simply return `-1`.
	#[inline]
	pub fn gc(&self, what: i32, data: i32) -> i32 {
		// SAFETY: self.0 is always valid.
		unsafe { lua_gc(self.0, what, data) }
	}

	/// Stops the garbage collector.
	#[inline]
	pub fn gc_stop(&self) {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCSTOP, 0);
	}

	/// Restarts the garbage collector.
	#[inline]
	pub fn gc_restart(&self) {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCRESTART, 0);
	}

	/// Performs a full garbage collection cycle.
	#[inline]
	pub fn gc_collect(&self) {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCCOLLECT, 0);
	}

	/// Returns the amount of memory in use by Lua, in Kbytes.
	#[inline]
	#[must_use]
	pub fn gc_count(&self) -> i32 {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCCOUNT, 0)
	}

	/// Returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024.
	#[inline]
	#[must_use]
	pub fn gc_count_b(&self) -> i32 {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCCOUNTB, 0)
	}

	/// Performs an incremental step of garbage collection.
	///
	/// `size` controls the step "size" in a non-specific way (larger values -> larger steps).
	///
	/// This function returns `true` if it finished a garbage collection cycle, `false` otherwise.
	#[inline]
	pub fn gc_step(&self, size: i32) -> bool {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCSTEP, size) != 0
	}

	/// Sets `pause` as the new pause value of the collector, returning the previous value.
	#[inline]
	pub fn gc_set_pause(&self, pause: i32) -> i32 {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCSETPAUSE, pause)
	}

	/// Sets `step_multiplier` as the new step multiplier value of the collector, returning the previous value.
	#[inline]
	pub fn gc_set_step_multiplier(&self, step_multiplier: i32) -> i32 {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCSETSTEPMUL, step_multiplier)
	}

	/// Returns `true` if the garbage collector is currently running, `false` otherwise.
	#[inline]
	pub fn gc_is_running(&self) -> bool {
		// SAFETY: self.0 is always valid.
		self.gc(ffi::LUA_GCISRUNNING, 0) != 0
	}
}