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