luaur-rt 0.1.3

Safe, ergonomic, mlua-style API for luaur (pure-Rust Luau).
Documentation
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
//! Deserialize a Lua [`Value`] into a Rust data structure.
//!
//! Mirrors `mlua::serde::de`, written directly over luaur-rt's `Value`/`Table`.

use std::cell::RefCell;
use std::collections::HashSet;
use std::ffi::c_void;
use std::rc::Rc;
use std::result::Result as StdResult;

use serde::de::{self, IntoDeserializer};

use crate::error::{Error, Result};
use crate::table::{Table, TablePairs};
use crate::value::Value;

/// A struct for deserializing Lua values into Rust values.
pub struct Deserializer {
    value: Value,
    options: Options,
    visited: Rc<RefCell<HashSet<*const c_void>>>,
    len: Option<usize>, // length hint for sequences
}

/// Options controlling [`Deserializer`] behavior. Mirrors
/// `mlua::serde::de::Options`.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct Options {
    /// If true, an attempt to deserialize unsupported types ([`Function`],
    /// [`Thread`], [`Error`], ...) is an error; otherwise they are skipped when
    /// iterating or deserialized as the unit type. Default: true.
    ///
    /// [`Function`]: crate::Function
    /// [`Thread`]: crate::Thread
    /// [`Error`]: crate::Error
    pub deny_unsupported_types: bool,

    /// If true, deserializing a recursive table (one that refers to itself) is
    /// an error; otherwise subsequent visits to the same table are ignored.
    /// Default: true.
    pub deny_recursive_tables: bool,

    /// If true, keys in tables are iterated in sorted order. Default: false.
    pub sort_keys: bool,

    /// If true, empty Lua tables are encoded as an array instead of a map.
    /// Default: false.
    pub encode_empty_tables_as_array: bool,

    /// If true, enable detection of mixed tables (both array-like and map-like
    /// entries, or several borders). Default: false.
    pub detect_mixed_tables: bool,
}

impl Default for Options {
    fn default() -> Self {
        const { Self::new() }
    }
}

impl Options {
    /// A new [`Options`] with default parameters.
    pub const fn new() -> Self {
        Options {
            deny_unsupported_types: true,
            deny_recursive_tables: true,
            sort_keys: false,
            encode_empty_tables_as_array: false,
            detect_mixed_tables: false,
        }
    }

    /// Sets `deny_unsupported_types`.
    #[must_use]
    pub const fn deny_unsupported_types(mut self, enabled: bool) -> Self {
        self.deny_unsupported_types = enabled;
        self
    }

    /// Sets `deny_recursive_tables`.
    #[must_use]
    pub const fn deny_recursive_tables(mut self, enabled: bool) -> Self {
        self.deny_recursive_tables = enabled;
        self
    }

    /// Sets `sort_keys`.
    #[must_use]
    pub const fn sort_keys(mut self, enabled: bool) -> Self {
        self.sort_keys = enabled;
        self
    }

    /// Sets `encode_empty_tables_as_array`.
    #[must_use]
    pub const fn encode_empty_tables_as_array(mut self, enabled: bool) -> Self {
        self.encode_empty_tables_as_array = enabled;
        self
    }

    /// Sets `detect_mixed_tables`.
    #[must_use]
    pub const fn detect_mixed_tables(mut self, enable: bool) -> Self {
        self.detect_mixed_tables = enable;
        self
    }
}

impl Deserializer {
    /// Creates a new Lua deserializer for `value`.
    pub fn new(value: Value) -> Self {
        Self::new_with_options(value, Options::default())
    }

    /// Creates a new Lua deserializer for `value` with custom options.
    pub fn new_with_options(value: Value, options: Options) -> Self {
        Deserializer {
            value,
            options,
            visited: Rc::new(RefCell::new(HashSet::new())),
            len: None,
        }
    }

    fn from_parts(
        value: Value,
        options: Options,
        visited: Rc<RefCell<HashSet<*const c_void>>>,
    ) -> Self {
        Deserializer {
            value,
            options,
            visited,
            len: None,
        }
    }

    fn with_len(mut self, len: usize) -> Self {
        self.len = Some(len);
        self
    }
}

impl<'de> serde::Deserializer<'de> for Deserializer {
    type Error = Error;

    #[inline]
    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        match self.value {
            Value::Nil => visitor.visit_unit(),
            Value::Boolean(b) => visitor.visit_bool(b),
            Value::Integer(i) => visitor.visit_i64(i),
            Value::Number(n) => visitor.visit_f64(n),
            Value::Vector(_) => self.deserialize_seq(visitor),
            Value::String(ref s) => match s.to_str() {
                Ok(s) => visitor.visit_str(&s),
                Err(_) => visitor.visit_bytes(&s.as_bytes()),
            },
            Value::Table(ref t) if super::is_null(&self.value) => {
                let _ = t;
                visitor.visit_none()
            }
            Value::Table(ref t) => {
                if let Some(len) = encode_as_array(t, self.options) {
                    self.with_len(len).deserialize_seq(visitor)
                } else {
                    self.deserialize_map(visitor)
                }
            }
            Value::Buffer(ref buf) => visitor.visit_bytes(buf.as_slice()),
            Value::Function(_)
            | Value::Thread(_)
            | Value::LightUserData(_)
            | Value::UserData(_)
            | Value::Error(_) => {
                if self.options.deny_unsupported_types {
                    let msg = format!("unsupported value type `{}`", self.value.type_name());
                    Err(de::Error::custom(msg))
                } else {
                    visitor.visit_unit()
                }
            }
        }
    }

    #[inline]
    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        match self.value {
            Value::Nil => visitor.visit_none(),
            _ if super::is_null(&self.value) => visitor.visit_none(),
            _ => visitor.visit_some(self),
        }
    }

    #[inline]
    fn deserialize_enum<V>(
        self,
        _name: &'static str,
        _variants: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        let (variant, value, _guard) = match self.value {
            Value::Table(table) => {
                let _guard = RecursionGuard::new(&table, &self.visited);

                let mut iter = table.pairs::<String, Value>();
                let (variant, value) = match iter.next() {
                    Some(v) => v?,
                    None => {
                        return Err(de::Error::invalid_value(
                            de::Unexpected::Map,
                            &"map with a single key",
                        ));
                    }
                };

                if iter.next().is_some() {
                    return Err(de::Error::invalid_value(
                        de::Unexpected::Map,
                        &"map with a single key",
                    ));
                }
                let skip = check_value_for_skip(&value, self.options, &self.visited)
                    .map_err(|err| Error::DeserializeError(err.to_string()))?;
                if skip {
                    return Err(de::Error::custom("bad enum value"));
                }

                (variant, Some(value), Some(_guard))
            }
            Value::String(variant) => (variant.to_str()?, None, None),
            _ => return Err(de::Error::custom("bad enum value")),
        };

        visitor.visit_enum(EnumDeserializer {
            variant,
            value,
            options: self.options,
            visited: self.visited,
        })
    }

    #[inline]
    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        match self.value {
            Value::Vector(vec) => {
                let mut deserializer = VecDeserializer {
                    vec,
                    next: 0,
                    options: self.options,
                    visited: self.visited,
                };
                visitor.visit_seq(&mut deserializer)
            }
            Value::Table(t) => {
                let _guard = RecursionGuard::new(&t, &self.visited);

                let len = self.len.unwrap_or_else(|| t.raw_len());
                let mut deserializer = SeqDeserializer {
                    table: t,
                    index: 1,
                    len,
                    options: self.options,
                    visited: self.visited,
                };
                let seq = visitor.visit_seq(&mut deserializer)?;
                if deserializer.index > deserializer.len {
                    Ok(seq)
                } else {
                    Err(de::Error::invalid_length(
                        len,
                        &"fewer elements in the table",
                    ))
                }
            }
            value => Err(de::Error::invalid_type(
                de::Unexpected::Other(value.type_name()),
                &"table",
            )),
        }
    }

    #[inline]
    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        self.deserialize_seq(visitor)
    }

    #[inline]
    fn deserialize_tuple_struct<V>(
        self,
        _name: &'static str,
        _len: usize,
        visitor: V,
    ) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        self.deserialize_seq(visitor)
    }

    #[inline]
    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        match self.value {
            Value::Table(t) => {
                let _guard = RecursionGuard::new(&t, &self.visited);

                let mut deserializer = MapDeserializer {
                    pairs: MapPairs::new(&t, self.options.sort_keys)?,
                    value: None,
                    options: self.options,
                    visited: self.visited,
                    processed: 0,
                };
                let map = visitor.visit_map(&mut deserializer)?;
                let count = deserializer.pairs.count();
                if count == 0 {
                    Ok(map)
                } else {
                    Err(de::Error::invalid_length(
                        deserializer.processed + count,
                        &"fewer elements in the table",
                    ))
                }
            }
            value => Err(de::Error::invalid_type(
                de::Unexpected::Other(value.type_name()),
                &"table",
            )),
        }
    }

    #[inline]
    fn deserialize_struct<V>(
        self,
        _name: &'static str,
        _fields: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        self.deserialize_map(visitor)
    }

    #[inline]
    fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_newtype_struct(self)
    }

    #[inline]
    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        if super::is_null(&self.value) {
            visitor.visit_unit()
        } else {
            self.deserialize_any(visitor)
        }
    }

    #[inline]
    fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        if super::is_null(&self.value) {
            visitor.visit_unit()
        } else {
            self.deserialize_any(visitor)
        }
    }

    serde::forward_to_deserialize_any! {
        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string bytes
        byte_buf identifier ignored_any
    }
}

// Reads the sequence part `1..=len` via raw access, yielding *every* slot up to
// `len` (including `nil` holes). This mirrors mlua's `TableSequence::with_len`,
// which iterates `raw_geti(i)` over a fixed length rather than stopping at the
// first border — so a sparse array like `{1,2,3,nil,5}` (raw length 5)
// round-trips identically through `from_value` and the `Serialize` impl.
struct SeqDeserializer {
    table: Table,
    index: usize,
    len: usize,
    options: Options,
    visited: Rc<RefCell<HashSet<*const c_void>>>,
}

impl<'de> de::SeqAccess<'de> for SeqDeserializer {
    type Error = Error;

    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
    where
        T: de::DeserializeSeed<'de>,
    {
        loop {
            if self.index > self.len {
                return Ok(None);
            }
            let value: Value = self.table.raw_get(self.index as i64)?;
            self.index += 1;
            let skip = check_value_for_skip(&value, self.options, &self.visited)
                .map_err(|err| Error::DeserializeError(err.to_string()))?;
            if skip {
                continue;
            }
            let visited = Rc::clone(&self.visited);
            let deserializer = Deserializer::from_parts(value, self.options, visited);
            return seed.deserialize(deserializer).map(Some);
        }
    }

    fn size_hint(&self) -> Option<usize> {
        Some(self.len + 1 - self.index)
    }
}

struct VecDeserializer {
    vec: crate::Vector,
    next: usize,
    options: Options,
    visited: Rc<RefCell<HashSet<*const c_void>>>,
}

impl<'de> de::SeqAccess<'de> for VecDeserializer {
    type Error = Error;

    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
    where
        T: de::DeserializeSeed<'de>,
    {
        if self.next >= crate::Vector::SIZE {
            return Ok(None);
        }
        let n = self.vec.0[self.next];
        self.next += 1;
        let visited = Rc::clone(&self.visited);
        let deserializer = Deserializer::from_parts(Value::Number(n as f64), self.options, visited);
        seed.deserialize(deserializer).map(Some)
    }

    fn size_hint(&self) -> Option<usize> {
        Some(crate::Vector::SIZE)
    }
}

pub(crate) enum MapPairs {
    Iter(TablePairs<Value, Value>),
    Vec(Vec<(Value, Value)>),
}

impl MapPairs {
    pub(crate) fn new(t: &Table, sort_keys: bool) -> Result<Self> {
        if sort_keys {
            let mut pairs = t.pairs::<Value, Value>().collect::<Result<Vec<_>>>()?;
            // Reverse order as we pop values from the end.
            pairs.sort_by(|(a, _), (b, _)| sort_cmp(b, a));
            Ok(MapPairs::Vec(pairs))
        } else {
            Ok(MapPairs::Iter(t.pairs::<Value, Value>()))
        }
    }

    pub(crate) fn count(self) -> usize {
        match self {
            MapPairs::Iter(iter) => iter.count(),
            MapPairs::Vec(vec) => vec.len(),
        }
    }
}

impl Iterator for MapPairs {
    type Item = Result<(Value, Value)>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            MapPairs::Iter(iter) => iter.next(),
            MapPairs::Vec(vec) => vec.pop().map(Ok),
        }
    }
}

struct MapDeserializer {
    pairs: MapPairs,
    value: Option<Value>,
    options: Options,
    visited: Rc<RefCell<HashSet<*const c_void>>>,
    processed: usize,
}

impl MapDeserializer {
    fn next_key_deserializer(&mut self) -> Result<Option<Deserializer>> {
        loop {
            match self.pairs.next() {
                Some(item) => {
                    let (key, value) = item?;
                    let skip_key = check_value_for_skip(&key, self.options, &self.visited)
                        .map_err(|err| Error::DeserializeError(err.to_string()))?;
                    let skip_value = check_value_for_skip(&value, self.options, &self.visited)
                        .map_err(|err| Error::DeserializeError(err.to_string()))?;
                    if skip_key || skip_value {
                        continue;
                    }
                    self.processed += 1;
                    self.value = Some(value);
                    let visited = Rc::clone(&self.visited);
                    let key_de = Deserializer::from_parts(key, self.options, visited);
                    return Ok(Some(key_de));
                }
                None => return Ok(None),
            }
        }
    }

    fn next_value_deserializer(&mut self) -> Result<Deserializer> {
        match self.value.take() {
            Some(value) => {
                let visited = Rc::clone(&self.visited);
                Ok(Deserializer::from_parts(value, self.options, visited))
            }
            None => Err(de::Error::custom("value is missing")),
        }
    }
}

impl<'de> de::MapAccess<'de> for MapDeserializer {
    type Error = Error;

    fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
    where
        T: de::DeserializeSeed<'de>,
    {
        match self.next_key_deserializer() {
            Ok(Some(key_de)) => seed.deserialize(key_de).map(Some),
            Ok(None) => Ok(None),
            Err(error) => Err(error),
        }
    }

    fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value>
    where
        T: de::DeserializeSeed<'de>,
    {
        match self.next_value_deserializer() {
            Ok(value_de) => seed.deserialize(value_de),
            Err(error) => Err(error),
        }
    }
}

struct EnumDeserializer {
    variant: String,
    value: Option<Value>,
    options: Options,
    visited: Rc<RefCell<HashSet<*const c_void>>>,
}

impl<'de> de::EnumAccess<'de> for EnumDeserializer {
    type Error = Error;
    type Variant = VariantDeserializer;

    fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant)>
    where
        T: de::DeserializeSeed<'de>,
    {
        let variant = self.variant.into_deserializer();
        let variant_access = VariantDeserializer {
            value: self.value,
            options: self.options,
            visited: self.visited,
        };
        seed.deserialize(variant).map(|v| (v, variant_access))
    }
}

struct VariantDeserializer {
    value: Option<Value>,
    options: Options,
    visited: Rc<RefCell<HashSet<*const c_void>>>,
}

impl<'de> de::VariantAccess<'de> for VariantDeserializer {
    type Error = Error;

    fn unit_variant(self) -> Result<()> {
        match self.value {
            Some(_) => Err(de::Error::invalid_type(
                de::Unexpected::NewtypeVariant,
                &"unit variant",
            )),
            None => Ok(()),
        }
    }

    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
    where
        T: de::DeserializeSeed<'de>,
    {
        match self.value {
            Some(value) => {
                seed.deserialize(Deserializer::from_parts(value, self.options, self.visited))
            }
            None => Err(de::Error::invalid_type(
                de::Unexpected::UnitVariant,
                &"newtype variant",
            )),
        }
    }

    fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        match self.value {
            Some(value) => serde::Deserializer::deserialize_seq(
                Deserializer::from_parts(value, self.options, self.visited),
                visitor,
            ),
            None => Err(de::Error::invalid_type(
                de::Unexpected::UnitVariant,
                &"tuple variant",
            )),
        }
    }

    fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        match self.value {
            Some(value) => serde::Deserializer::deserialize_map(
                Deserializer::from_parts(value, self.options, self.visited),
                visitor,
            ),
            None => Err(de::Error::invalid_type(
                de::Unexpected::UnitVariant,
                &"struct variant",
            )),
        }
    }
}

// Adds a table pointer to the `visited` set and removes it on drop. Used to
// track recursive tables while still allowing the same table to be traversed
// multiple times at different points.
pub(crate) struct RecursionGuard {
    ptr: *const c_void,
    visited: Rc<RefCell<HashSet<*const c_void>>>,
}

impl RecursionGuard {
    #[inline]
    pub(crate) fn new(table: &Table, visited: &Rc<RefCell<HashSet<*const c_void>>>) -> Self {
        let visited = Rc::clone(visited);
        let ptr = table.to_pointer();
        visited.borrow_mut().insert(ptr);
        RecursionGuard { ptr, visited }
    }
}

impl Drop for RecursionGuard {
    fn drop(&mut self) {
        self.visited.borrow_mut().remove(&self.ptr);
    }
}

// Checks `options` and decides whether to emit an error or skip the value.
pub(crate) fn check_value_for_skip(
    value: &Value,
    options: Options,
    visited: &RefCell<HashSet<*const c_void>>,
) -> StdResult<bool, &'static str> {
    // The `null` sentinel is a table; it must never be treated as a recursive
    // table, and is always serialized (as `null`/`none`).
    if super::is_null(value) {
        return Ok(false);
    }
    match value {
        Value::Table(table) => {
            let ptr = table.to_pointer();
            if visited.borrow().contains(&ptr) {
                if options.deny_recursive_tables {
                    return Err("recursive table detected");
                }
                return Ok(true); // skip
            }
        }
        Value::Function(_) | Value::Thread(_) | Value::UserData(_) | Value::Error(_)
            if !options.deny_unsupported_types =>
        {
            return Ok(true); // skip
        }
        _ => {}
    }
    Ok(false) // do not skip
}

// ---------------------------------------------------------------------------
// Table -> array detection and value ordering (serde-only helpers).
//
// Implemented as free functions over `Table`/`Value` so the base `table.rs` /
// `value.rs` stay unchanged. Mirrors mlua's `Table::encode_as_array` and
// `Value::sort_cmp`.
// ---------------------------------------------------------------------------

/// If `table` is an array, returns `(non-nil count, max integer index)`;
/// `None` if any key is not a positive integer.
fn find_array_len(table: &Table) -> Option<(usize, usize)> {
    let (mut count, mut max_index) = (0usize, 0usize);
    for pair in table.pairs::<Value, Value>() {
        let (k, _) = pair.ok()?;
        let n = match k {
            Value::Integer(i) if i >= 1 => i as usize,
            Value::Number(f) if f.fract() == 0.0 && f >= 1.0 => f as usize,
            _ => return None,
        };
        max_index = max_index.max(n);
        count += 1;
    }
    Some((count, max_index))
}

/// Determines whether `table` should be encoded as an array; returns the array
/// length if so. Mirrors `mlua::Table::encode_as_array`.
pub(crate) fn encode_as_array(table: &Table, options: Options) -> Option<usize> {
    if options.detect_mixed_tables {
        if let Some((len, max_idx)) = find_array_len(table) {
            // Too-sparse arrays are encoded as maps instead.
            if len < 10 || len * 2 >= max_idx {
                return Some(max_idx);
            }
        }
    } else {
        let len = table.raw_len();
        if len > 0 || super::has_array_metatable(table) {
            return Some(len);
        }
        if options.encode_empty_tables_as_array && table.is_empty() {
            return Some(0);
        }
    }
    None
}

/// Total order over [`Value`]s used for `sort_keys`. Mirrors
/// `mlua::Value::sort_cmp`, with the `null` sentinel ordered like mlua's
/// `LightUserData(NULL)` (just after `Nil`).
fn sort_cmp(a: &Value, b: &Value) -> std::cmp::Ordering {
    use std::cmp::Ordering;

    let a_null = super::is_null(a);
    let b_null = super::is_null(b);
    match (a, b) {
        (Value::Nil, Value::Nil) => Ordering::Equal,
        (Value::Nil, _) => Ordering::Less,
        (_, Value::Nil) => Ordering::Greater,
        // `null` sentinel (special case)
        _ if a_null && b_null => Ordering::Equal,
        _ if a_null => Ordering::Less,
        _ if b_null => Ordering::Greater,
        (Value::Boolean(x), Value::Boolean(y)) => x.cmp(y),
        (Value::Boolean(_), _) => Ordering::Less,
        (_, Value::Boolean(_)) => Ordering::Greater,
        (Value::Integer(x), Value::Integer(y)) => x.cmp(y),
        (Value::Integer(x), Value::Number(y)) => {
            (*x as f64).partial_cmp(y).unwrap_or(Ordering::Equal)
        }
        (Value::Number(x), Value::Integer(y)) => {
            x.partial_cmp(&(*y as f64)).unwrap_or(Ordering::Equal)
        }
        (Value::Number(x), Value::Number(y)) => x.partial_cmp(y).unwrap_or(Ordering::Equal),
        (Value::Integer(_) | Value::Number(_), _) => Ordering::Less,
        (_, Value::Integer(_) | Value::Number(_)) => Ordering::Greater,
        (Value::Vector(x), Value::Vector(y)) => x.partial_cmp(y).unwrap_or(Ordering::Equal),
        (Value::String(x), Value::String(y)) => x.as_bytes().cmp(&y.as_bytes()),
        (Value::String(_), _) => Ordering::Less,
        (_, Value::String(_)) => Ordering::Greater,
        // Other variants are ordered by pointer identity.
        (x, y) => x.to_pointer().cmp(&y.to_pointer()),
    }
}