Skip to main content

luaur_rt/
traits.rs

1//! The conversion traits. Mirror `mlua::{IntoLua, FromLua, IntoLuaMulti,
2//! FromLuaMulti}`.
3//!
4//! Like mlua, the traits also carry **internal, stack-oriented** hooks
5//! (`from_stack`, `from_stack_multi`) used by the call/eval plumbing to skip
6//! the intermediate [`Value`] representation for hot types (see
7//! `lua_convert_float!` in mlua: `f64`/`f32` read the raw `lua_Number`
8//! straight off the stack, which also preserves details `Value` folds away,
9//! e.g. the sign bit of `-0.0`). These are `#[doc(hidden)]`: the public
10//! surface is the value-level methods, whose *names and shapes*
11//! (`into_lua(self, &Lua)`, `from_lua(value, &Lua)`, `into_lua_multi`,
12//! `from_lua_multi`) match mlua exactly.
13//!
14//! Note these hooks take `&Lua` rather than mlua's internal `&RawLua`: luaur
15//! has no separate raw handle.
16
17use crate::error::Result;
18use crate::multi::MultiValue;
19use crate::state::Lua;
20use crate::value::Value;
21use core::ffi::c_int;
22
23/// Convert a Rust value into a single Lua [`Value`].
24///
25/// Mirrors `mlua::IntoLua`.
26pub trait IntoLua: Sized {
27    /// Perform the conversion.
28    fn into_lua(self, lua: &Lua) -> Result<Value>;
29}
30
31/// Convert a single Lua [`Value`] into a Rust value.
32///
33/// Mirrors `mlua::FromLua`.
34pub trait FromLua: Sized {
35    /// Perform the conversion.
36    fn from_lua(value: Value, lua: &Lua) -> Result<Self>;
37
38    /// Convert an argument at 1-based position `i`. The default forwards to
39    /// [`FromLua::from_lua`]; specific impls can produce nicer messages.
40    /// Mirrors `mlua::FromLua::from_lua_arg`.
41    fn from_lua_arg(arg: Value, _i: usize, _to: Option<&str>, lua: &Lua) -> Result<Self> {
42        Self::from_lua(arg, lua)
43    }
44
45    /// Internal: convert the value on the stack at index `idx` directly, the
46    /// way mlua's `FromLua::from_stack` does. The default materializes a
47    /// [`Value`] first (mirroring mlua's `lua.stack_value(idx, None)`);
48    /// impls with a stack fast path (floats) read the raw value instead.
49    /// Must not permanently mutate the stack (a temporary push is fine as
50    /// long as it is popped).
51    #[doc(hidden)]
52    #[inline]
53    unsafe fn from_stack(idx: c_int, lua: &Lua) -> Result<Self> {
54        Self::from_lua(lua.value_from_stack(idx)?, lua)
55    }
56}
57
58/// Convert a Rust value into a sequence of Lua values (multiple returns / args).
59///
60/// Mirrors `mlua::IntoLuaMulti`.
61pub trait IntoLuaMulti: Sized {
62    /// Perform the conversion.
63    fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue>;
64}
65
66/// Convert a sequence of Lua values into a Rust value.
67///
68/// Mirrors `mlua::FromLuaMulti`.
69pub trait FromLuaMulti: Sized {
70    /// Perform the conversion.
71    fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<Self>;
72
73    /// Internal: collect `nvals` results sitting on the stack at indices
74    /// `base+1 ..= base+nvals` directly. The default materializes each into
75    /// a [`Value`] and forwards to [`FromLuaMulti::from_lua_multi`]
76    /// (mirroring mlua's `FromLuaMulti::from_stack_multi` default).
77    /// Must not permanently mutate the stack.
78    #[doc(hidden)]
79    #[allow(unused_variables)]
80    unsafe fn from_stack_multi(base: c_int, nvals: c_int, lua: &Lua) -> Result<Self> {
81        let mut values = MultiValue::with_capacity(nvals.max(0) as usize);
82        for i in 0..nvals {
83            values.push_back(lua.value_from_stack(base + 1 + i)?);
84        }
85        Self::from_lua_multi(values, lua)
86    }
87}
88
89// Any single-value type is trivially a multi-value of length one.
90impl<T: IntoLua> IntoLuaMulti for T {
91    fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue> {
92        let mut m = MultiValue::with_capacity(1);
93        m.push_back(self.into_lua(lua)?);
94        Ok(m)
95    }
96}
97
98// And any single-value type can be parsed from the first of a multi-value
99// (extra values are ignored, matching Lua's "take what you need" calling
100// convention). The stack-multi default forwards to `from_stack` so types
101// with a stack fast path (floats) keep it on the multi-value path too.
102impl<T: FromLua> FromLuaMulti for T {
103    fn from_lua_multi(mut values: MultiValue, lua: &Lua) -> Result<Self> {
104        let v = values.pop_front().unwrap_or(Value::Nil);
105        T::from_lua(v, lua)
106    }
107
108    unsafe fn from_stack_multi(base: c_int, nvals: c_int, lua: &Lua) -> Result<Self> {
109        if nvals <= 0 {
110            return T::from_lua(Value::Nil, lua);
111        }
112        unsafe { T::from_stack(base + 1, lua) }
113    }
114}