Skip to main content

mlua/serde/
de.rs

1//! Deserialize Lua values to a Rust data structure.
2
3use std::cell::RefCell;
4use std::os::raw::c_void;
5use std::rc::Rc;
6use std::result::Result as StdResult;
7
8use rustc_hash::FxHashSet;
9use serde::de::{self, IntoDeserializer};
10
11use crate::error::{Error, Result};
12use crate::table::{Table, TablePairs, TableSequence};
13use crate::userdata::AnyUserData;
14use crate::value::Value;
15
16/// A struct for deserializing Lua values into Rust values.
17#[derive(Debug, Default)]
18pub struct Deserializer {
19    value: Value,
20    options: Options,
21    visited: Rc<RefCell<FxHashSet<*const c_void>>>,
22    len: Option<usize>, // A length hint for sequences
23}
24
25/// A struct with options to change default deserializer behavior.
26#[derive(Debug, Clone, Copy)]
27#[non_exhaustive]
28pub struct Options {
29    /// Maximum nesting depth for tables.
30    ///
31    /// Increasing this limit may require a larger thread stack. Zero rejects all tables.
32    ///
33    /// Default: **128**
34    pub recursion_limit: usize,
35
36    /// If true, an attempt to serialize types such as [`Function`], [`Thread`], [`LightUserData`]
37    /// and [`Error`] will cause an error.
38    /// Otherwise these types skipped when iterating or serialized as unit type.
39    ///
40    /// Default: **true**
41    ///
42    /// [`Function`]: crate::Function
43    /// [`Thread`]: crate::Thread
44    /// [`LightUserData`]: crate::LightUserData
45    /// [`Error`]: crate::Error
46    pub deny_unsupported_types: bool,
47
48    /// If true, an attempt to serialize a recursive table (table that refers to itself)
49    /// will cause an error.
50    /// Otherwise subsequent attempts to serialize the same table will be ignored.
51    ///
52    /// Default: **true**
53    pub deny_recursive_tables: bool,
54
55    /// If true, keys in tables will be iterated in sorted order.
56    ///
57    /// Default: **false**
58    pub sort_keys: bool,
59
60    /// If true, empty Lua tables will be encoded as array, instead of map.
61    ///
62    /// Default: **false**
63    pub encode_empty_tables_as_array: bool,
64
65    /// If true, enable detection of mixed tables.
66    ///
67    /// A mixed table is a table that has both array-like and map-like entries or several borders.
68    /// See [`The Length Operator`] documentation for details about borders.
69    ///
70    /// When this option is disabled, a table with a non-zero length (with one or more borders) will
71    /// be always encoded as an array.
72    ///
73    /// Default: **false**
74    ///
75    /// [`The Length Operator`]: https://www.lua.org/manual/5.4/manual.html#3.4.7
76    pub detect_mixed_tables: bool,
77
78    #[cfg(feature = "luau")]
79    pub(crate) borrow_buffers: bool,
80}
81
82impl Default for Options {
83    fn default() -> Self {
84        const { Self::new() }
85    }
86}
87
88impl Options {
89    /// Returns a new instance of `Options` with default parameters.
90    pub const fn new() -> Self {
91        Options {
92            recursion_limit: super::DEFAULT_RECURSION_LIMIT,
93            deny_unsupported_types: true,
94            deny_recursive_tables: true,
95            sort_keys: false,
96            encode_empty_tables_as_array: false,
97            detect_mixed_tables: false,
98            #[cfg(feature = "luau")]
99            borrow_buffers: false,
100        }
101    }
102
103    /// Sets [`recursion_limit`](Self::recursion_limit).
104    #[must_use]
105    pub const fn recursion_limit(mut self, limit: usize) -> Self {
106        self.recursion_limit = limit;
107        self
108    }
109
110    /// Sets [`deny_unsupported_types`] option.
111    ///
112    /// [`deny_unsupported_types`]: #structfield.deny_unsupported_types
113    #[must_use]
114    pub const fn deny_unsupported_types(mut self, enabled: bool) -> Self {
115        self.deny_unsupported_types = enabled;
116        self
117    }
118
119    /// Sets [`deny_recursive_tables`] option.
120    ///
121    /// [`deny_recursive_tables`]: #structfield.deny_recursive_tables
122    #[must_use]
123    pub const fn deny_recursive_tables(mut self, enabled: bool) -> Self {
124        self.deny_recursive_tables = enabled;
125        self
126    }
127
128    /// Sets [`sort_keys`] option.
129    ///
130    /// [`sort_keys`]: #structfield.sort_keys
131    #[must_use]
132    pub const fn sort_keys(mut self, enabled: bool) -> Self {
133        self.sort_keys = enabled;
134        self
135    }
136
137    /// Sets [`encode_empty_tables_as_array`] option.
138    ///
139    /// [`encode_empty_tables_as_array`]: #structfield.encode_empty_tables_as_array
140    #[must_use]
141    pub const fn encode_empty_tables_as_array(mut self, enabled: bool) -> Self {
142        self.encode_empty_tables_as_array = enabled;
143        self
144    }
145
146    /// Sets [`detect_mixed_tables`] option.
147    ///
148    /// [`detect_mixed_tables`]: #structfield.detect_mixed_tables
149    #[must_use]
150    pub const fn detect_mixed_tables(mut self, enable: bool) -> Self {
151        self.detect_mixed_tables = enable;
152        self
153    }
154
155    /// If true, pass Luau buffer bytes directly to visitors without copying.
156    ///
157    /// Default: **false**.
158    ///
159    /// # Safety
160    ///
161    /// When enabled, the caller must ensure that visitors and any code they invoke do not
162    /// mutate a buffer while its bytes are borrowed.
163    #[cfg(feature = "luau")]
164    #[must_use]
165    pub const unsafe fn borrow_buffers(mut self, enabled: bool) -> Self {
166        self.borrow_buffers = enabled;
167        self
168    }
169}
170
171impl Deserializer {
172    /// Creates a new Lua Deserializer for the [`Value`].
173    pub fn new(value: Value) -> Self {
174        Self::new_with_options(value, Options::default())
175    }
176
177    /// Creates a new Lua Deserializer for the [`Value`] with custom options.
178    pub fn new_with_options(value: Value, options: Options) -> Self {
179        Deserializer {
180            value,
181            options,
182            ..Default::default()
183        }
184    }
185
186    fn from_parts(value: Value, options: Options, visited: Rc<RefCell<FxHashSet<*const c_void>>>) -> Self {
187        Deserializer {
188            value,
189            options,
190            visited,
191            len: None,
192        }
193    }
194
195    fn with_len(mut self, len: usize) -> Self {
196        self.len = Some(len);
197        self
198    }
199}
200
201impl<'de> serde::Deserializer<'de> for Deserializer {
202    type Error = Error;
203
204    #[inline]
205    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
206    where
207        V: de::Visitor<'de>,
208    {
209        match self.value {
210            Value::Nil => visitor.visit_unit(),
211            Value::Boolean(b) => visitor.visit_bool(b),
212            #[allow(clippy::useless_conversion)]
213            Value::Integer(i) => visitor.visit_i64(i.into()),
214            #[allow(clippy::useless_conversion)]
215            Value::Number(n) => visitor.visit_f64(n.into()),
216            #[cfg(feature = "luau")]
217            Value::Vector(_) => self.deserialize_seq(visitor),
218            Value::String(s) => match s.to_str() {
219                Ok(s) => visitor.visit_str(&s),
220                Err(_) => visitor.visit_bytes(&s.as_bytes()),
221            },
222            Value::Table(ref t) => {
223                if let Some(len) = t.encode_as_array(self.options) {
224                    self.with_len(len).deserialize_seq(visitor)
225                } else {
226                    self.deserialize_map(visitor)
227                }
228            }
229            Value::LightUserData(ud) if ud.0.is_null() => visitor.visit_none(),
230            Value::UserData(ud) if ud.is_serializable() => {
231                serde_userdata(ud, |value| value.deserialize_any(visitor))
232            }
233            #[cfg(feature = "luau")]
234            Value::Buffer(buf) if self.options.borrow_buffers => {
235                let lua = buf.0.lua.lock();
236                visitor.visit_bytes(buf.as_slice(&lua))
237            }
238            #[cfg(feature = "luau")]
239            Value::Buffer(buf) => visitor.visit_bytes(&buf.to_vec()),
240            Value::Function(_)
241            | Value::Thread(_)
242            | Value::UserData(_)
243            | Value::LightUserData(_)
244            | Value::Error(_)
245            | Value::Other(_) => {
246                if self.options.deny_unsupported_types {
247                    let msg = format!("unsupported value type `{}`", self.value.type_name());
248                    Err(de::Error::custom(msg))
249                } else {
250                    visitor.visit_unit()
251                }
252            }
253        }
254    }
255
256    #[inline]
257    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
258    where
259        V: de::Visitor<'de>,
260    {
261        match self.value {
262            Value::Nil => visitor.visit_none(),
263            Value::LightUserData(ud) if ud.0.is_null() => visitor.visit_none(),
264            _ => visitor.visit_some(self),
265        }
266    }
267
268    #[inline]
269    fn deserialize_enum<V>(
270        self,
271        name: &'static str,
272        variants: &'static [&'static str],
273        visitor: V,
274    ) -> Result<V::Value>
275    where
276        V: de::Visitor<'de>,
277    {
278        let (variant, value, _guard) = match self.value {
279            Value::Table(table) => {
280                let _guard = RecursionGuard::new(&table, &self.visited, self.options.recursion_limit)
281                    .map_err(|err| Error::DeserializeError(err.to_string()))?;
282
283                let mut iter = table.pairs::<String, Value>();
284                let (variant, value) = match iter.next() {
285                    Some(v) => v?,
286                    None => {
287                        return Err(de::Error::invalid_value(
288                            de::Unexpected::Map,
289                            &"map with a single key",
290                        ));
291                    }
292                };
293
294                if iter.next().is_some() {
295                    return Err(de::Error::invalid_value(
296                        de::Unexpected::Map,
297                        &"map with a single key",
298                    ));
299                }
300                let skip = check_value_for_skip(&value, self.options, &self.visited)
301                    .map_err(|err| Error::DeserializeError(err.to_string()))?;
302                if skip {
303                    return Err(de::Error::custom("bad enum value"));
304                }
305
306                (variant, Some(value), Some(_guard))
307            }
308            Value::String(variant) => (variant.to_str()?.to_owned(), None, None),
309            Value::UserData(ud) if ud.is_serializable() => {
310                return serde_userdata(ud, |value| value.deserialize_enum(name, variants, visitor));
311            }
312            _ => return Err(de::Error::custom("bad enum value")),
313        };
314
315        visitor.visit_enum(EnumDeserializer {
316            variant,
317            value,
318            options: self.options,
319            visited: self.visited,
320        })
321    }
322
323    #[inline]
324    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
325    where
326        V: de::Visitor<'de>,
327    {
328        match self.value {
329            #[cfg(feature = "luau")]
330            Value::Vector(vec) => {
331                let mut deserializer = VecDeserializer {
332                    vec,
333                    next: 0,
334                    options: self.options,
335                    visited: self.visited,
336                };
337                let seq = visitor.visit_seq(&mut deserializer)?;
338                if deserializer.next == crate::Vector::SIZE {
339                    Ok(seq)
340                } else {
341                    Err(de::Error::invalid_length(
342                        crate::Vector::SIZE,
343                        &"fewer elements in the vector",
344                    ))
345                }
346            }
347            Value::Table(t) => {
348                let _guard = RecursionGuard::new(&t, &self.visited, self.options.recursion_limit)
349                    .map_err(|err| Error::DeserializeError(err.to_string()))?;
350
351                let len = self.len.unwrap_or_else(|| t.raw_len());
352                let mut deserializer = SeqDeserializer {
353                    seq: t.sequence_values().with_len(len),
354                    options: self.options,
355                    visited: self.visited,
356                };
357                let seq = visitor.visit_seq(&mut deserializer)?;
358                if deserializer.seq.next().is_none() {
359                    Ok(seq)
360                } else {
361                    Err(de::Error::invalid_length(len, &"fewer elements in the table"))
362                }
363            }
364            Value::UserData(ud) if ud.is_serializable() => {
365                serde_userdata(ud, |value| value.deserialize_seq(visitor))
366            }
367            value => Err(de::Error::invalid_type(
368                de::Unexpected::Other(value.type_name()),
369                &"table",
370            )),
371        }
372    }
373
374    #[inline]
375    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
376    where
377        V: de::Visitor<'de>,
378    {
379        self.deserialize_seq(visitor)
380    }
381
382    #[inline]
383    fn deserialize_tuple_struct<V>(self, _name: &'static str, _len: usize, visitor: V) -> Result<V::Value>
384    where
385        V: de::Visitor<'de>,
386    {
387        self.deserialize_seq(visitor)
388    }
389
390    #[inline]
391    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
392    where
393        V: de::Visitor<'de>,
394    {
395        match self.value {
396            Value::Table(t) => {
397                let _guard = RecursionGuard::new(&t, &self.visited, self.options.recursion_limit)
398                    .map_err(|err| Error::DeserializeError(err.to_string()))?;
399
400                let mut deserializer = MapDeserializer {
401                    pairs: MapPairs::new(&t, self.options.sort_keys)?,
402                    value: None,
403                    options: self.options,
404                    visited: self.visited,
405                    processed: 0,
406                };
407                let map = visitor.visit_map(&mut deserializer)?;
408                let count = deserializer.pairs.count();
409                if count == 0 {
410                    Ok(map)
411                } else {
412                    Err(de::Error::invalid_length(
413                        deserializer.processed + count,
414                        &"fewer elements in the table",
415                    ))
416                }
417            }
418            Value::UserData(ud) if ud.is_serializable() => {
419                serde_userdata(ud, |value| value.deserialize_map(visitor))
420            }
421            value => Err(de::Error::invalid_type(
422                de::Unexpected::Other(value.type_name()),
423                &"table",
424            )),
425        }
426    }
427
428    #[inline]
429    fn deserialize_struct<V>(
430        self,
431        _name: &'static str,
432        _fields: &'static [&'static str],
433        visitor: V,
434    ) -> Result<V::Value>
435    where
436        V: de::Visitor<'de>,
437    {
438        self.deserialize_map(visitor)
439    }
440
441    #[inline]
442    fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value>
443    where
444        V: de::Visitor<'de>,
445    {
446        match self.value {
447            Value::UserData(ud) if ud.is_serializable() => {
448                serde_userdata(ud, |value| value.deserialize_newtype_struct(name, visitor))
449            }
450            _ => visitor.visit_newtype_struct(self),
451        }
452    }
453
454    #[inline]
455    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
456    where
457        V: de::Visitor<'de>,
458    {
459        match self.value {
460            Value::LightUserData(ud) if ud.0.is_null() => visitor.visit_unit(),
461            _ => self.deserialize_any(visitor),
462        }
463    }
464
465    #[inline]
466    fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
467    where
468        V: de::Visitor<'de>,
469    {
470        match self.value {
471            Value::LightUserData(ud) if ud.0.is_null() => visitor.visit_unit(),
472            _ => self.deserialize_any(visitor),
473        }
474    }
475
476    serde::forward_to_deserialize_any! {
477        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string bytes
478        byte_buf identifier ignored_any
479    }
480}
481
482struct SeqDeserializer<'a> {
483    seq: TableSequence<'a, Value>,
484    options: Options,
485    visited: Rc<RefCell<FxHashSet<*const c_void>>>,
486}
487
488impl<'de> de::SeqAccess<'de> for SeqDeserializer<'_> {
489    type Error = Error;
490
491    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
492    where
493        T: de::DeserializeSeed<'de>,
494    {
495        loop {
496            match self.seq.next() {
497                Some(value) => {
498                    let value = value?;
499                    let skip = check_value_for_skip(&value, self.options, &self.visited)
500                        .map_err(|err| Error::DeserializeError(err.to_string()))?;
501                    if skip {
502                        continue;
503                    }
504                    let visited = Rc::clone(&self.visited);
505                    let deserializer = Deserializer::from_parts(value, self.options, visited);
506                    return seed.deserialize(deserializer).map(Some);
507                }
508                None => return Ok(None),
509            }
510        }
511    }
512
513    fn size_hint(&self) -> Option<usize> {
514        match self.seq.size_hint() {
515            (lower, Some(upper)) if lower == upper => Some(upper),
516            _ => None,
517        }
518    }
519}
520
521#[cfg(feature = "luau")]
522struct VecDeserializer {
523    vec: crate::Vector,
524    next: usize,
525    options: Options,
526    visited: Rc<RefCell<FxHashSet<*const c_void>>>,
527}
528
529#[cfg(feature = "luau")]
530impl<'de> de::SeqAccess<'de> for VecDeserializer {
531    type Error = Error;
532
533    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
534    where
535        T: de::DeserializeSeed<'de>,
536    {
537        match self.vec.0.get(self.next) {
538            Some(&n) => {
539                self.next += 1;
540                let visited = Rc::clone(&self.visited);
541                let deserializer = Deserializer::from_parts(Value::Number(n as _), self.options, visited);
542                seed.deserialize(deserializer).map(Some)
543            }
544            None => Ok(None),
545        }
546    }
547
548    fn size_hint(&self) -> Option<usize> {
549        Some(crate::Vector::SIZE)
550    }
551}
552
553pub(crate) enum MapPairs<'a> {
554    Iter(TablePairs<'a, Value, Value>),
555    Vec(Vec<(Value, Value)>),
556}
557
558impl<'a> MapPairs<'a> {
559    pub(crate) fn new(t: &'a Table, sort_keys: bool) -> Result<Self> {
560        if sort_keys {
561            let mut pairs = t.collect_pairs()?;
562            pairs.sort_by(|(a, _), (b, _)| b.sort_cmp(a)); // reverse order as we pop values from the end
563            Ok(MapPairs::Vec(pairs))
564        } else {
565            Ok(MapPairs::Iter(t.pairs::<Value, Value>()))
566        }
567    }
568
569    pub(crate) fn count(self) -> usize {
570        match self {
571            MapPairs::Iter(iter) => iter.count(),
572            MapPairs::Vec(vec) => vec.len(),
573        }
574    }
575
576    pub(crate) fn size_hint(&self) -> (usize, Option<usize>) {
577        match self {
578            MapPairs::Iter(iter) => iter.size_hint(),
579            MapPairs::Vec(vec) => (vec.len(), Some(vec.len())),
580        }
581    }
582}
583
584impl Iterator for MapPairs<'_> {
585    type Item = Result<(Value, Value)>;
586
587    fn next(&mut self) -> Option<Self::Item> {
588        match self {
589            MapPairs::Iter(iter) => iter.next(),
590            MapPairs::Vec(vec) => vec.pop().map(Ok),
591        }
592    }
593}
594
595struct MapDeserializer<'a> {
596    pairs: MapPairs<'a>,
597    value: Option<Value>,
598    options: Options,
599    visited: Rc<RefCell<FxHashSet<*const c_void>>>,
600    processed: usize,
601}
602
603impl MapDeserializer<'_> {
604    fn next_key_deserializer(&mut self) -> Result<Option<Deserializer>> {
605        loop {
606            match self.pairs.next() {
607                Some(item) => {
608                    let (key, value) = item?;
609                    let skip_key = check_key_for_skip(&key, self.options, &self.visited)
610                        .map_err(|err| Error::DeserializeError(err.to_string()))?;
611                    let skip_value = check_value_for_skip(&value, self.options, &self.visited)
612                        .map_err(|err| Error::DeserializeError(err.to_string()))?;
613                    if skip_key || skip_value {
614                        continue;
615                    }
616                    self.processed += 1;
617                    self.value = Some(value);
618                    let visited = Rc::clone(&self.visited);
619                    let key_de = Deserializer::from_parts(key, self.options, visited);
620                    return Ok(Some(key_de));
621                }
622                None => return Ok(None),
623            }
624        }
625    }
626
627    fn next_value_deserializer(&mut self) -> Result<Deserializer> {
628        match self.value.take() {
629            Some(value) => {
630                let visited = Rc::clone(&self.visited);
631                Ok(Deserializer::from_parts(value, self.options, visited))
632            }
633            None => Err(de::Error::custom("value is missing")),
634        }
635    }
636}
637
638impl<'de> de::MapAccess<'de> for MapDeserializer<'_> {
639    type Error = Error;
640
641    fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
642    where
643        T: de::DeserializeSeed<'de>,
644    {
645        match self.next_key_deserializer() {
646            Ok(Some(key_de)) => seed.deserialize(key_de).map(Some),
647            Ok(None) => Ok(None),
648            Err(error) => Err(error),
649        }
650    }
651
652    fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value>
653    where
654        T: de::DeserializeSeed<'de>,
655    {
656        match self.next_value_deserializer() {
657            Ok(value_de) => seed.deserialize(value_de),
658            Err(error) => Err(error),
659        }
660    }
661
662    fn size_hint(&self) -> Option<usize> {
663        match self.pairs.size_hint() {
664            (lower, Some(upper)) if lower == upper => Some(upper),
665            _ => None,
666        }
667    }
668}
669
670struct EnumDeserializer {
671    variant: String,
672    value: Option<Value>,
673    options: Options,
674    visited: Rc<RefCell<FxHashSet<*const c_void>>>,
675}
676
677impl<'de> de::EnumAccess<'de> for EnumDeserializer {
678    type Error = Error;
679    type Variant = VariantDeserializer;
680
681    fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant)>
682    where
683        T: de::DeserializeSeed<'de>,
684    {
685        let variant = self.variant.into_deserializer();
686        let variant_access = VariantDeserializer {
687            value: self.value,
688            options: self.options,
689            visited: self.visited,
690        };
691        seed.deserialize(variant).map(|v| (v, variant_access))
692    }
693}
694
695struct VariantDeserializer {
696    value: Option<Value>,
697    options: Options,
698    visited: Rc<RefCell<FxHashSet<*const c_void>>>,
699}
700
701impl<'de> de::VariantAccess<'de> for VariantDeserializer {
702    type Error = Error;
703
704    fn unit_variant(self) -> Result<()> {
705        match self.value {
706            Some(_) => Err(de::Error::invalid_type(
707                de::Unexpected::NewtypeVariant,
708                &"unit variant",
709            )),
710            None => Ok(()),
711        }
712    }
713
714    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
715    where
716        T: de::DeserializeSeed<'de>,
717    {
718        match self.value {
719            Some(value) => seed.deserialize(Deserializer::from_parts(value, self.options, self.visited)),
720            None => Err(de::Error::invalid_type(
721                de::Unexpected::UnitVariant,
722                &"newtype variant",
723            )),
724        }
725    }
726
727    fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
728    where
729        V: de::Visitor<'de>,
730    {
731        match self.value {
732            Some(value) => serde::Deserializer::deserialize_seq(
733                Deserializer::from_parts(value, self.options, self.visited),
734                visitor,
735            ),
736            None => Err(de::Error::invalid_type(
737                de::Unexpected::UnitVariant,
738                &"tuple variant",
739            )),
740        }
741    }
742
743    fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
744    where
745        V: de::Visitor<'de>,
746    {
747        match self.value {
748            Some(value) => serde::Deserializer::deserialize_map(
749                Deserializer::from_parts(value, self.options, self.visited),
750                visitor,
751            ),
752            None => Err(de::Error::invalid_type(
753                de::Unexpected::UnitVariant,
754                &"struct variant",
755            )),
756        }
757    }
758}
759
760// Adds `ptr` to the `visited` map and removes on drop
761// Used to track recursive tables but allow to traverse same tables multiple times
762pub(crate) struct RecursionGuard {
763    ptr: *const c_void,
764    visited: Rc<RefCell<FxHashSet<*const c_void>>>,
765}
766
767impl RecursionGuard {
768    #[inline]
769    pub(crate) fn new(
770        table: &Table,
771        visited: &Rc<RefCell<FxHashSet<*const c_void>>>,
772        limit: usize,
773    ) -> StdResult<Self, &'static str> {
774        if visited.borrow().len() >= limit {
775            return Err("recursion limit exceeded");
776        }
777        let visited = Rc::clone(visited);
778        let ptr = table.to_pointer();
779        visited.borrow_mut().insert(ptr);
780        Ok(RecursionGuard { ptr, visited })
781    }
782}
783
784impl Drop for RecursionGuard {
785    fn drop(&mut self) {
786        self.visited.borrow_mut().remove(&self.ptr);
787    }
788}
789
790pub(crate) fn check_key_for_skip(
791    key: &Value,
792    options: Options,
793    visited: &RefCell<FxHashSet<*const c_void>>,
794) -> StdResult<bool, &'static str> {
795    if key.is_null() && !options.deny_unsupported_types {
796        return Ok(true);
797    }
798    check_value_for_skip(key, options, visited)
799}
800
801// Checks `options` and decides should we emit an error or skip next element
802pub(crate) fn check_value_for_skip(
803    value: &Value,
804    options: Options,
805    visited: &RefCell<FxHashSet<*const c_void>>,
806) -> StdResult<bool, &'static str> {
807    match value {
808        Value::Table(table) => {
809            let ptr = table.to_pointer();
810            if visited.borrow().contains(&ptr) {
811                if options.deny_recursive_tables {
812                    return Err("recursive table detected");
813                }
814                return Ok(true); // skip
815            }
816        }
817        Value::LightUserData(ud) if ud.0.is_null() => {}
818        Value::UserData(ud) if ud.is_serializable() => {}
819        Value::Function(_)
820        | Value::Thread(_)
821        | Value::UserData(_)
822        | Value::LightUserData(_)
823        | Value::Error(_)
824            if !options.deny_unsupported_types =>
825        {
826            return Ok(true); // skip
827        }
828        _ => {}
829    }
830    Ok(false) // do not skip
831}
832
833fn serde_userdata<V>(
834    ud: AnyUserData,
835    f: impl FnOnce(serde_value::Value) -> std::result::Result<V, serde_value::DeserializerError>,
836) -> Result<V> {
837    match serde_value::to_value(ud) {
838        Ok(value) => match f(value) {
839            Ok(r) => Ok(r),
840            Err(error) => Err(Error::DeserializeError(error.to_string())),
841        },
842        Err(error) => Err(Error::SerializeError(error.to_string())),
843    }
844}