Coverage Report

Created: 2026-01-30 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sval-2.16.0/src/data/map.rs
Line
Count
Source
1
use crate::{std::fmt, Result, Stream, Value};
2
3
/**
4
An adapter that streams a slice of key-value pairs as a map.
5
 */
6
#[repr(transparent)]
7
pub struct MapSlice<K, V>([(K, V)]);
8
9
impl<K, V> MapSlice<K, V> {
10
    /**
11
    Treat a slice of key-value pairs as a map.
12
     */
13
0
    pub const fn new<'a>(map: &'a [(K, V)]) -> &'a Self {
14
        // SAFETY: `MapSlice` and `[(K, V)]` have the same ABI
15
0
        unsafe { &*(map as *const _ as *const MapSlice<K, V>) }
16
0
    }
17
18
    /**
19
    Get a reference to the underlying slice.
20
     */
21
0
    pub const fn as_slice(&self) -> &[(K, V)] {
22
0
        &self.0
23
0
    }
24
}
25
26
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for MapSlice<K, V> {
27
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28
0
        fmt::Debug::fmt(&self.0, f)
29
0
    }
30
}
31
32
impl<K, V> AsRef<[(K, V)]> for MapSlice<K, V> {
33
0
    fn as_ref(&self) -> &[(K, V)] {
34
0
        &self.0
35
0
    }
36
}
37
38
impl<K: Value, V: Value> Value for MapSlice<K, V> {
39
0
    fn stream<'sval, S: Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> Result {
40
0
        stream.map_begin(Some(self.0.len()))?;
41
42
0
        for (k, v) in self.0.iter() {
43
0
            stream.map_key_begin()?;
44
0
            stream.value(k)?;
45
0
            stream.map_key_end()?;
46
47
0
            stream.map_value_begin()?;
48
0
            stream.value(v)?;
49
0
            stream.map_value_end()?;
50
        }
51
52
0
        stream.map_end()
53
0
    }
54
}
55
56
#[cfg(feature = "alloc")]
57
mod alloc_support {
58
    use super::*;
59
    use crate::std::collections::BTreeMap;
60
61
    impl<K: Value, V: Value> Value for BTreeMap<K, V> {
62
0
        fn stream<'sval, S: Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> Result {
63
0
            stream.map_begin(Some(self.len()))?;
64
65
0
            for (k, v) in self {
66
0
                stream.map_key_begin()?;
67
0
                stream.value(k)?;
68
0
                stream.map_key_end()?;
69
70
0
                stream.map_value_begin()?;
71
0
                stream.value(v)?;
72
0
                stream.map_value_end()?;
73
            }
74
75
0
            stream.map_end()
76
0
        }
77
    }
78
}
79
80
#[cfg(feature = "std")]
81
mod std_support {
82
    use super::*;
83
    use crate::std::{collections::HashMap, hash::BuildHasher};
84
85
    impl<K: Value, V: Value, H: BuildHasher> Value for HashMap<K, V, H> {
86
0
        fn stream<'sval, S: Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> Result {
87
0
            stream.map_begin(Some(self.len()))?;
88
89
0
            for (k, v) in self {
90
0
                stream.map_key_begin()?;
91
0
                stream.value(k)?;
92
0
                stream.map_key_end()?;
93
94
0
                stream.map_value_begin()?;
95
0
                stream.value(v)?;
96
0
                stream.map_value_end()?;
97
            }
98
99
0
            stream.map_end()
100
0
        }
101
    }
102
}