• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adierking / unplug / 15960713325

29 Jun 2025 11:47PM UTC coverage: 77.134%. First build
15960713325

push

github

adierking
hsd: Display list parsing

0 of 206 new or added lines in 5 files covered. (0.0%)

20344 of 26375 relevant lines covered (77.13%)

1083190.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/unplug/src/hsd/array.rs
1
use super::pointer::DefaultIn;
2
use super::pointer::{NodeBase, ReadPointerBase};
3
use super::{Error, Node, Pointer, ReadPointer, Result};
4
use crate::common::ReadFrom;
5
use bumpalo::collections::vec::IntoIter;
6
use bumpalo::collections::Vec;
7
use bumpalo::Bump;
8
use std::ops::{Deref, DerefMut};
9

10
/// Trait for values that can be used in arrays.
11
/// Arrays (always?) end in a sentinel value, and the type needs to specify how to detect that.
12
pub trait ArrayElement {
13
    fn is_end_of_array(&self) -> bool;
14
}
15

16
impl<'a, T: Node<'a>> ArrayElement for Pointer<'a, T> {
17
    fn is_end_of_array(&self) -> bool {
×
18
        self.is_null()
×
19
    }
×
20
}
21

22
/// An array of pointers to nodes.
23
pub type PointerArray<'a, T> = Array<'a, Pointer<'a, T>>;
24

25
/// An array of values terminated by a sentinel value (e.g. null pointer).
26
/// Typically this needs to be contained in a pointer itself (TODO: make this easier).
27
#[derive(Clone, PartialEq, Eq)]
×
28
pub struct Array<'a, T: Node<'a> + ArrayElement> {
29
    elements: Vec<'a, T>,
30
}
31

32
impl<'a, T: Node<'a> + ArrayElement + std::fmt::Debug> std::fmt::Debug for Array<'a, T> {
33
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
34
        f.debug_list().entries(&self.elements).finish()
×
35
    }
×
36
}
37

38
impl<'a, T: Node<'a> + ArrayElement> Array<'a, T> {
39
    pub fn new_in(arena: &'a Bump) -> Self {
×
40
        Self { elements: Vec::new_in(arena) }
×
41
    }
×
42
}
43

44
impl<'a, T: Node<'a> + ArrayElement> DefaultIn<'a> for Array<'a, T> {
45
    fn default_in(arena: &'a Bump) -> Self {
×
46
        Self::new_in(arena)
×
47
    }
×
48
}
49

50
impl<'a, T: Node<'a> + ArrayElement> Deref for Array<'a, T> {
51
    type Target = [T];
52
    fn deref(&self) -> &Self::Target {
×
53
        self.elements.deref()
×
54
    }
×
55
}
56

57
impl<'a, T: Node<'a> + ArrayElement> DerefMut for Array<'a, T> {
58
    fn deref_mut(&mut self) -> &mut Self::Target {
×
59
        self.elements.deref_mut()
×
60
    }
×
61
}
62

63
impl<'a, T: Node<'a> + ArrayElement> IntoIterator for Array<'a, T> {
64
    type Item = T;
65
    type IntoIter = IntoIter<'a, T>;
66

67
    fn into_iter(self) -> Self::IntoIter {
×
68
        self.elements.into_iter()
×
69
    }
×
70
}
71

72
impl<'a, R, T> ReadFrom<R> for Array<'a, T>
73
where
74
    R: ReadPointer<'a> + ?Sized,
75
    T: Node<'a> + ArrayElement + ReadFrom<R, Error = Error>,
76
{
77
    type Error = Error;
78
    fn read_from(reader: &mut R) -> Result<Self> {
×
79
        let mut elements = Vec::new_in(reader.arena());
×
80
        loop {
81
            let value = T::read_from(reader)?;
×
82
            if value.is_end_of_array() {
×
83
                break;
×
84
            }
×
85
            elements.push(value);
×
86
        }
87
        Ok(Self { elements })
×
88
    }
×
89
}
90

91
impl<'a, T> Node<'a> for Array<'a, T> where
92
    for<'x> T: Node<'a> + ArrayElement + ReadFrom<dyn ReadPointerBase<'a> + 'x, Error = Error> + 'a
93
{
94
}
95

96
/// An immutable array of bytes.
97
///
98
/// This is not compatible with `read_pointer()`. Use `ByteArray::read_pointer()` to both read and
99
/// specify a size.
NEW
100
#[derive(Copy, Clone)]
×
101
pub struct ByteArray<'a> {
102
    arena: &'a Bump,
103
    bytes: &'a [u8],
104
    max_len: usize,
105
}
106

107
impl<'a> ByteArray<'a> {
108
    /// Pass this to `read_pointer()` to guess the size of the buffer based on adjacent nodes.
109
    /// Only use this if there is no way to know the buffer size ahead of time.
110
    pub const UNKNOWN_LENGTH: usize = usize::MAX;
111

112
    /// Create an empty byte array.
NEW
113
    pub fn new_in(arena: &'a Bump) -> Self {
×
NEW
114
        Self { arena, bytes: &[], max_len: 0 }
×
NEW
115
    }
×
116

117
    /// Create an empty byte array with a max length set.
NEW
118
    pub fn with_max_len_in(arena: &'a Bump, max_len: usize) -> Self {
×
NEW
119
        Self { arena, bytes: &[], max_len }
×
NEW
120
    }
×
121

122
    /// Create a byte array from a span of bytes.
NEW
123
    pub fn with_bytes_in(arena: &'a Bump, bytes: &[u8]) -> Self {
×
NEW
124
        Self { arena, bytes: arena.alloc_slice_copy(bytes), max_len: bytes.len() }
×
NEW
125
    }
×
126

127
    /// Read a pointer to a buffer with the given maximum size.
128
    /// If the size is unknown, use `UNKNOWN_LENGTH` to guess the size.
NEW
129
    pub fn read_pointer<R>(reader: &mut R, max_len: usize) -> Result<Pointer<'a, Self>>
×
NEW
130
    where
×
NEW
131
        R: ReadPointer<'a> + ?Sized,
×
NEW
132
    {
×
NEW
133
        reader.read_pointer_into(Self::with_max_len_in(reader.arena(), max_len))
×
NEW
134
    }
×
135

136
    /// Return a slice over the bytes in the array.
NEW
137
    pub fn as_slice(&self) -> &'a [u8] {
×
NEW
138
        self.bytes
×
NEW
139
    }
×
140
}
141

142
impl<'a> std::fmt::Debug for ByteArray<'a> {
NEW
143
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
NEW
144
        f.debug_struct("ByteArray").field("len", &self.bytes.len()).finish()
×
NEW
145
    }
×
146
}
147

148
impl Deref for ByteArray<'_> {
149
    type Target = [u8];
NEW
150
    fn deref(&self) -> &Self::Target {
×
NEW
151
        self.as_slice()
×
NEW
152
    }
×
153
}
154

155
// Manual impl of NodeBase; we can't use ReadFrom because the buffer needs to have a size assigned.
156
impl<'a> NodeBase<'a> for ByteArray<'a> {
NEW
157
    fn read<'r>(&mut self, reader: &'r mut dyn ReadPointerBase<'a>, max_size: usize) -> Result<()> {
×
NEW
158
        let len = if self.max_len == Self::UNKNOWN_LENGTH { max_size } else { self.max_len };
×
NEW
159
        let bytes = self.arena.alloc_slice_fill_default(len);
×
NEW
160
        reader.read_exact(bytes)?;
×
NEW
161
        self.bytes = bytes;
×
NEW
162
        Ok(())
×
NEW
163
    }
×
164
}
165

166
impl<'a> Node<'a> for ByteArray<'a> {}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc