low_luajit/lua/type/nil.rs
1use crate::{AmountReturned, LuaState, ffi};
2
3/// Nil (& none) related functions.
4impl LuaState {
5 /// Returns `true` if the value at the given acceptable `index` is `nil`, `false` otherwise.
6 #[inline]
7 #[must_use]
8 pub fn is_nil(&self, index: i32) -> bool {
9 // SAFETY: self.0 is always valid.
10 unsafe { ffi::lua_isnil(self.0, index) != 0 }
11 }
12
13 /// Returns `true` if the value at the given acceptable `index` is invalid (out of bounds), `false` otherwise.
14 #[inline]
15 #[must_use]
16 pub fn is_none(&self, index: i32) -> bool {
17 // SAFETY: self.0 is always valid.
18 unsafe { ffi::lua_isnone(self.0, index) != 0 }
19 }
20
21 /// Returns `true` if the value at the given acceptable `index` is `nil` or invalid (out of bounds), `false` otherwise.
22 #[inline]
23 #[must_use]
24 pub fn is_none_or_nil(&self, index: i32) -> bool {
25 // SAFETY: self.0 is always valid.
26 unsafe { ffi::lua_isnoneornil(self.0, index) != 0 }
27 }
28
29 /// Pushes `nil` onto the stack.
30 #[inline]
31 pub fn push_nil(&mut self) {
32 // SAFETY: self.0 is always valid.
33 unsafe { ffi::lua_pushnil(self.0); }
34 }
35
36 /// Finishes the currently called [`CFunction`], returning `nil` to the caller.
37 ///
38 /// The `LuaState` wrapper is consumed to prevent invalidating the [`AmountReturned`] (the Lua state itself is left intact).
39 ///
40 /// To return no value at all instead of `nil`, see [`LuaState::return_none`].
41 /// *(There is a small difference, but it's usually negligible.)*
42 ///
43 /// [`CFunction`]: ./type.CFunction.html
44 #[inline]
45 pub fn return_nil(mut self) -> AmountReturned {
46 self.push_nil();
47
48 // SAFETY: There should always be at least one element in the stack.
49 unsafe { AmountReturned::unchecked(1) }
50 }
51
52 /// Finishes the currently called [`CFunction`], returning no value to the caller.
53 ///
54 /// This is currently effectively the same as just returning [`AmountReturned::NONE`].
55 ///
56 /// To return `nil` instead of no value at all, see [`LuaState::return_nil`].
57 /// *(There is a small difference, but it's usually negligible.)*
58 ///
59 /// [`CFunction`]: ./type.CFunction.html
60 #[inline]
61 pub fn return_none(self) -> AmountReturned {
62 AmountReturned::NONE
63 }
64}