1#![cfg_attr(
3 feature = "tracing",
4 allow(clippy::used_underscore_binding, reason = "Only used in tracing::instrument")
5)]
6
7mod de;
8mod ser;
9
10use ::alloc::{
11 borrow::{Cow, ToOwned},
12 boxed::Box,
13 collections::VecDeque,
14 string::String,
15 vec::Vec,
16};
17use ::core::{
18 fmt::Write,
19 marker::PhantomData,
20 ops::{Deref, DerefMut},
21};
22use ::serde::{Deserialize, Serialize};
23
24use crate::{Config, Result};
25
26#[cfg_attr(feature = "tracing", ::tracing::instrument(skip(value)))]
28pub fn to_value_with_config<T>(value: &T, config: Config) -> Result<Value<'static>>
29where
30 T: Serialize,
31{
32 let ser = ser::ValueSerializer::new(config.use_indices);
33 value.serialize(ser)
34}
35
36pub fn to_value<T>(value: &T) -> Result<Value<'static>>
38where
39 T: Serialize,
40{
41 to_value_with_config(value, Config::default())
42}
43
44#[cfg_attr(feature = "tracing", ::tracing::instrument(skip(value)))]
46pub fn from_value_with_config<'de, T>(value: Value<'de>, _config: Config) -> Result<T>
47where
48 T: Deserialize<'de>,
49{
50 let de = de::ValueDeserializer::new(value);
51 T::deserialize(de)
52}
53
54pub fn from_value<'de, T>(value: Value<'de>) -> Result<T>
56where
57 T: Deserialize<'de>,
58{
59 from_value_with_config(value, Config::default())
60}
61
62#[derive(Debug, Clone, Default)]
68pub enum Value<'a> {
69 #[default]
71 Null,
72 Bool(bool),
74 Integer(Integer),
76 Float(Float),
78 Bytes(Cow<'a, [u8]>),
80 String(Cow<'a, str>),
82 Array(VecDeque<Self>),
84 Map(VecDeque<(Self, Self)>),
86}
87
88#[derive(Debug, Clone, Default)]
90pub struct OwnedValue(Value<'static>);
91
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
94pub enum Integer {
95 Unsigned(u128),
97 Signed(i128),
99}
100
101#[derive(Debug, Clone, Copy, PartialEq)]
103pub enum Float {
104 F32(f32),
106 F64(f64),
108}
109
110impl<'a> Value<'a> {
111 pub fn borrow_clone(&self) -> Value<'_> {
113 match self {
114 Value::Null => Value::Null,
115 Value::Bool(b) => Value::Bool(*b),
116 Value::Integer(int) => Value::Integer(*int),
117 Value::Float(float) => Value::Float(*float),
118 Value::Bytes(bytes) => Value::Bytes(Cow::Borrowed(bytes)),
119 Value::String(s) => Value::String(Cow::Borrowed(s)),
120 Value::Array(arr) => Value::Array(arr.iter().map(Self::borrow_clone).collect()),
121 Value::Map(map) => Value::Map(
122 map.iter().map(|(key, value)| (key.borrow_clone(), value.borrow_clone())).collect(),
123 ),
124 }
125 }
126
127 #[must_use]
129 pub fn into_owned(self) -> OwnedValue {
130 let value = match self {
131 Value::Null => Value::Null,
132 Value::Bool(b) => Value::Bool(b),
133 Value::Integer(int) => Value::Integer(int),
134 Value::Float(float) => Value::Float(float),
135 Value::Bytes(bytes) => Value::Bytes(bytes.into_owned().into()),
136 Value::String(s) => Value::String(s.into_owned().into()),
137 Value::Array(arr) => Value::Array(arr.into_iter().map(|v| v.into_owned().0).collect()),
138 Value::Map(map) => Value::Map(
139 map.into_iter()
140 .map(|(key, value)| (key.into_owned().0, value.into_owned().0))
141 .collect(),
142 ),
143 };
144 OwnedValue(value)
145 }
146
147 #[inline]
149 pub fn deserialize_as<'de, T>(self) -> Result<T>
150 where
151 'a: 'de,
152 T: Deserialize<'de>,
153 {
154 T::deserialize(de::ValueDeserializer::new(self))
155 }
156
157 #[must_use]
162 #[inline]
163 pub fn is_empty(&self) -> bool {
164 match self {
165 Value::Null => true,
166 Value::Bool(_) | Value::Integer(_) | Value::Float(_) => false,
167 Value::Bytes(bytes) => bytes.is_empty(),
168 Value::String(s) => s.is_empty(),
169 Value::Array(arr) => arr.is_empty(),
170 Value::Map(map) => map.is_empty(),
171 }
172 }
173
174 #[must_use]
176 pub const fn as_bool(&self) -> Option<bool> {
177 if let Value::Bool(v) = self { Some(*v) } else { None }
178 }
179
180 #[must_use]
182 pub const fn as_int(&self) -> Option<Integer> {
183 if let Value::Integer(v) = self { Some(*v) } else { None }
184 }
185
186 #[must_use]
188 pub const fn as_float(&self) -> Option<Float> {
189 if let Value::Float(v) = self { Some(*v) } else { None }
190 }
191
192 #[must_use]
194 pub fn as_bytes(&self) -> Option<&[u8]> {
195 if let Value::Bytes(v) = self { Some(v) } else { None }
196 }
197
198 #[must_use]
200 pub fn as_string(&self) -> Option<&str> {
201 if let Value::String(v) = self { Some(v) } else { None }
202 }
203
204 #[must_use]
206 pub const fn as_array(&self) -> Option<&VecDeque<Value<'a>>> {
207 if let Value::Array(v) = self { Some(v) } else { None }
208 }
209
210 #[must_use]
212 pub const fn as_map(&self) -> Option<&VecDeque<(Value<'a>, Value<'a>)>> {
213 if let Value::Map(v) = self { Some(v) } else { None }
214 }
215
216 #[must_use]
218 pub fn into_values(self) -> Iter<Value<'static>> {
219 match self.into_owned().0 {
220 Value::Array(arr) => Iter::new(arr.into_iter()),
221 Value::Map(map) => Iter::new(map.into_iter().map(|(_key, value)| value)),
222 _ => Iter::new(::core::iter::empty()),
223 }
224 }
225}
226
227impl OwnedValue {
228 #[must_use]
230 pub fn new(value: Value<'_>) -> Self {
231 value.into_owned()
232 }
233
234 #[must_use]
236 pub fn into_inner(self) -> Value<'static> {
237 self.0
238 }
239}
240
241impl Deref for OwnedValue {
242 type Target = Value<'static>;
243
244 fn deref(&self) -> &Self::Target {
245 &self.0
246 }
247}
248
249impl DerefMut for OwnedValue {
250 fn deref_mut(&mut self) -> &mut Self::Target {
251 &mut self.0
252 }
253}
254
255impl ::core::fmt::Display for Value<'_> {
256 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
257 match self {
258 Value::Null => f.write_str("null"),
259 Value::Bool(b) if *b => f.write_str("true"),
260 Value::Bool(_) => f.write_str("false"),
261 Value::Integer(int) => ::core::fmt::Display::fmt(int, f),
262 Value::Float(float) => ::core::fmt::Display::fmt(float, f),
263 Value::Bytes(bytes) => {
264 f.write_str("0x")?;
265 for (i, byte) in bytes.iter().enumerate() {
266 if i > 0 && i % 4 == 0 {
267 f.write_char('_')?;
268 }
269 write!(f, "{byte:02X}")?;
270 }
271 Ok(())
272 }
273 Value::String(s) => f.write_str(s),
274 Value::Array(arr) => {
275 f.write_char('[')?;
276 for (i, value) in arr.iter().enumerate() {
277 if i > 0 {
278 f.write_str(", ")?;
279 }
280 ::core::fmt::Display::fmt(value, f)?;
281 }
282 f.write_char(']')
283 }
284 Value::Map(map) => {
285 f.write_char('{')?;
286 for (i, (key, value)) in map.iter().enumerate() {
287 if i > 0 {
288 f.write_str(", ")?;
289 }
290 ::core::fmt::Display::fmt(key, f)?;
291 f.write_str(": ")?;
292 ::core::fmt::Display::fmt(value, f)?;
293 }
294 f.write_char('}')
295 }
296 }
297 }
298}
299
300impl ::core::fmt::Display for Integer {
301 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
302 match self {
303 Integer::Unsigned(int) => ::core::fmt::Display::fmt(int, f),
304 Integer::Signed(int) => ::core::fmt::Display::fmt(int, f),
305 }
306 }
307}
308
309impl ::core::fmt::Display for Float {
310 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
311 match self {
312 Float::F32(float) => ::core::fmt::Display::fmt(float, f),
313 Float::F64(float) => ::core::fmt::Display::fmt(float, f),
314 }
315 }
316}
317
318impl Serialize for Value<'_> {
319 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
320 where
321 S: ::serde::Serializer,
322 {
323 match self {
324 Value::Null => serializer.serialize_none(),
325 Value::Bool(b) => serializer.serialize_bool(*b),
326 Value::Integer(Integer::Unsigned(int)) => serializer.serialize_u128(*int),
327 Value::Integer(Integer::Signed(int)) => serializer.serialize_i128(*int),
328 Value::Float(Float::F32(float)) => serializer.serialize_f32(*float),
329 Value::Float(Float::F64(float)) => serializer.serialize_f64(*float),
330 Value::Bytes(bytes) => serializer.serialize_bytes(bytes),
331 Value::String(s) => serializer.serialize_str(s),
332 Value::Array(arr) => {
333 use ::serde::ser::SerializeSeq;
334
335 let mut ser = serializer.serialize_seq(Some(arr.len()))?;
336 for value in arr {
337 ser.serialize_element(value)?;
338 }
339 ser.end()
340 }
341 Value::Map(map) => {
342 use ::serde::ser::SerializeMap;
343
344 let mut ser = serializer.serialize_map(Some(map.len()))?;
345 for (key, value) in map {
346 ser.serialize_entry(key, value)?;
347 }
348 ser.end()
349 }
350 }
351 }
352}
353
354impl<'de> Deserialize<'de> for Value<'de> {
355 #[inline]
356 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
357 where
358 D: serde::Deserializer<'de>,
359 {
360 deserializer.deserialize_any(ValueVisitor::default())
361 }
362}
363
364impl<'de> Deserialize<'de> for OwnedValue {
365 #[inline]
366 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
367 where
368 D: serde::Deserializer<'de>,
369 {
370 deserializer.deserialize_any(ValueVisitor::default()).map(Value::into_owned)
371 }
372}
373
374#[derive(Debug, Clone, Copy, Default)]
376struct ValueVisitor<'a>(PhantomData<Value<'a>>);
377
378impl<'de> ::serde::de::Visitor<'de> for ValueVisitor<'de> {
379 type Value = Value<'de>;
380
381 #[inline]
382 fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
383 formatter.write_str("any value")
384 }
385
386 #[inline]
387 fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
388 where
389 E: serde::de::Error,
390 {
391 Ok(Value::Bool(v))
392 }
393
394 #[inline]
395 fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
396 where
397 E: serde::de::Error,
398 {
399 self.visit_i128(i128::from(v))
400 }
401
402 #[inline]
403 fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
404 where
405 E: serde::de::Error,
406 {
407 Ok(Value::Integer(Integer::Signed(v)))
408 }
409
410 #[inline]
411 fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
412 where
413 E: serde::de::Error,
414 {
415 self.visit_u128(u128::from(v))
416 }
417
418 #[inline]
419 fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
420 where
421 E: serde::de::Error,
422 {
423 Ok(Value::Integer(Integer::Unsigned(v)))
424 }
425
426 #[inline]
427 fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
428 where
429 E: serde::de::Error,
430 {
431 Ok(Value::Float(Float::F32(v)))
432 }
433
434 #[inline]
435 fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
436 where
437 E: serde::de::Error,
438 {
439 Ok(Value::Float(Float::F64(v)))
440 }
441
442 #[inline]
443 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
444 where
445 E: serde::de::Error,
446 {
447 Ok(Value::String(Cow::Owned(v.to_owned())))
448 }
449
450 #[inline]
451 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
452 where
453 E: serde::de::Error,
454 {
455 Ok(Value::String(Cow::Borrowed(v)))
456 }
457
458 #[inline]
459 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
460 where
461 E: serde::de::Error,
462 {
463 Ok(Value::String(Cow::Owned(v)))
464 }
465
466 #[inline]
467 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
468 where
469 E: serde::de::Error,
470 {
471 Ok(Value::Bytes(Cow::Owned(v.to_vec())))
472 }
473
474 #[inline]
475 fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
476 where
477 E: serde::de::Error,
478 {
479 Ok(Value::Bytes(Cow::Borrowed(v)))
480 }
481
482 #[inline]
483 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
484 where
485 E: serde::de::Error,
486 {
487 Ok(Value::Bytes(Cow::Owned(v)))
488 }
489
490 #[inline]
491 fn visit_none<E>(self) -> Result<Self::Value, E>
492 where
493 E: serde::de::Error,
494 {
495 Ok(Value::Null)
496 }
497
498 #[inline]
499 fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
500 where
501 D: serde::Deserializer<'de>,
502 {
503 deserializer.deserialize_any(self)
504 }
505
506 #[inline]
507 fn visit_unit<E>(self) -> Result<Self::Value, E>
508 where
509 E: serde::de::Error,
510 {
511 Ok(Value::Null)
512 }
513
514 #[inline]
515 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
516 where
517 A: serde::de::SeqAccess<'de>,
518 {
519 let mut arr = seq.size_hint().map_or_else(VecDeque::new, VecDeque::with_capacity);
520
521 while let Some(value) = seq.next_element()? {
522 arr.push_back(value);
523 }
524
525 Ok(Value::Array(arr))
526 }
527
528 #[inline]
529 fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
530 where
531 A: serde::de::MapAccess<'de>,
532 {
533 let mut entries = map.size_hint().map_or_else(VecDeque::new, VecDeque::with_capacity);
534
535 while let Some((key, value)) = map.next_entry()? {
536 entries.push_back((key, value));
537 }
538
539 Ok(Value::Map(entries))
540 }
541}
542
543impl PartialEq for Value<'_> {
544 fn eq(&self, other: &Self) -> bool {
545 match (self, other) {
546 (Self::Null, Self::Null) => true,
547 (Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
548 (Self::Integer(l0), Self::Integer(r0)) => l0 == r0,
549 (Self::Float(l0), Self::Float(r0)) => l0 == r0,
550 (Self::Bytes(l0), Self::Bytes(r0)) => l0 == r0,
551 (Self::String(l0), Self::String(r0)) => l0 == r0,
552 (Self::Array(l0), Self::Array(r0)) => l0 == r0,
553 (Self::Map(l0), Self::Map(r0)) => l0 == r0,
554 _ => false,
555 }
556 }
557}
558
559impl PartialEq<bool> for Value<'_> {
560 fn eq(&self, other: &bool) -> bool {
561 match self {
562 Value::Bool(b) => b == other,
563 _ => false,
564 }
565 }
566}
567
568impl PartialEq<u128> for Value<'_> {
569 fn eq(&self, other: &u128) -> bool {
570 match self {
571 Value::Integer(Integer::Unsigned(int)) => int == other,
572 _ => false,
573 }
574 }
575}
576
577impl PartialEq<i128> for Value<'_> {
578 fn eq(&self, other: &i128) -> bool {
579 match self {
580 Value::Integer(Integer::Signed(int)) => int == other,
581 _ => false,
582 }
583 }
584}
585
586impl PartialEq<f32> for Value<'_> {
587 fn eq(&self, other: &f32) -> bool {
588 match self {
589 Value::Float(Float::F32(float)) => float == other,
590 Value::Float(Float::F64(float)) => *float == f64::from(*other),
591 _ => false,
592 }
593 }
594}
595
596impl PartialEq<f64> for Value<'_> {
597 fn eq(&self, other: &f64) -> bool {
598 match self {
599 Value::Float(Float::F32(float)) => f64::from(*float) == *other,
600 Value::Float(Float::F64(float)) => float == other,
601 _ => false,
602 }
603 }
604}
605
606impl PartialEq<[u8]> for Value<'_> {
607 fn eq(&self, other: &[u8]) -> bool {
608 match self {
609 Value::Bytes(bytes) => bytes.as_ref() == other,
610 _ => false,
611 }
612 }
613}
614
615impl PartialEq<str> for Value<'_> {
616 fn eq(&self, other: &str) -> bool {
617 match self {
618 Value::String(s) => s == other,
619 _ => false,
620 }
621 }
622}
623
624impl<'a> From<Option<Value<'a>>> for Value<'a> {
625 #[inline]
626 fn from(value: Option<Value<'a>>) -> Self {
627 value.unwrap_or_else(|| Value::Null)
628 }
629}
630
631impl From<()> for Value<'_> {
632 #[inline]
633 fn from((): ()) -> Self {
634 Value::Null
635 }
636}
637
638impl From<bool> for Value<'_> {
639 #[inline]
640 fn from(value: bool) -> Self {
641 Value::Bool(value)
642 }
643}
644
645impl From<u8> for Value<'_> {
646 #[inline]
647 fn from(value: u8) -> Self {
648 Value::Integer(Integer::Unsigned(u128::from(value)))
649 }
650}
651
652impl From<i8> for Value<'_> {
653 #[inline]
654 fn from(value: i8) -> Self {
655 Value::Integer(Integer::Signed(i128::from(value)))
656 }
657}
658
659impl From<u16> for Value<'_> {
660 #[inline]
661 fn from(value: u16) -> Self {
662 Value::Integer(Integer::Unsigned(u128::from(value)))
663 }
664}
665
666impl From<i16> for Value<'_> {
667 #[inline]
668 fn from(value: i16) -> Self {
669 Value::Integer(Integer::Signed(i128::from(value)))
670 }
671}
672
673impl From<u32> for Value<'_> {
674 #[inline]
675 fn from(value: u32) -> Self {
676 Value::Integer(Integer::Unsigned(u128::from(value)))
677 }
678}
679
680impl From<i32> for Value<'_> {
681 #[inline]
682 fn from(value: i32) -> Self {
683 Value::Integer(Integer::Signed(i128::from(value)))
684 }
685}
686
687impl From<u64> for Value<'_> {
688 #[inline]
689 fn from(value: u64) -> Self {
690 Value::Integer(Integer::Unsigned(u128::from(value)))
691 }
692}
693
694impl From<i64> for Value<'_> {
695 #[inline]
696 fn from(value: i64) -> Self {
697 Value::Integer(Integer::Signed(i128::from(value)))
698 }
699}
700
701impl From<usize> for Value<'_> {
702 #[inline]
703 fn from(value: usize) -> Self {
704 Value::Integer(Integer::Unsigned(value as u128))
705 }
706}
707
708impl From<isize> for Value<'_> {
709 #[inline]
710 fn from(value: isize) -> Self {
711 Value::Integer(Integer::Signed(value as i128))
712 }
713}
714
715impl From<u128> for Value<'_> {
716 #[inline]
717 fn from(value: u128) -> Self {
718 Value::Integer(Integer::Unsigned(value))
719 }
720}
721
722impl From<i128> for Value<'_> {
723 #[inline]
724 fn from(value: i128) -> Self {
725 Value::Integer(Integer::Signed(value))
726 }
727}
728
729impl From<f32> for Value<'_> {
730 #[inline]
731 fn from(value: f32) -> Self {
732 Value::Float(Float::F32(value))
733 }
734}
735
736impl From<f64> for Value<'_> {
737 #[inline]
738 fn from(value: f64) -> Self {
739 Value::Float(Float::F64(value))
740 }
741}
742
743impl<'a> From<&'a [u8]> for Value<'a> {
744 #[inline]
745 fn from(value: &'a [u8]) -> Self {
746 Value::Bytes(Cow::Borrowed(value))
747 }
748}
749
750impl From<Vec<u8>> for Value<'_> {
751 #[inline]
752 fn from(value: Vec<u8>) -> Self {
753 Value::Bytes(Cow::Owned(value))
754 }
755}
756
757impl<'a> From<Cow<'a, [u8]>> for Value<'a> {
758 #[inline]
759 fn from(value: Cow<'a, [u8]>) -> Self {
760 Value::Bytes(value)
761 }
762}
763
764impl<'a> From<&'a str> for Value<'a> {
765 #[inline]
766 fn from(value: &'a str) -> Self {
767 Value::String(Cow::Borrowed(value))
768 }
769}
770
771impl From<String> for Value<'_> {
772 #[inline]
773 fn from(value: String) -> Self {
774 Value::String(Cow::Owned(value))
775 }
776}
777
778impl<'a> From<Cow<'a, str>> for Value<'a> {
779 #[inline]
780 fn from(value: Cow<'a, str>) -> Self {
781 Value::String(value)
782 }
783}
784
785impl<'a> From<VecDeque<Value<'a>>> for Value<'a> {
786 #[inline]
787 fn from(value: VecDeque<Value<'a>>) -> Self {
788 Value::Array(value)
789 }
790}
791
792impl<'a> From<VecDeque<(Value<'a>, Value<'a>)>> for Value<'a> {
793 #[inline]
794 fn from(value: VecDeque<(Value<'a>, Value<'a>)>) -> Self {
795 Value::Map(value)
796 }
797}
798
799impl<'a, T> FromIterator<T> for Value<'a>
800where
801 T: Into<Value<'a>>,
802{
803 #[inline]
804 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
805 Self::from(iter.into_iter().map(Into::into).collect::<VecDeque<_>>())
806 }
807}
808
809impl<'a, K, V> FromIterator<(K, V)> for Value<'a>
810where
811 K: Into<Value<'a>>,
812 V: Into<Value<'a>>,
813{
814 #[inline]
815 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
816 Self::from(iter.into_iter().map(|(k, v)| (k.into(), v.into())).collect::<VecDeque<_>>())
817 }
818}
819
820trait AllIteratorTrait<Item>:
822 Iterator<Item = Item> + ExactSizeIterator + DoubleEndedIterator + ::core::fmt::Debug
823{
824}
825impl<I, T> AllIteratorTrait<T> for I where
826 I: Iterator<Item = T> + ExactSizeIterator + DoubleEndedIterator + ::core::fmt::Debug
827{
828}
829
830#[derive(Debug)]
832pub struct Iter<T>(Box<dyn AllIteratorTrait<T> + Send + Sync>);
833
834impl<T> Iter<T> {
835 fn new<I>(iter: I) -> Self
837 where
838 I: AllIteratorTrait<T> + Send + Sync + 'static,
839 {
840 Self(Box::new(iter))
841 }
842}
843
844impl<T> Iterator for Iter<T> {
845 type Item = T;
846
847 fn next(&mut self) -> Option<Self::Item> {
848 self.0.next()
849 }
850
851 fn size_hint(&self) -> (usize, Option<usize>) {
852 self.0.size_hint()
853 }
854
855 fn count(self) -> usize
856 where
857 Self: Sized,
858 {
859 self.0.count()
860 }
861
862 fn last(self) -> Option<Self::Item>
863 where
864 Self: Sized,
865 {
866 self.0.last()
867 }
868
869 fn nth(&mut self, n: usize) -> Option<Self::Item> {
870 self.0.nth(n)
871 }
872}
873
874impl<T> ExactSizeIterator for Iter<T> {
875 fn len(&self) -> usize {
876 self.0.len()
877 }
878}
879
880impl<T> DoubleEndedIterator for Iter<T> {
881 fn next_back(&mut self) -> Option<Self::Item> {
882 self.0.next_back()
883 }
884
885 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
886 self.0.nth_back(n)
887 }
888}
889
890
891#[cfg(test)]
892mod tests;