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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::ffi::{c_char, c_int};

use crate::ffi::{self, lua_Integer, lua_Number, lua_State};
use crate::macros::count;

/// Trait implemented for types that can be pushed onto the Lua stack.
pub trait Pushable {
    /// Pushes all its values on the Lua stack, returning the number of values
    /// that it pushed.
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error>;
}

impl Pushable for () {
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error> {
        ffi::lua_pushnil(lstate);
        Ok(1)
    }
}

impl Pushable for bool {
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error> {
        ffi::lua_pushboolean(lstate, self as _);
        Ok(1)
    }
}

impl Pushable for lua_Integer {
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error> {
        ffi::lua_pushinteger(lstate, self);
        Ok(1)
    }
}

/// Implements `LuaPushable` for an integer type that implements
/// `Into<lua_Integer>`.
macro_rules! push_into_integer {
    ($integer:ty) => {
        impl Pushable for $integer {
            unsafe fn push(
                self,
                lstate: *mut lua_State,
            ) -> Result<c_int, crate::Error> {
                let n: lua_Integer = self.into();
                n.push(lstate)
            }
        }
    };
}

/// Implements `LuaPushable` for an integer type that implements
/// `TryInto<lua_Integer>`.
macro_rules! push_try_into_integer {
    ($integer:ty) => {
        impl Pushable for $integer {
            unsafe fn push(
                self,
                lstate: *mut lua_State,
            ) -> Result<c_int, crate::Error> {
                let n: lua_Integer = self.try_into().map_err(
                    |err: std::num::TryFromIntError| {
                        crate::Error::push_error(
                            std::any::type_name::<$integer>(),
                            err.to_string(),
                        )
                    },
                )?;
                n.push(lstate)
            }
        }
    };
}

push_into_integer!(i8);
push_into_integer!(u8);
push_into_integer!(i16);
push_try_into_integer!(u16);
push_try_into_integer!(i32);
push_try_into_integer!(u32);
push_try_into_integer!(i64);
push_try_into_integer!(u64);
push_try_into_integer!(usize);

impl Pushable for lua_Number {
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error> {
        ffi::lua_pushnumber(lstate, self);
        Ok(1)
    }
}

impl Pushable for f32 {
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error> {
        (self as lua_Number).push(lstate)
    }
}

impl Pushable for String {
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error> {
        ffi::lua_pushlstring(
            lstate,
            self.as_ptr() as *const c_char,
            self.len(),
        );
        Ok(1)
    }
}

impl<T> Pushable for Option<T>
where
    T: Pushable,
{
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error> {
        match self {
            Some(t) => t.push(lstate),
            None => ().push(lstate),
        }
    }
}

impl<T> Pushable for Vec<T>
where
    T: Pushable,
{
    unsafe fn push(
        self,
        lstate: *mut lua_State,
    ) -> Result<c_int, crate::Error> {
        ffi::lua_createtable(lstate, self.len() as _, 0);

        for (i, obj) in self.into_iter().enumerate() {
            obj.push(lstate)?;
            ffi::lua_rawseti(lstate, -2, (i + 1) as _);
        }

        Ok(1)
    }
}

/// Implements `LuaPushable` for a tuple `(a, b, c, ..)` where all the elements
/// in the tuple implement `LuaPushable`.
macro_rules! push_tuple {
    ($($name:ident)*) => {
        impl<$($name,)*> Pushable for ($($name,)*)
        where
            $($name: Pushable,)*
        {
            #[allow(non_snake_case)]
            unsafe fn push(
                self,
                lstate: *mut lua_State,
            ) -> Result<c_int, crate::Error> {
                let ($($name,)*) = self;
                $($name.push(lstate)?;)*
                Ok(count!($($name)*))
            }
        }
    }
}

push_tuple!(A);
push_tuple!(A B);
push_tuple!(A B C);
push_tuple!(A B C D);
push_tuple!(A B C D E);
push_tuple!(A B C D E F);
push_tuple!(A B C D E F G);
push_tuple!(A B C D E F G H);
push_tuple!(A B C D E F G H I);
push_tuple!(A B C D E F G H I J);
push_tuple!(A B C D E F G H I J K);
push_tuple!(A B C D E F G H I J K L);
push_tuple!(A B C D E F G H I J K L M);
push_tuple!(A B C D E F G H I J K L M N);
push_tuple!(A B C D E F G H I J K L M N O);
push_tuple!(A B C D E F G H I J K L M N O P);