valence_nbt/
list.rs

1use std::borrow::Cow;
2use std::hash::Hash;
3use std::iter::FusedIterator;
4
5use crate::value::{ValueMut, ValueRef};
6use crate::{Compound, Tag, Value};
7
8/// An NBT list value.
9///
10/// NBT lists are homogeneous, meaning each list element must be of the same
11/// type. This is opposed to a format like JSON where lists can be
12/// heterogeneous. Here is a JSON list that would be illegal in NBT:
13///
14/// ```json
15/// [42, "hello", {}]
16/// ```
17///
18/// Every possible element type has its own variant in this enum. As a result,
19/// heterogeneous lists are unrepresentable.
20#[derive(Clone, Default, Debug)]
21pub enum List<S = String> {
22    /// The list with the element type of `TAG_End` and length of zero.
23    #[default]
24    End,
25    Byte(Vec<i8>),
26    Short(Vec<i16>),
27    Int(Vec<i32>),
28    Long(Vec<i64>),
29    Float(Vec<f32>),
30    Double(Vec<f64>),
31    ByteArray(Vec<Vec<i8>>),
32    String(Vec<S>),
33    List(Vec<List<S>>),
34    Compound(Vec<Compound<S>>),
35    IntArray(Vec<Vec<i32>>),
36    LongArray(Vec<Vec<i64>>),
37}
38
39impl<S> PartialEq for List<S>
40where
41    S: Ord + Hash,
42{
43    fn eq(&self, other: &Self) -> bool {
44        match self {
45            List::End => matches!(other, List::End),
46            List::Byte(list) => matches!(other, List::Byte(other_list) if list == other_list),
47            List::Short(list) => matches!(other, List::Short(other_list) if list == other_list),
48            List::Int(list) => matches!(other, List::Int(other_list) if list == other_list),
49            List::Long(list) => matches!(other, List::Long(other_list) if list == other_list),
50            List::Float(list) => matches!(other, List::Float(other_list) if list == other_list),
51            List::Double(list) => matches!(other, List::Double(other_list) if list == other_list),
52            List::ByteArray(list) => {
53                matches!(other, List::ByteArray(other_list) if list == other_list)
54            }
55            List::String(list) => matches!(other, List::String(other_list) if list == other_list),
56            List::List(list) => matches!(other, List::List(other_list) if list == other_list),
57            List::Compound(list) => {
58                matches!(other, List::Compound(other_list) if list == other_list)
59            }
60            List::IntArray(list) => {
61                matches!(other, List::IntArray(other_list) if list == other_list)
62            }
63            List::LongArray(list) => {
64                matches!(other, List::LongArray(other_list) if list == other_list)
65            }
66        }
67    }
68}
69
70impl<S> List<S> {
71    /// Constructs a new empty NBT list, with the element type of `TAG_End`.
72    pub fn new() -> Self {
73        Self::End
74    }
75
76    /// Returns the length of this list.
77    pub fn len(&self) -> usize {
78        match self {
79            List::End => 0,
80            List::Byte(l) => l.len(),
81            List::Short(l) => l.len(),
82            List::Int(l) => l.len(),
83            List::Long(l) => l.len(),
84            List::Float(l) => l.len(),
85            List::Double(l) => l.len(),
86            List::ByteArray(l) => l.len(),
87            List::String(l) => l.len(),
88            List::List(l) => l.len(),
89            List::Compound(l) => l.len(),
90            List::IntArray(l) => l.len(),
91            List::LongArray(l) => l.len(),
92        }
93    }
94
95    /// Returns `true` if this list has no elements. `false` otherwise.
96    pub fn is_empty(&self) -> bool {
97        self.len() == 0
98    }
99
100    /// Returns the element type of this list.
101    pub fn element_tag(&self) -> Tag {
102        match self {
103            List::End => Tag::End,
104            List::Byte(_) => Tag::Byte,
105            List::Short(_) => Tag::Short,
106            List::Int(_) => Tag::Int,
107            List::Long(_) => Tag::Long,
108            List::Float(_) => Tag::Float,
109            List::Double(_) => Tag::Double,
110            List::ByteArray(_) => Tag::ByteArray,
111            List::String(_) => Tag::String,
112            List::List(_) => Tag::List,
113            List::Compound(_) => Tag::Compound,
114            List::IntArray(_) => Tag::IntArray,
115            List::LongArray(_) => Tag::LongArray,
116        }
117    }
118
119    /// Gets a reference to the value at the given index in this list, or `None`
120    /// if the index is out of bounds.
121    pub fn get(&self, index: usize) -> Option<ValueRef<S>> {
122        match self {
123            List::End => None,
124            List::Byte(list) => list.get(index).map(ValueRef::Byte),
125            List::Short(list) => list.get(index).map(ValueRef::Short),
126            List::Int(list) => list.get(index).map(ValueRef::Int),
127            List::Long(list) => list.get(index).map(ValueRef::Long),
128            List::Float(list) => list.get(index).map(ValueRef::Float),
129            List::Double(list) => list.get(index).map(ValueRef::Double),
130            List::ByteArray(list) => list.get(index).map(|arr| ValueRef::ByteArray(&arr[..])),
131            List::String(list) => list.get(index).map(ValueRef::String),
132            List::List(list) => list.get(index).map(ValueRef::List),
133            List::Compound(list) => list.get(index).map(ValueRef::Compound),
134            List::IntArray(list) => list.get(index).map(|arr| ValueRef::IntArray(&arr[..])),
135            List::LongArray(list) => list.get(index).map(|arr| ValueRef::LongArray(&arr[..])),
136        }
137    }
138
139    /// Gets a mutable reference to the value at the given index in this list,
140    /// or `None` if the index is out of bounds.
141    pub fn get_mut(&mut self, index: usize) -> Option<ValueMut<S>> {
142        match self {
143            List::End => None,
144            List::Byte(list) => list.get_mut(index).map(ValueMut::Byte),
145            List::Short(list) => list.get_mut(index).map(ValueMut::Short),
146            List::Int(list) => list.get_mut(index).map(ValueMut::Int),
147            List::Long(list) => list.get_mut(index).map(ValueMut::Long),
148            List::Float(list) => list.get_mut(index).map(ValueMut::Float),
149            List::Double(list) => list.get_mut(index).map(ValueMut::Double),
150            List::ByteArray(list) => list.get_mut(index).map(ValueMut::ByteArray),
151            List::String(list) => list.get_mut(index).map(ValueMut::String),
152            List::List(list) => list.get_mut(index).map(ValueMut::List),
153            List::Compound(list) => list.get_mut(index).map(ValueMut::Compound),
154            List::IntArray(list) => list.get_mut(index).map(ValueMut::IntArray),
155            List::LongArray(list) => list.get_mut(index).map(ValueMut::LongArray),
156        }
157    }
158
159    /// Attempts to add the given value to the end of this list, failing if
160    /// adding the value would result in the list not being heterogeneous (have
161    /// multiple types inside it). Returns `true` if the value was added,
162    /// `false` otherwise.
163    #[must_use]
164    pub fn try_push<V: Into<Value<S>>>(&mut self, value: V) -> bool {
165        let value = value.into();
166        match self {
167            List::End => {
168                *self = List::from(value);
169                true
170            }
171            List::Byte(list) => {
172                if let Value::Byte(value) = value {
173                    list.push(value);
174                    true
175                } else {
176                    false
177                }
178            }
179            List::Short(list) => {
180                if let Value::Short(value) = value {
181                    list.push(value);
182                    true
183                } else {
184                    false
185                }
186            }
187            List::Int(list) => {
188                if let Value::Int(value) = value {
189                    list.push(value);
190                    true
191                } else {
192                    false
193                }
194            }
195            List::Long(list) => {
196                if let Value::Long(value) = value {
197                    list.push(value);
198                    true
199                } else {
200                    false
201                }
202            }
203            List::Float(list) => {
204                if let Value::Float(value) = value {
205                    list.push(value);
206                    true
207                } else {
208                    false
209                }
210            }
211            List::Double(list) => {
212                if let Value::Double(value) = value {
213                    list.push(value);
214                    true
215                } else {
216                    false
217                }
218            }
219            List::ByteArray(list) => {
220                if let Value::ByteArray(value) = value {
221                    list.push(value);
222                    true
223                } else {
224                    false
225                }
226            }
227            List::String(list) => {
228                if let Value::String(value) = value {
229                    list.push(value);
230                    true
231                } else {
232                    false
233                }
234            }
235            List::List(list) => {
236                if let Value::List(value) = value {
237                    list.push(value);
238                    true
239                } else {
240                    false
241                }
242            }
243            List::Compound(list) => {
244                if let Value::Compound(value) = value {
245                    list.push(value);
246                    true
247                } else {
248                    false
249                }
250            }
251            List::IntArray(list) => {
252                if let Value::IntArray(value) = value {
253                    list.push(value);
254                    true
255                } else {
256                    false
257                }
258            }
259            List::LongArray(list) => {
260                if let Value::LongArray(value) = value {
261                    list.push(value);
262                    true
263                } else {
264                    false
265                }
266            }
267        }
268    }
269
270    /// Attempts to insert the given value at the given index in this list,
271    /// failing if adding the value would result in the list not being
272    /// heterogeneous (have multiple types inside it). Returns `true` if the
273    /// value was added, `false` otherwise.
274    ///
275    /// # Panics
276    ///
277    /// Panics if the index is greater than the length of the list.
278    #[must_use]
279    pub fn try_insert<V: Into<Value<S>>>(&mut self, index: usize, value: V) -> bool {
280        let value = value.into();
281
282        #[cold]
283        #[inline(never)]
284        fn assert_failed(index: usize, len: usize) -> ! {
285            panic!("insertion index (is {index}) should be <= len (is {len})");
286        }
287
288        match self {
289            List::End => {
290                if index > 0 {
291                    assert_failed(index, 0);
292                }
293                *self = List::from(value);
294                true
295            }
296            List::Byte(list) => {
297                if let Value::Byte(value) = value {
298                    list.insert(index, value);
299                    true
300                } else {
301                    false
302                }
303            }
304            List::Short(list) => {
305                if let Value::Short(value) = value {
306                    list.insert(index, value);
307                    true
308                } else {
309                    false
310                }
311            }
312            List::Int(list) => {
313                if let Value::Int(value) = value {
314                    list.insert(index, value);
315                    true
316                } else {
317                    false
318                }
319            }
320            List::Long(list) => {
321                if let Value::Long(value) = value {
322                    list.insert(index, value);
323                    true
324                } else {
325                    false
326                }
327            }
328            List::Float(list) => {
329                if let Value::Float(value) = value {
330                    list.insert(index, value);
331                    true
332                } else {
333                    false
334                }
335            }
336            List::Double(list) => {
337                if let Value::Double(value) = value {
338                    list.insert(index, value);
339                    true
340                } else {
341                    false
342                }
343            }
344            List::ByteArray(list) => {
345                if let Value::ByteArray(value) = value {
346                    list.insert(index, value);
347                    true
348                } else {
349                    false
350                }
351            }
352            List::String(list) => {
353                if let Value::String(value) = value {
354                    list.insert(index, value);
355                    true
356                } else {
357                    false
358                }
359            }
360            List::List(list) => {
361                if let Value::List(value) = value {
362                    list.insert(index, value);
363                    true
364                } else {
365                    false
366                }
367            }
368            List::Compound(list) => {
369                if let Value::Compound(value) = value {
370                    list.insert(index, value);
371                    true
372                } else {
373                    false
374                }
375            }
376            List::IntArray(list) => {
377                if let Value::IntArray(value) = value {
378                    list.insert(index, value);
379                    true
380                } else {
381                    false
382                }
383            }
384            List::LongArray(list) => {
385                if let Value::LongArray(value) = value {
386                    list.insert(index, value);
387                    true
388                } else {
389                    false
390                }
391            }
392        }
393    }
394
395    /// Removes the element at the given index in the list, and returns the
396    /// value removed.
397    ///
398    /// # Panics
399    ///
400    /// Panics if `index` is out of bounds.
401    #[track_caller]
402    pub fn remove(&mut self, index: usize) -> Value<S> {
403        #[cold]
404        #[inline(never)]
405        #[track_caller]
406        fn assert_failed(index: usize, len: usize) -> ! {
407            panic!("removal index (is {index}) should be < len (is {len})");
408        }
409
410        let removed = match self {
411            List::End => assert_failed(index, 0),
412            List::Byte(list) => Value::Byte(list.remove(index)),
413            List::Short(list) => Value::Short(list.remove(index)),
414            List::Int(list) => Value::Int(list.remove(index)),
415            List::Long(list) => Value::Long(list.remove(index)),
416            List::Float(list) => Value::Float(list.remove(index)),
417            List::Double(list) => Value::Double(list.remove(index)),
418            List::ByteArray(list) => Value::ByteArray(list.remove(index)),
419            List::String(list) => Value::String(list.remove(index)),
420            List::List(list) => Value::List(list.remove(index)),
421            List::Compound(list) => Value::Compound(list.remove(index)),
422            List::IntArray(list) => Value::IntArray(list.remove(index)),
423            List::LongArray(list) => Value::LongArray(list.remove(index)),
424        };
425
426        if self.is_empty() {
427            *self = List::End;
428        }
429
430        removed
431    }
432
433    /// Returns only the elements specified by the predicate, passing a mutable
434    /// reference to it.
435    ///
436    /// In other words, removes all elements `e` such that `f(ValueMut(&mut e))`
437    /// returns `false`. This method operates in place, visiting each element
438    /// exactly once in the original order, and preserves the order of the
439    /// retained elements.
440    pub fn retain<F>(&mut self, mut f: F)
441    where
442        F: FnMut(ValueMut<S>) -> bool,
443    {
444        match self {
445            List::End => {}
446            List::Byte(list) => list.retain_mut(|v| f(ValueMut::Byte(v))),
447            List::Short(list) => list.retain_mut(|v| f(ValueMut::Short(v))),
448            List::Int(list) => list.retain_mut(|v| f(ValueMut::Int(v))),
449            List::Long(list) => list.retain_mut(|v| f(ValueMut::Long(v))),
450            List::Float(list) => list.retain_mut(|v| f(ValueMut::Float(v))),
451            List::Double(list) => list.retain_mut(|v| f(ValueMut::Double(v))),
452            List::ByteArray(list) => list.retain_mut(|v| f(ValueMut::ByteArray(v))),
453            List::String(list) => list.retain_mut(|v| f(ValueMut::String(v))),
454            List::List(list) => list.retain_mut(|v| f(ValueMut::List(v))),
455            List::Compound(list) => list.retain_mut(|v| f(ValueMut::Compound(v))),
456            List::IntArray(list) => list.retain_mut(|v| f(ValueMut::IntArray(v))),
457            List::LongArray(list) => list.retain_mut(|v| f(ValueMut::LongArray(v))),
458        }
459
460        if self.is_empty() {
461            *self = List::End;
462        }
463    }
464
465    /// Returns an iterator over this list. This iterator yields [`ValueRef`]s.
466    pub fn iter(&self) -> Iter<S> {
467        Iter {
468            inner: match self {
469                List::End => IterInner::End,
470                List::Byte(list) => IterInner::Byte(list.iter()),
471                List::Short(list) => IterInner::Short(list.iter()),
472                List::Int(list) => IterInner::Int(list.iter()),
473                List::Long(list) => IterInner::Long(list.iter()),
474                List::Float(list) => IterInner::Float(list.iter()),
475                List::Double(list) => IterInner::Double(list.iter()),
476                List::ByteArray(list) => IterInner::ByteArray(list.iter()),
477                List::String(list) => IterInner::String(list.iter()),
478                List::List(list) => IterInner::List(list.iter()),
479                List::Compound(list) => IterInner::Compound(list.iter()),
480                List::IntArray(list) => IterInner::IntArray(list.iter()),
481                List::LongArray(list) => IterInner::LongArray(list.iter()),
482            },
483        }
484    }
485
486    /// Returns a mutable iterator over this list. This iterator yields
487    /// [`ValueMut`]s.
488    pub fn iter_mut(&mut self) -> IterMut<S> {
489        IterMut {
490            inner: match self {
491                List::End => IterMutInner::End,
492                List::Byte(list) => IterMutInner::Byte(list.iter_mut()),
493                List::Short(list) => IterMutInner::Short(list.iter_mut()),
494                List::Int(list) => IterMutInner::Int(list.iter_mut()),
495                List::Long(list) => IterMutInner::Long(list.iter_mut()),
496                List::Float(list) => IterMutInner::Float(list.iter_mut()),
497                List::Double(list) => IterMutInner::Double(list.iter_mut()),
498                List::ByteArray(list) => IterMutInner::ByteArray(list.iter_mut()),
499                List::String(list) => IterMutInner::String(list.iter_mut()),
500                List::List(list) => IterMutInner::List(list.iter_mut()),
501                List::Compound(list) => IterMutInner::Compound(list.iter_mut()),
502                List::IntArray(list) => IterMutInner::IntArray(list.iter_mut()),
503                List::LongArray(list) => IterMutInner::LongArray(list.iter_mut()),
504            },
505        }
506    }
507}
508
509impl<S> From<Vec<i8>> for List<S> {
510    fn from(v: Vec<i8>) -> Self {
511        List::Byte(v)
512    }
513}
514
515impl<S> From<Vec<i16>> for List<S> {
516    fn from(v: Vec<i16>) -> Self {
517        List::Short(v)
518    }
519}
520
521impl<S> From<Vec<i32>> for List<S> {
522    fn from(v: Vec<i32>) -> Self {
523        List::Int(v)
524    }
525}
526
527impl<S> From<Vec<i64>> for List<S> {
528    fn from(v: Vec<i64>) -> Self {
529        List::Long(v)
530    }
531}
532
533impl<S> From<Vec<f32>> for List<S> {
534    fn from(v: Vec<f32>) -> Self {
535        List::Float(v)
536    }
537}
538
539impl<S> From<Vec<f64>> for List<S> {
540    fn from(v: Vec<f64>) -> Self {
541        List::Double(v)
542    }
543}
544
545impl<S> From<Vec<Vec<i8>>> for List<S> {
546    fn from(v: Vec<Vec<i8>>) -> Self {
547        List::ByteArray(v)
548    }
549}
550
551impl From<Vec<String>> for List<String> {
552    fn from(v: Vec<String>) -> Self {
553        List::String(v)
554    }
555}
556
557impl<'a> From<Vec<Cow<'a, str>>> for List<Cow<'a, str>> {
558    fn from(v: Vec<Cow<'a, str>>) -> Self {
559        List::String(v)
560    }
561}
562
563#[cfg(feature = "java_string")]
564impl From<Vec<java_string::JavaString>> for List<java_string::JavaString> {
565    fn from(v: Vec<java_string::JavaString>) -> Self {
566        List::String(v)
567    }
568}
569
570#[cfg(feature = "java_string")]
571impl<'a> From<Vec<Cow<'a, java_string::JavaStr>>> for List<Cow<'a, java_string::JavaStr>> {
572    fn from(v: Vec<Cow<'a, java_string::JavaStr>>) -> Self {
573        List::String(v)
574    }
575}
576
577impl<S> From<Vec<List<S>>> for List<S> {
578    fn from(v: Vec<List<S>>) -> Self {
579        List::List(v)
580    }
581}
582
583impl<S> From<Vec<Compound<S>>> for List<S> {
584    fn from(v: Vec<Compound<S>>) -> Self {
585        List::Compound(v)
586    }
587}
588
589impl<S> From<Vec<Vec<i32>>> for List<S> {
590    fn from(v: Vec<Vec<i32>>) -> Self {
591        List::IntArray(v)
592    }
593}
594
595impl<S> From<Vec<Vec<i64>>> for List<S> {
596    fn from(v: Vec<Vec<i64>>) -> Self {
597        List::LongArray(v)
598    }
599}
600
601/// Converts a value to a singleton list.
602impl<S> From<Value<S>> for List<S> {
603    fn from(value: Value<S>) -> Self {
604        match value {
605            Value::Byte(v) => List::Byte(vec![v]),
606            Value::Short(v) => List::Short(vec![v]),
607            Value::Int(v) => List::Int(vec![v]),
608            Value::Long(v) => List::Long(vec![v]),
609            Value::Float(v) => List::Float(vec![v]),
610            Value::Double(v) => List::Double(vec![v]),
611            Value::ByteArray(v) => List::ByteArray(vec![v]),
612            Value::String(v) => List::String(vec![v]),
613            Value::List(v) => List::List(vec![v]),
614            Value::Compound(v) => List::Compound(vec![v]),
615            Value::IntArray(v) => List::IntArray(vec![v]),
616            Value::LongArray(v) => List::LongArray(vec![v]),
617        }
618    }
619}
620
621impl<S> IntoIterator for List<S> {
622    type Item = Value<S>;
623    type IntoIter = IntoIter<S>;
624
625    fn into_iter(self) -> Self::IntoIter {
626        IntoIter {
627            inner: match self {
628                List::End => IntoIterInner::End,
629                List::Byte(list) => IntoIterInner::Byte(list.into_iter()),
630                List::Short(list) => IntoIterInner::Short(list.into_iter()),
631                List::Int(list) => IntoIterInner::Int(list.into_iter()),
632                List::Long(list) => IntoIterInner::Long(list.into_iter()),
633                List::Float(list) => IntoIterInner::Float(list.into_iter()),
634                List::Double(list) => IntoIterInner::Double(list.into_iter()),
635                List::ByteArray(list) => IntoIterInner::ByteArray(list.into_iter()),
636                List::String(list) => IntoIterInner::String(list.into_iter()),
637                List::List(list) => IntoIterInner::List(list.into_iter()),
638                List::Compound(list) => IntoIterInner::Compound(list.into_iter()),
639                List::IntArray(list) => IntoIterInner::IntArray(list.into_iter()),
640                List::LongArray(list) => IntoIterInner::LongArray(list.into_iter()),
641            },
642        }
643    }
644}
645
646impl<'a, S> IntoIterator for &'a List<S> {
647    type Item = ValueRef<'a, S>;
648    type IntoIter = Iter<'a, S>;
649
650    fn into_iter(self) -> Self::IntoIter {
651        self.iter()
652    }
653}
654
655impl<'a, S> IntoIterator for &'a mut List<S> {
656    type Item = ValueMut<'a, S>;
657    type IntoIter = IterMut<'a, S>;
658
659    fn into_iter(self) -> Self::IntoIter {
660        self.iter_mut()
661    }
662}
663
664/// The owned iterator type for [`List`].
665#[derive(Clone, Debug)]
666pub struct IntoIter<S = String> {
667    inner: IntoIterInner<S>,
668}
669
670#[derive(Clone, Debug)]
671enum IntoIterInner<S> {
672    End,
673    Byte(std::vec::IntoIter<i8>),
674    Short(std::vec::IntoIter<i16>),
675    Int(std::vec::IntoIter<i32>),
676    Long(std::vec::IntoIter<i64>),
677    Float(std::vec::IntoIter<f32>),
678    Double(std::vec::IntoIter<f64>),
679    ByteArray(std::vec::IntoIter<Vec<i8>>),
680    String(std::vec::IntoIter<S>),
681    List(std::vec::IntoIter<List<S>>),
682    Compound(std::vec::IntoIter<Compound<S>>),
683    IntArray(std::vec::IntoIter<Vec<i32>>),
684    LongArray(std::vec::IntoIter<Vec<i64>>),
685}
686
687impl<S> Iterator for IntoIter<S> {
688    type Item = Value<S>;
689
690    fn next(&mut self) -> Option<Self::Item> {
691        match &mut self.inner {
692            IntoIterInner::End => None,
693            IntoIterInner::Byte(i) => i.next().map(Value::Byte),
694            IntoIterInner::Short(i) => i.next().map(Value::Short),
695            IntoIterInner::Int(i) => i.next().map(Value::Int),
696            IntoIterInner::Long(i) => i.next().map(Value::Long),
697            IntoIterInner::Float(i) => i.next().map(Value::Float),
698            IntoIterInner::Double(i) => i.next().map(Value::Double),
699            IntoIterInner::ByteArray(i) => i.next().map(Value::ByteArray),
700            IntoIterInner::String(i) => i.next().map(Value::String),
701            IntoIterInner::List(i) => i.next().map(Value::List),
702            IntoIterInner::Compound(i) => i.next().map(Value::Compound),
703            IntoIterInner::IntArray(i) => i.next().map(Value::IntArray),
704            IntoIterInner::LongArray(i) => i.next().map(Value::LongArray),
705        }
706    }
707
708    fn size_hint(&self) -> (usize, Option<usize>) {
709        match &self.inner {
710            IntoIterInner::End => (0, Some(0)),
711            IntoIterInner::Byte(i) => i.size_hint(),
712            IntoIterInner::Short(i) => i.size_hint(),
713            IntoIterInner::Int(i) => i.size_hint(),
714            IntoIterInner::Long(i) => i.size_hint(),
715            IntoIterInner::Float(i) => i.size_hint(),
716            IntoIterInner::Double(i) => i.size_hint(),
717            IntoIterInner::ByteArray(i) => i.size_hint(),
718            IntoIterInner::String(i) => i.size_hint(),
719            IntoIterInner::List(i) => i.size_hint(),
720            IntoIterInner::Compound(i) => i.size_hint(),
721            IntoIterInner::IntArray(i) => i.size_hint(),
722            IntoIterInner::LongArray(i) => i.size_hint(),
723        }
724    }
725}
726
727impl<S> DoubleEndedIterator for IntoIter<S> {
728    fn next_back(&mut self) -> Option<Self::Item> {
729        match &mut self.inner {
730            IntoIterInner::End => None,
731            IntoIterInner::Byte(i) => i.next_back().map(Value::Byte),
732            IntoIterInner::Short(i) => i.next_back().map(Value::Short),
733            IntoIterInner::Int(i) => i.next_back().map(Value::Int),
734            IntoIterInner::Long(i) => i.next_back().map(Value::Long),
735            IntoIterInner::Float(i) => i.next_back().map(Value::Float),
736            IntoIterInner::Double(i) => i.next_back().map(Value::Double),
737            IntoIterInner::ByteArray(i) => i.next_back().map(Value::ByteArray),
738            IntoIterInner::String(i) => i.next_back().map(Value::String),
739            IntoIterInner::List(i) => i.next_back().map(Value::List),
740            IntoIterInner::Compound(i) => i.next_back().map(Value::Compound),
741            IntoIterInner::IntArray(i) => i.next_back().map(Value::IntArray),
742            IntoIterInner::LongArray(i) => i.next_back().map(Value::LongArray),
743        }
744    }
745}
746
747impl<S> ExactSizeIterator for IntoIter<S> {
748    fn len(&self) -> usize {
749        match &self.inner {
750            IntoIterInner::End => 0,
751            IntoIterInner::Byte(i) => i.len(),
752            IntoIterInner::Short(i) => i.len(),
753            IntoIterInner::Int(i) => i.len(),
754            IntoIterInner::Long(i) => i.len(),
755            IntoIterInner::Float(i) => i.len(),
756            IntoIterInner::Double(i) => i.len(),
757            IntoIterInner::ByteArray(i) => i.len(),
758            IntoIterInner::String(i) => i.len(),
759            IntoIterInner::List(i) => i.len(),
760            IntoIterInner::Compound(i) => i.len(),
761            IntoIterInner::IntArray(i) => i.len(),
762            IntoIterInner::LongArray(i) => i.len(),
763        }
764    }
765}
766
767impl<S> FusedIterator for IntoIter<S> {}
768
769/// The borrowing iterator type for [`List`].
770#[derive(Clone, Debug)]
771pub struct Iter<'a, S = String> {
772    inner: IterInner<'a, S>,
773}
774
775#[derive(Clone, Debug)]
776enum IterInner<'a, S> {
777    End,
778    Byte(std::slice::Iter<'a, i8>),
779    Short(std::slice::Iter<'a, i16>),
780    Int(std::slice::Iter<'a, i32>),
781    Long(std::slice::Iter<'a, i64>),
782    Float(std::slice::Iter<'a, f32>),
783    Double(std::slice::Iter<'a, f64>),
784    ByteArray(std::slice::Iter<'a, Vec<i8>>),
785    String(std::slice::Iter<'a, S>),
786    List(std::slice::Iter<'a, List<S>>),
787    Compound(std::slice::Iter<'a, Compound<S>>),
788    IntArray(std::slice::Iter<'a, Vec<i32>>),
789    LongArray(std::slice::Iter<'a, Vec<i64>>),
790}
791
792impl<'a, S> Iterator for Iter<'a, S> {
793    type Item = ValueRef<'a, S>;
794
795    fn next(&mut self) -> Option<Self::Item> {
796        match &mut self.inner {
797            IterInner::End => None,
798            IterInner::Byte(i) => i.next().map(ValueRef::Byte),
799            IterInner::Short(i) => i.next().map(ValueRef::Short),
800            IterInner::Int(i) => i.next().map(ValueRef::Int),
801            IterInner::Long(i) => i.next().map(ValueRef::Long),
802            IterInner::Float(i) => i.next().map(ValueRef::Float),
803            IterInner::Double(i) => i.next().map(ValueRef::Double),
804            IterInner::ByteArray(i) => i.next().map(|arr| ValueRef::ByteArray(&arr[..])),
805            IterInner::String(i) => i.next().map(ValueRef::String),
806            IterInner::List(i) => i.next().map(ValueRef::List),
807            IterInner::Compound(i) => i.next().map(ValueRef::Compound),
808            IterInner::IntArray(i) => i.next().map(|arr| ValueRef::IntArray(&arr[..])),
809            IterInner::LongArray(i) => i.next().map(|arr| ValueRef::LongArray(&arr[..])),
810        }
811    }
812
813    fn size_hint(&self) -> (usize, Option<usize>) {
814        match &self.inner {
815            IterInner::End => (0, Some(0)),
816            IterInner::Byte(i) => i.size_hint(),
817            IterInner::Short(i) => i.size_hint(),
818            IterInner::Int(i) => i.size_hint(),
819            IterInner::Long(i) => i.size_hint(),
820            IterInner::Float(i) => i.size_hint(),
821            IterInner::Double(i) => i.size_hint(),
822            IterInner::ByteArray(i) => i.size_hint(),
823            IterInner::String(i) => i.size_hint(),
824            IterInner::List(i) => i.size_hint(),
825            IterInner::Compound(i) => i.size_hint(),
826            IterInner::IntArray(i) => i.size_hint(),
827            IterInner::LongArray(i) => i.size_hint(),
828        }
829    }
830}
831
832impl<S> DoubleEndedIterator for Iter<'_, S> {
833    fn next_back(&mut self) -> Option<Self::Item> {
834        match &mut self.inner {
835            IterInner::End => None,
836            IterInner::Byte(i) => i.next_back().map(ValueRef::Byte),
837            IterInner::Short(i) => i.next_back().map(ValueRef::Short),
838            IterInner::Int(i) => i.next_back().map(ValueRef::Int),
839            IterInner::Long(i) => i.next_back().map(ValueRef::Long),
840            IterInner::Float(i) => i.next_back().map(ValueRef::Float),
841            IterInner::Double(i) => i.next_back().map(ValueRef::Double),
842            IterInner::ByteArray(i) => i.next_back().map(|arr| ValueRef::ByteArray(&arr[..])),
843            IterInner::String(i) => i.next_back().map(ValueRef::String),
844            IterInner::List(i) => i.next_back().map(ValueRef::List),
845            IterInner::Compound(i) => i.next_back().map(ValueRef::Compound),
846            IterInner::IntArray(i) => i.next_back().map(|arr| ValueRef::IntArray(&arr[..])),
847            IterInner::LongArray(i) => i.next_back().map(|arr| ValueRef::LongArray(&arr[..])),
848        }
849    }
850}
851
852impl<S> ExactSizeIterator for Iter<'_, S> {
853    fn len(&self) -> usize {
854        match &self.inner {
855            IterInner::End => 0,
856            IterInner::Byte(i) => i.len(),
857            IterInner::Short(i) => i.len(),
858            IterInner::Int(i) => i.len(),
859            IterInner::Long(i) => i.len(),
860            IterInner::Float(i) => i.len(),
861            IterInner::Double(i) => i.len(),
862            IterInner::ByteArray(i) => i.len(),
863            IterInner::String(i) => i.len(),
864            IterInner::List(i) => i.len(),
865            IterInner::Compound(i) => i.len(),
866            IterInner::IntArray(i) => i.len(),
867            IterInner::LongArray(i) => i.len(),
868        }
869    }
870}
871
872impl<S> FusedIterator for Iter<'_, S> {}
873
874/// The mutable borrowing iterator type for [`List`].
875#[derive(Debug)]
876pub struct IterMut<'a, S = String> {
877    inner: IterMutInner<'a, S>,
878}
879
880#[derive(Debug)]
881enum IterMutInner<'a, S> {
882    End,
883    Byte(std::slice::IterMut<'a, i8>),
884    Short(std::slice::IterMut<'a, i16>),
885    Int(std::slice::IterMut<'a, i32>),
886    Long(std::slice::IterMut<'a, i64>),
887    Float(std::slice::IterMut<'a, f32>),
888    Double(std::slice::IterMut<'a, f64>),
889    ByteArray(std::slice::IterMut<'a, Vec<i8>>),
890    String(std::slice::IterMut<'a, S>),
891    List(std::slice::IterMut<'a, List<S>>),
892    Compound(std::slice::IterMut<'a, Compound<S>>),
893    IntArray(std::slice::IterMut<'a, Vec<i32>>),
894    LongArray(std::slice::IterMut<'a, Vec<i64>>),
895}
896
897impl<'a, S> Iterator for IterMut<'a, S> {
898    type Item = ValueMut<'a, S>;
899
900    fn next(&mut self) -> Option<Self::Item> {
901        match &mut self.inner {
902            IterMutInner::End => None,
903            IterMutInner::Byte(i) => i.next().map(ValueMut::Byte),
904            IterMutInner::Short(i) => i.next().map(ValueMut::Short),
905            IterMutInner::Int(i) => i.next().map(ValueMut::Int),
906            IterMutInner::Long(i) => i.next().map(ValueMut::Long),
907            IterMutInner::Float(i) => i.next().map(ValueMut::Float),
908            IterMutInner::Double(i) => i.next().map(ValueMut::Double),
909            IterMutInner::ByteArray(i) => i.next().map(ValueMut::ByteArray),
910            IterMutInner::String(i) => i.next().map(ValueMut::String),
911            IterMutInner::List(i) => i.next().map(ValueMut::List),
912            IterMutInner::Compound(i) => i.next().map(ValueMut::Compound),
913            IterMutInner::IntArray(i) => i.next().map(ValueMut::IntArray),
914            IterMutInner::LongArray(i) => i.next().map(ValueMut::LongArray),
915        }
916    }
917
918    fn size_hint(&self) -> (usize, Option<usize>) {
919        match &self.inner {
920            IterMutInner::End => (0, Some(0)),
921            IterMutInner::Byte(i) => i.size_hint(),
922            IterMutInner::Short(i) => i.size_hint(),
923            IterMutInner::Int(i) => i.size_hint(),
924            IterMutInner::Long(i) => i.size_hint(),
925            IterMutInner::Float(i) => i.size_hint(),
926            IterMutInner::Double(i) => i.size_hint(),
927            IterMutInner::ByteArray(i) => i.size_hint(),
928            IterMutInner::String(i) => i.size_hint(),
929            IterMutInner::List(i) => i.size_hint(),
930            IterMutInner::Compound(i) => i.size_hint(),
931            IterMutInner::IntArray(i) => i.size_hint(),
932            IterMutInner::LongArray(i) => i.size_hint(),
933        }
934    }
935}
936
937impl<S> DoubleEndedIterator for IterMut<'_, S> {
938    fn next_back(&mut self) -> Option<Self::Item> {
939        match &mut self.inner {
940            IterMutInner::End => None,
941            IterMutInner::Byte(i) => i.next_back().map(ValueMut::Byte),
942            IterMutInner::Short(i) => i.next_back().map(ValueMut::Short),
943            IterMutInner::Int(i) => i.next_back().map(ValueMut::Int),
944            IterMutInner::Long(i) => i.next_back().map(ValueMut::Long),
945            IterMutInner::Float(i) => i.next_back().map(ValueMut::Float),
946            IterMutInner::Double(i) => i.next_back().map(ValueMut::Double),
947            IterMutInner::ByteArray(i) => i.next_back().map(ValueMut::ByteArray),
948            IterMutInner::String(i) => i.next_back().map(ValueMut::String),
949            IterMutInner::List(i) => i.next_back().map(ValueMut::List),
950            IterMutInner::Compound(i) => i.next_back().map(ValueMut::Compound),
951            IterMutInner::IntArray(i) => i.next_back().map(ValueMut::IntArray),
952            IterMutInner::LongArray(i) => i.next_back().map(ValueMut::LongArray),
953        }
954    }
955}
956
957impl<S> ExactSizeIterator for IterMut<'_, S> {
958    fn len(&self) -> usize {
959        match &self.inner {
960            IterMutInner::End => 0,
961            IterMutInner::Byte(i) => i.len(),
962            IterMutInner::Short(i) => i.len(),
963            IterMutInner::Int(i) => i.len(),
964            IterMutInner::Long(i) => i.len(),
965            IterMutInner::Float(i) => i.len(),
966            IterMutInner::Double(i) => i.len(),
967            IterMutInner::ByteArray(i) => i.len(),
968            IterMutInner::String(i) => i.len(),
969            IterMutInner::List(i) => i.len(),
970            IterMutInner::Compound(i) => i.len(),
971            IterMutInner::IntArray(i) => i.len(),
972            IterMutInner::LongArray(i) => i.len(),
973        }
974    }
975}
976
977impl<S> FusedIterator for IterMut<'_, S> {}