forked from seanmonstar/httparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiter.rs
More file actions
157 lines (136 loc) · 3.69 KB
/
iter.rs
File metadata and controls
157 lines (136 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use core::convert::TryInto;
use core::convert::TryFrom;
#[allow(missing_docs)]
pub struct Bytes<'a> {
start: *const u8,
end: *const u8,
cursor: *const u8,
phantom: core::marker::PhantomData<&'a ()>,
}
#[allow(missing_docs)]
impl<'a> Bytes<'a> {
#[inline]
pub fn new(slice: &'a [u8]) -> Bytes<'a> {
let start = slice.as_ptr();
let end = unsafe { start.add(slice.len()) };
let cursor = start;
Bytes {
start,
end,
cursor,
phantom: core::marker::PhantomData,
}
}
#[inline]
pub fn pos(&self) -> usize {
self.cursor as usize - self.start as usize
}
#[inline]
pub fn peek(&self) -> Option<u8> {
if self.cursor < self.end {
// SAFETY: bounds checked
Some(unsafe { *self.cursor })
} else {
None
}
}
#[inline]
pub fn peek_ahead(&self, n: usize) -> Option<u8> {
let ptr = unsafe { self.cursor.add(n) };
if ptr < self.end {
// SAFETY: bounds checked
Some(unsafe { *ptr })
} else {
None
}
}
#[inline]
pub fn peek_n<'b: 'a, U: TryFrom<&'a [u8]>>(&'b self, n: usize) -> Option<U> {
// TODO: once we bump MSRC, use const generics to allow only [u8; N] reads
// TODO: drop `n` arg in favour of const
// let n = core::mem::size_of::<U>();
self.as_ref().get(..n)?.try_into().ok()
}
#[inline]
pub unsafe fn bump(&mut self) {
self.advance(1)
}
#[inline]
pub unsafe fn advance(&mut self, n: usize) {
self.cursor = self.cursor.add(n);
debug_assert!(self.cursor <= self.end, "overflow");
}
#[inline]
pub fn len(&self) -> usize {
self.end as usize - self.cursor as usize
}
#[inline]
pub fn slice(&mut self) -> &'a [u8] {
// not moving position at all, so it's safe
let slice = unsafe { slice_from_ptr_range(self.start, self.cursor) };
self.commit();
slice
}
// TODO: this is an anti-pattern, should be removed
#[inline]
pub unsafe fn slice_skip(&mut self, skip: usize) -> &'a [u8] {
debug_assert!(self.cursor.sub(skip) >= self.start);
let head = slice_from_ptr_range(self.start, self.cursor.sub(skip));
self.commit();
head
}
#[inline]
pub fn commit(&mut self) {
self.start = self.cursor
}
#[inline]
pub unsafe fn advance_and_commit(&mut self, n: usize) {
self.advance(n);
self.commit();
}
#[inline]
pub fn as_ptr(&self) -> *const u8 {
self.cursor
}
#[inline]
pub fn start(&self) -> *const u8 {
self.start
}
#[inline]
pub fn end(&self) -> *const u8 {
self.end
}
#[inline]
pub unsafe fn set_cursor(&mut self, ptr: *const u8) {
debug_assert!(ptr >= self.start);
debug_assert!(ptr <= self.end);
self.cursor = ptr;
}
}
impl<'a> AsRef<[u8]> for Bytes<'a> {
#[inline]
fn as_ref(&self) -> &[u8] {
unsafe { slice_from_ptr_range(self.cursor, self.end) }
}
}
#[inline]
unsafe fn slice_from_ptr_range<'a>(start: *const u8, end: *const u8) -> &'a [u8] {
debug_assert!(start <= end);
core::slice::from_raw_parts(start, end as usize - start as usize)
}
impl<'a> Iterator for Bytes<'a> {
type Item = u8;
#[inline]
fn next(&mut self) -> Option<u8> {
if self.cursor < self.end {
// SAFETY: bounds checked
unsafe {
let b = *self.cursor;
self.bump();
Some(b)
}
} else {
None
}
}
}