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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use crate::{
    ffi,
    lua_tables::LuaTable,
    tuples::TuplePushError::{self, First, Other},
    AsLua, LuaRead, LuaState, Push, PushGuard, PushInto, PushOne, PushOneInto, ReadResult, Void,
    WrongType,
};

use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::{self, Debug};
use std::hash::Hash;
use std::iter;
use std::num::NonZeroI32;

#[inline]
pub(crate) fn push_iter<L, I>(lua: L, iterator: I) -> Result<PushGuard<L>, (PushIterErrorOf<I>, L)>
where
    L: AsLua,
    I: Iterator,
    <I as Iterator>::Item: PushInto<LuaState>,
{
    // creating empty table
    unsafe { ffi::lua_newtable(lua.as_lua()) };

    for (elem, index) in iterator.zip(1..) {
        let size = match elem.push_into_lua(lua.as_lua()) {
            Ok(pushed) => pushed.forget_internal(),
            Err((err, _)) => unsafe {
                // TODO(gmoshkin): return an error capturing this push guard
                // drop the lua table
                drop(PushGuard::new(lua.as_lua(), 1));
                return Err((PushIterError::ValuePushError(err), lua));
            },
        };

        match size {
            0 => continue,
            1 => {
                lua.as_lua().push_one(index).forget_internal();
                unsafe { ffi::lua_insert(lua.as_lua(), -2) }
                unsafe { ffi::lua_settable(lua.as_lua(), -3) }
            }
            2 => unsafe { ffi::lua_settable(lua.as_lua(), -3) },
            n => unsafe {
                // TODO(gmoshkin): return an error capturing this push guard
                // n + 1 == n values from the recent push + lua table
                drop(PushGuard::new(lua.as_lua(), n + 1));
                return Err((PushIterError::TooManyValues(n), lua));
            },
        }
    }

    unsafe { Ok(PushGuard::new(lua, 1)) }
}

pub type PushIterErrorOf<I> = PushIterError<<<I as Iterator>::Item as PushInto<LuaState>>::Err>;

#[derive(Debug, PartialEq, Eq)]
pub enum PushIterError<E> {
    TooManyValues(i32),
    ValuePushError(E),
}

impl<E> PushIterError<E> {
    pub fn map<F, R>(self, f: F) -> PushIterError<R>
    where
        F: FnOnce(E) -> R,
    {
        match self {
            Self::ValuePushError(e) => PushIterError::ValuePushError(f(e)),
            Self::TooManyValues(n) => PushIterError::TooManyValues(n),
        }
    }
}

impl<E> fmt::Display for PushIterError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::TooManyValues(n) => {
                write!(
                    fmt,
                    "Can only push 1 or 2 values as lua table item, got {} instead",
                    n,
                )
            }
            Self::ValuePushError(e) => {
                write!(fmt, "Pushing iterable item failed: {}", e)
            }
        }
    }
}

// NOTE: only the following From<_> for Void implementations are correct,
//       don't add other ones!

// T::Err: Void => no error possible
// NOTE: making this one generic would conflict with the below implementations.
impl From<PushIterError<Void>> for Void {
    fn from(_: PushIterError<Void>) -> Self {
        unreachable!("no way to create instance of Void")
    }
}

// T::Err: Void; (T,) => no error possible
impl<T> From<PushIterError<TuplePushError<T, Void>>> for Void
where
    T: Into<Void>,
{
    fn from(_: PushIterError<TuplePushError<T, Void>>) -> Self {
        unreachable!("no way to create instance of Void")
    }
}

// K::Err: Void; V::Err: Void; (K, V) => no error possible
impl<K, V> From<PushIterError<TuplePushError<K, TuplePushError<V, Void>>>> for Void
where
    K: Into<Void>,
    V: Into<Void>,
{
    fn from(_: PushIterError<TuplePushError<K, TuplePushError<V, Void>>>) -> Self {
        unreachable!("no way to create instance of Void")
    }
}

////////////////////////////////////////////////////////////////////////////////
// TableFromIter
////////////////////////////////////////////////////////////////////////////////

/// A wrapper struct for converting arbitrary iterators into lua tables. Use
/// this instead of converting the iterator into a `Vec` to avoid unnecessary
/// allocations
/// # Example
/// ```no_run
/// use std::io::BufRead;
/// let lua = tlua::Lua::new();
/// lua.set(
///     "foo",
///     tlua::TableFromIter(std::io::stdin().lock().lines().flatten()),
/// )
/// // Global variable 'foo' now contains an array of lines read from stdin
/// ```
pub struct TableFromIter<I>(pub I);

impl<L, I> PushInto<L> for TableFromIter<I>
where
    L: AsLua,
    I: Iterator,
    <I as Iterator>::Item: PushInto<LuaState>,
{
    type Err = PushIterError<<I::Item as PushInto<LuaState>>::Err>;

    fn push_into_lua(self, lua: L) -> crate::PushIntoResult<L, Self> {
        push_iter(lua, self.0)
    }
}

impl<L, I> PushOneInto<L> for TableFromIter<I>
where
    L: AsLua,
    I: Iterator,
    <I as Iterator>::Item: PushInto<LuaState>,
{
}

////////////////////////////////////////////////////////////////////////////////
/// Vec
////////////////////////////////////////////////////////////////////////////////

impl<L, T> Push<L> for Vec<T>
where
    L: AsLua,
    T: Push<LuaState>,
{
    type Err = PushIterError<T::Err>;

    #[inline]
    fn push_to_lua(&self, lua: L) -> Result<PushGuard<L>, (Self::Err, L)> {
        push_iter(lua, self.iter())
    }
}

impl<L, T> PushOne<L> for Vec<T>
where
    L: AsLua,
    T: Push<LuaState>,
{
}

impl<L, T> PushInto<L> for Vec<T>
where
    L: AsLua,
    T: PushInto<LuaState>,
{
    type Err = PushIterError<T::Err>;

    #[inline]
    fn push_into_lua(self, lua: L) -> Result<PushGuard<L>, (Self::Err, L)> {
        push_iter(lua, self.into_iter())
    }
}

impl<L, T> PushOneInto<L> for Vec<T>
where
    L: AsLua,
    T: PushInto<LuaState>,
{
}

impl<L, T> LuaRead<L> for Vec<T>
where
    L: AsLua,
    T: for<'a> LuaRead<PushGuard<&'a LuaTable<L>>>,
    T: 'static,
{
    fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L> {
        // We need this as iteration order isn't guaranteed to match order of
        // keys, even if they're numeric
        // https://www.lua.org/manual/5.2/manual.html#pdf-next
        let table = match LuaTable::lua_read_at_position(lua, index) {
            Ok(table) => table,
            Err(lua) => return Err(lua),
        };
        let mut dict: BTreeMap<i32, T> = BTreeMap::new();

        let mut max_key = i32::MIN;
        let mut min_key = i32::MAX;

        {
            let mut iter = table.iter::<i32, T>();
            while let Some(maybe_kv) = iter.next() {
                let (key, value) = crate::unwrap_ok_or! { maybe_kv,
                    Err(e) => {
                        drop(iter);
                        let lua = table.into_inner();
                        let e = e.when("converting Lua table to Vec<_>")
                            .expected_type::<Self>();
                        return Err((lua, e))
                    }
                };
                max_key = max_key.max(key);
                min_key = min_key.min(key);
                dict.insert(key, value);
            }
        }

        if dict.is_empty() {
            return Ok(vec![]);
        }

        if min_key != 1 {
            // Rust doesn't support sparse arrays or arrays with negative
            // indices
            let e = WrongType::info("converting Lua table to Vec<_>")
                .expected("indexes in range 1..N")
                .actual(format!("value with index {}", min_key));
            return Err((table.into_inner(), e));
        }

        let mut result = Vec::with_capacity(max_key as _);

        // We expect to start with first element of table and have this
        // be smaller that first key by one
        let mut previous_key = 0;

        // By this point, we actually iterate the map to move values to Vec
        // and check that table represented non-sparse 1-indexed array
        for (k, v) in dict {
            if previous_key + 1 != k {
                let e = WrongType::info("converting Lua table to Vec<_>")
                    .expected("indexes in range 1..N")
                    .actual(format!("Lua table with missing index {}", previous_key + 1));
                return Err((table.into_inner(), e));
            } else {
                // We just push, thus converting Lua 1-based indexing
                // to Rust 0-based indexing
                result.push(v);
                previous_key = k;
            }
        }

        Ok(result)
    }
}

////////////////////////////////////////////////////////////////////////////////
/// \[T]
////////////////////////////////////////////////////////////////////////////////

impl<L, T> Push<L> for [T]
where
    L: AsLua,
    T: Push<LuaState>,
{
    type Err = PushIterError<T::Err>;

    #[inline]
    fn push_to_lua(&self, lua: L) -> Result<PushGuard<L>, (Self::Err, L)> {
        push_iter(lua, self.iter())
    }
}

impl<L, T> PushOne<L> for [T]
where
    L: AsLua,
    T: Push<LuaState>,
{
}

////////////////////////////////////////////////////////////////////////////////
/// [T; N]
////////////////////////////////////////////////////////////////////////////////

impl<L, T, const N: usize> Push<L> for [T; N]
where
    L: AsLua,
    T: Push<LuaState>,
{
    type Err = PushIterError<T::Err>;

    #[inline]
    fn push_to_lua(&self, lua: L) -> Result<PushGuard<L>, (Self::Err, L)> {
        push_iter(lua, self.iter())
    }
}

impl<L, T, const N: usize> PushOne<L> for [T; N]
where
    L: AsLua,
    T: Push<LuaState>,
{
}

impl<L, T, const N: usize> PushInto<L> for [T; N]
where
    L: AsLua,
    T: PushInto<LuaState>,
{
    type Err = PushIterError<T::Err>;

    #[inline]
    fn push_into_lua(self, lua: L) -> Result<PushGuard<L>, (Self::Err, L)> {
        push_iter(lua, IntoIterator::into_iter(self))
    }
}

impl<L, T, const N: usize> PushOneInto<L> for [T; N]
where
    L: AsLua,
    T: PushInto<LuaState>,
{
}

impl<L, T, const N: usize> LuaRead<L> for [T; N]
where
    L: AsLua,
    T: for<'a> LuaRead<PushGuard<&'a LuaTable<L>>>,
    T: 'static,
{
    fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L> {
        let table = match LuaTable::lua_read_at_position(lua, index) {
            Ok(table) => table,
            Err(lua) => return Err(lua),
        };

        let mut res = std::mem::MaybeUninit::uninit();
        let ptr = &mut res as *mut _ as *mut [T; N] as *mut T;
        let mut was_assigned = [false; N];
        let mut err = None;

        for maybe_kv in table.iter::<i32, T>() {
            match maybe_kv {
                Ok((key, value)) if 1 <= key && key as usize <= N => {
                    let i = (key - 1) as usize;
                    unsafe { std::ptr::write(ptr.add(i), value) }
                    was_assigned[i] = true;
                }
                Err(e) => {
                    err = Some(Error::Subtype(e));
                    break;
                }
                Ok((index, _)) => {
                    err = Some(Error::WrongIndex(index));
                    break;
                }
            }
        }

        if err.is_none() {
            err = was_assigned
                .iter()
                .zip(1..)
                .find(|(&was_assigned, _)| !was_assigned)
                .map(|(_, i)| Error::MissingIndex(i));
        }

        let err = crate::unwrap_or! { err,
            return Ok(unsafe { res.assume_init() });
        };

        for i in IntoIterator::into_iter(was_assigned)
            .enumerate()
            .flat_map(|(i, was_assigned)| was_assigned.then(|| i))
        {
            unsafe { std::ptr::drop_in_place(ptr.add(i)) }
        }

        let when = "converting Lua table to array";
        let e = match err {
            Error::Subtype(err) => err.when(when).expected_type::<Self>(),
            Error::WrongIndex(index) => WrongType::info(when)
                .expected(format!("indexes in range 1..={}", N))
                .actual(format!("value with index {}", index)),
            Error::MissingIndex(index) => WrongType::info(when)
                .expected(format!("indexes in range 1..={}", N))
                .actual(format!("Lua table with missing index {}", index)),
        };
        return Err((table.into_inner(), e));

        enum Error {
            Subtype(WrongType),
            WrongIndex(i32),
            MissingIndex(i32),
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
/// HashMap
////////////////////////////////////////////////////////////////////////////////

impl<L, K, V> LuaRead<L> for HashMap<K, V>
where
    L: AsLua,
    K: 'static + Hash + Eq,
    K: for<'k> LuaRead<&'k LuaTable<L>>,
    V: 'static,
    V: for<'v> LuaRead<PushGuard<&'v LuaTable<L>>>,
{
    fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L> {
        let table = LuaTable::lua_read_at_position(lua, index)?;
        let res: Result<_, _> = table.iter().collect();
        res.map_err(|err| {
            let l = table.into_inner();
            let e = err
                .when("converting Lua table to HashMap<_, _>")
                .expected_type::<Self>();
            (l, e)
        })
    }
}

macro_rules! push_hashmap_impl {
    ($self:expr, $lua:expr) => {
        push_iter($lua, $self.into_iter()).map_err(|(e, lua)| match e {
            PushIterError::TooManyValues(_) => unreachable!("K and V implement PushOne"),
            PushIterError::ValuePushError(First(e)) => (First(e), lua),
            PushIterError::ValuePushError(Other(e)) => (Other(e.first()), lua),
        })
    };
}

impl<L, K, V> Push<L> for HashMap<K, V>
where
    L: AsLua,
    K: PushOne<LuaState> + Eq + Hash + Debug,
    V: PushOne<LuaState> + Debug,
{
    type Err = TuplePushError<K::Err, V::Err>;

    #[inline]
    fn push_to_lua(&self, lua: L) -> Result<PushGuard<L>, (Self::Err, L)> {
        push_hashmap_impl!(self, lua)
    }
}

impl<L, K, V> PushOne<L> for HashMap<K, V>
where
    L: AsLua,
    K: PushOne<LuaState> + Eq + Hash + Debug,
    V: PushOne<LuaState> + Debug,
{
}

impl<L, K, V> PushInto<L> for HashMap<K, V>
where
    L: AsLua,
    K: PushOneInto<LuaState> + Eq + Hash + Debug,
    V: PushOneInto<LuaState> + Debug,
{
    type Err = TuplePushError<K::Err, V::Err>;

    #[inline]
    fn push_into_lua(self, lua: L) -> Result<PushGuard<L>, (Self::Err, L)> {
        push_hashmap_impl!(self, lua)
    }
}

impl<L, K, V> PushOneInto<L> for HashMap<K, V>
where
    L: AsLua,
    K: PushOneInto<LuaState> + Eq + Hash + Debug,
    V: PushOneInto<LuaState> + Debug,
{
}

////////////////////////////////////////////////////////////////////////////////
/// HashSet
////////////////////////////////////////////////////////////////////////////////

macro_rules! push_hashset_impl {
    ($self:expr, $lua:expr) => {
        push_iter($lua, $self.into_iter().zip(iter::repeat(true))).map_err(|(e, lua)| match e {
            PushIterError::TooManyValues(_) => unreachable!("K implements PushOne"),
            PushIterError::ValuePushError(First(e)) => (e, lua),
            PushIterError::ValuePushError(Other(_)) => {
                unreachable!("no way to create instance of Void")
            }
        })
    };
}

impl<L, K> Push<L> for HashSet<K>
where
    L: AsLua,
    K: PushOne<LuaState> + Eq + Hash + Debug,
{
    type Err = K::Err;

    #[inline]
    fn push_to_lua(&self, lua: L) -> Result<PushGuard<L>, (K::Err, L)> {
        push_hashset_impl!(self, lua)
    }
}

impl<L, K> PushOne<L> for HashSet<K>
where
    L: AsLua,
    K: PushOne<LuaState> + Eq + Hash + Debug,
{
}

impl<L, K> PushInto<L> for HashSet<K>
where
    L: AsLua,
    K: PushOneInto<LuaState> + Eq + Hash + Debug,
{
    type Err = K::Err;

    #[inline]
    fn push_into_lua(self, lua: L) -> Result<PushGuard<L>, (K::Err, L)> {
        push_hashset_impl!(self, lua)
    }
}

impl<L, K> PushOneInto<L> for HashSet<K>
where
    L: AsLua,
    K: PushOneInto<LuaState> + Eq + Hash + Debug,
{
}