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

shnewto / bnf / 4568677051

pending completion
4568677051

push

github

Jeffrey Crocker
LLVM coverage reporting

2125 of 2200 relevant lines covered (96.59%)

53550.13 hits per line

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

81.82
/src/append_vec.rs
1
/// Create a new id type for an [`AppendOnlyVec`], which will be a wrapped [`usize`].
2
/// Example usage: `append_only_vec_id!(pub(crate) ProductionId)`;
3
macro_rules! append_only_vec_id {
4
    ($visible:vis $id:ident) => {
5
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
105,018✔
6
        $visible struct $id(usize);
7

8
        impl From<usize> for $id {
9
            fn from(id: usize) -> Self {
31,128✔
10
                Self(id)
31,128✔
11
            }
31,128✔
12
        }
13

14
        impl From<$id> for usize {
15
            fn from(id: $id) -> Self {
240,110✔
16
                id.0
240,110✔
17
            }
240,110✔
18
        }
19
    };
20
}
21

22
pub(crate) use append_only_vec_id;
23

24
/// Vector type which does *not* allow item removal during lifetime.
25
/// Useful for data structures with complex, shared ownership, such as graphs.
26
#[derive(Debug, Clone)]
×
27
pub(crate) struct AppendOnlyVec<T, I> {
28
    vec: Vec<T>,
29
    id_type: std::marker::PhantomData<I>,
30
}
31

32
impl<T, I> AppendOnlyVec<T, I>
33
where
34
    I: From<usize> + Into<usize>,
35
{
36
    pub fn new() -> Self {
1,035✔
37
        Self::default()
1,035✔
38
    }
1,035✔
39
    pub fn len(&self) -> usize {
31,128✔
40
        self.vec.len()
31,128✔
41
    }
31,128✔
42
    fn next_id(&self) -> I {
31,128✔
43
        I::from(self.len())
31,128✔
44
    }
31,128✔
45
    pub fn push(&mut self, item: T) -> I {
15,564✔
46
        let id = self.next_id();
15,564✔
47
        self.vec.push(item);
15,564✔
48
        id
15,564✔
49
    }
15,564✔
50
    pub fn push_with_id<F>(&mut self, build: F) -> &T
15,564✔
51
    where
15,564✔
52
        F: Fn(I) -> T,
15,564✔
53
    {
15,564✔
54
        let id = self.next_id();
15,564✔
55
        let item = build(id);
15,564✔
56
        let id = self.push(item);
15,564✔
57
        self.get(id).expect("failed to get appended item")
15,564✔
58
    }
15,564✔
59
    pub fn get(&self, id: I) -> Option<&T> {
240,110✔
60
        self.vec.get::<usize>(id.into())
240,110✔
61
    }
240,110✔
62
    #[cfg(test)]
63
    pub fn iter(&self) -> impl Iterator<Item = &T> {
5✔
64
        self.vec.iter()
5✔
65
    }
5✔
66
}
67

68
impl<T, K> Default for AppendOnlyVec<T, K> {
69
    fn default() -> Self {
2,070✔
70
        Self::from(vec![])
2,070✔
71
    }
2,070✔
72
}
73

74
impl<T, K> From<Vec<T>> for AppendOnlyVec<T, K> {
75
    fn from(vec: Vec<T>) -> Self {
2,070✔
76
        Self {
2,070✔
77
            vec,
2,070✔
78
            id_type: std::marker::PhantomData,
2,070✔
79
        }
2,070✔
80
    }
2,070✔
81
}
82

83
impl<T, K> IntoIterator for AppendOnlyVec<T, K> {
84
    type Item = <Vec<T> as IntoIterator>::Item;
85
    type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
86
    fn into_iter(self) -> Self::IntoIter {
×
87
        self.vec.into_iter()
×
88
    }
×
89
}
90

91
impl<'a, T, K> IntoIterator for &'a AppendOnlyVec<T, K> {
92
    type Item = &'a T;
93
    type IntoIter = std::slice::Iter<'a, T>;
94
    fn into_iter(self) -> Self::IntoIter {
×
95
        self.vec.iter()
×
96
    }
×
97
}
98

99
impl<'a, T, K> IntoIterator for &'a mut AppendOnlyVec<T, K> {
100
    type Item = &'a mut T;
101
    type IntoIter = std::slice::IterMut<'a, T>;
102
    fn into_iter(self) -> Self::IntoIter {
×
103
        self.vec.iter_mut()
×
104
    }
×
105
}
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