Coverage Report

Created: 2025-08-26 07:09

/src/h2/src/proto/streams/buffer.rs
Line
Count
Source (jump to first uncovered line)
1
use slab::Slab;
2
3
/// Buffers frames for multiple streams.
4
#[derive(Debug)]
5
pub struct Buffer<T> {
6
    slab: Slab<Slot<T>>,
7
}
8
9
/// A sequence of frames in a `Buffer`
10
#[derive(Debug)]
11
pub struct Deque {
12
    indices: Option<Indices>,
13
}
14
15
/// Tracks the head & tail for a sequence of frames in a `Buffer`.
16
#[derive(Debug, Default, Copy, Clone)]
17
struct Indices {
18
    head: usize,
19
    tail: usize,
20
}
21
22
#[derive(Debug)]
23
struct Slot<T> {
24
    value: T,
25
    next: Option<usize>,
26
}
27
28
impl<T> Buffer<T> {
29
25.5k
    pub fn new() -> Self {
30
25.5k
        Buffer { slab: Slab::new() }
31
25.5k
    }
<h2::proto::streams::buffer::Buffer<h2::proto::streams::recv::Event>>::new
Line
Count
Source
29
12.7k
    pub fn new() -> Self {
30
12.7k
        Buffer { slab: Slab::new() }
31
12.7k
    }
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::new
Line
Count
Source
29
445
    pub fn new() -> Self {
30
445
        Buffer { slab: Slab::new() }
31
445
    }
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::new
Line
Count
Source
29
12.3k
    pub fn new() -> Self {
30
12.3k
        Buffer { slab: Slab::new() }
31
12.3k
    }
32
33
1.07k
    pub fn is_empty(&self) -> bool {
34
1.07k
        self.slab.is_empty()
35
1.07k
    }
Unexecuted instantiation: <h2::proto::streams::buffer::Buffer<_>>::is_empty
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::is_empty
Line
Count
Source
33
1.07k
    pub fn is_empty(&self) -> bool {
34
1.07k
        self.slab.is_empty()
35
1.07k
    }
36
}
37
38
impl Deque {
39
901k
    pub fn new() -> Self {
40
901k
        Deque { indices: None }
41
901k
    }
42
43
3.71M
    pub fn is_empty(&self) -> bool {
44
3.71M
        self.indices.is_none()
45
3.71M
    }
46
47
880k
    pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) {
48
880k
        let key = buf.slab.insert(Slot { value, next: None });
49
880k
50
880k
        match self.indices {
51
428k
            Some(ref mut idxs) => {
52
428k
                buf.slab[idxs.tail].next = Some(key);
53
428k
                idxs.tail = key;
54
428k
            }
55
452k
            None => {
56
452k
                self.indices = Some(Indices {
57
452k
                    head: key,
58
452k
                    tail: key,
59
452k
                });
60
452k
            }
61
        }
62
880k
    }
<h2::proto::streams::buffer::Deque>::push_back::<h2::proto::streams::recv::Event>
Line
Count
Source
47
4.22k
    pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) {
48
4.22k
        let key = buf.slab.insert(Slot { value, next: None });
49
4.22k
50
4.22k
        match self.indices {
51
2.56k
            Some(ref mut idxs) => {
52
2.56k
                buf.slab[idxs.tail].next = Some(key);
53
2.56k
                idxs.tail = key;
54
2.56k
            }
55
1.66k
            None => {
56
1.66k
                self.indices = Some(Indices {
57
1.66k
                    head: key,
58
1.66k
                    tail: key,
59
1.66k
                });
60
1.66k
            }
61
        }
62
4.22k
    }
<h2::proto::streams::buffer::Deque>::push_back::<h2::frame::Frame>
Line
Count
Source
47
357
    pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) {
48
357
        let key = buf.slab.insert(Slot { value, next: None });
49
357
50
357
        match self.indices {
51
0
            Some(ref mut idxs) => {
52
0
                buf.slab[idxs.tail].next = Some(key);
53
0
                idxs.tail = key;
54
0
            }
55
357
            None => {
56
357
                self.indices = Some(Indices {
57
357
                    head: key,
58
357
                    tail: key,
59
357
                });
60
357
            }
61
        }
62
357
    }
<h2::proto::streams::buffer::Deque>::push_back::<h2::frame::Frame>
Line
Count
Source
47
876k
    pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) {
48
876k
        let key = buf.slab.insert(Slot { value, next: None });
49
876k
50
876k
        match self.indices {
51
425k
            Some(ref mut idxs) => {
52
425k
                buf.slab[idxs.tail].next = Some(key);
53
425k
                idxs.tail = key;
54
425k
            }
55
450k
            None => {
56
450k
                self.indices = Some(Indices {
57
450k
                    head: key,
58
450k
                    tail: key,
59
450k
                });
60
450k
            }
61
        }
62
876k
    }
63
64
44.5k
    pub fn push_front<T>(&mut self, buf: &mut Buffer<T>, value: T) {
65
44.5k
        let key = buf.slab.insert(Slot { value, next: None });
66
44.5k
67
44.5k
        match self.indices {
68
0
            Some(ref mut idxs) => {
69
0
                buf.slab[key].next = Some(idxs.head);
70
0
                idxs.head = key;
71
0
            }
72
44.5k
            None => {
73
44.5k
                self.indices = Some(Indices {
74
44.5k
                    head: key,
75
44.5k
                    tail: key,
76
44.5k
                });
77
44.5k
            }
78
        }
79
44.5k
    }
Unexecuted instantiation: <h2::proto::streams::buffer::Deque>::push_front::<h2::proto::streams::recv::Event>
<h2::proto::streams::buffer::Deque>::push_front::<h2::frame::Frame>
Line
Count
Source
64
44.5k
    pub fn push_front<T>(&mut self, buf: &mut Buffer<T>, value: T) {
65
44.5k
        let key = buf.slab.insert(Slot { value, next: None });
66
44.5k
67
44.5k
        match self.indices {
68
0
            Some(ref mut idxs) => {
69
0
                buf.slab[key].next = Some(idxs.head);
70
0
                idxs.head = key;
71
0
            }
72
44.5k
            None => {
73
44.5k
                self.indices = Some(Indices {
74
44.5k
                    head: key,
75
44.5k
                    tail: key,
76
44.5k
                });
77
44.5k
            }
78
        }
79
44.5k
    }
80
81
1.96M
    pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> {
82
1.96M
        match self.indices {
83
924k
            Some(mut idxs) => {
84
924k
                let mut slot = buf.slab.remove(idxs.head);
85
924k
86
924k
                if idxs.head == idxs.tail {
87
496k
                    assert!(slot.next.is_none());
88
496k
                    self.indices = None;
89
428k
                } else {
90
428k
                    idxs.head = slot.next.take().unwrap();
91
428k
                    self.indices = Some(idxs);
92
428k
                }
93
94
924k
                Some(slot.value)
95
            }
96
1.03M
            None => None,
97
        }
98
1.96M
    }
<h2::proto::streams::buffer::Deque>::pop_front::<h2::proto::streams::recv::Event>
Line
Count
Source
81
456k
    pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> {
82
456k
        match self.indices {
83
3.15k
            Some(mut idxs) => {
84
3.15k
                let mut slot = buf.slab.remove(idxs.head);
85
3.15k
86
3.15k
                if idxs.head == idxs.tail {
87
887
                    assert!(slot.next.is_none());
88
887
                    self.indices = None;
89
2.26k
                } else {
90
2.26k
                    idxs.head = slot.next.take().unwrap();
91
2.26k
                    self.indices = Some(idxs);
92
2.26k
                }
93
94
3.15k
                Some(slot.value)
95
            }
96
453k
            None => None,
97
        }
98
456k
    }
<h2::proto::streams::buffer::Deque>::pop_front::<h2::frame::Frame>
Line
Count
Source
81
714
    pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> {
82
714
        match self.indices {
83
357
            Some(mut idxs) => {
84
357
                let mut slot = buf.slab.remove(idxs.head);
85
357
86
357
                if idxs.head == idxs.tail {
87
357
                    assert!(slot.next.is_none());
88
357
                    self.indices = None;
89
0
                } else {
90
0
                    idxs.head = slot.next.take().unwrap();
91
0
                    self.indices = Some(idxs);
92
0
                }
93
94
357
                Some(slot.value)
95
            }
96
357
            None => None,
97
        }
98
714
    }
<h2::proto::streams::buffer::Deque>::pop_front::<h2::frame::Frame>
Line
Count
Source
81
1.50M
    pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> {
82
1.50M
        match self.indices {
83
920k
            Some(mut idxs) => {
84
920k
                let mut slot = buf.slab.remove(idxs.head);
85
920k
86
920k
                if idxs.head == idxs.tail {
87
495k
                    assert!(slot.next.is_none());
88
495k
                    self.indices = None;
89
425k
                } else {
90
425k
                    idxs.head = slot.next.take().unwrap();
91
425k
                    self.indices = Some(idxs);
92
425k
                }
93
94
920k
                Some(slot.value)
95
            }
96
585k
            None => None,
97
        }
98
1.50M
    }
99
}