netgauze_locate/
lib.rs

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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#[cfg(test)]
mod tests;

use nom::{AsBytes, Compare, CompareResult, InputIter, InputLength, InputTake, Offset, Slice};
use std::ops::{RangeFrom, RangeTo};

/// Cloned from the crate `nom_locate` but with the omission of computing
/// the line & column number since we don't care about them in binary protocols,
/// and they do make using the `LocateSpan` slower.
#[derive(Debug, Clone, Copy)]
pub struct BinarySpan<T> {
    offset: usize,
    fragment: T,
}

impl<T> BinarySpan<T> {
    pub const fn new(buffer: T) -> Self {
        Self {
            offset: 0,
            fragment: buffer,
        }
    }

    /// Similar to `new_extra`, but allows overriding offset.
    /// # Safety
    /// This is unsafe, because giving an offset too large may result in
    /// undefined behavior, as some methods move back along the fragment
    /// assuming any negative index within the offset is valid.
    pub const unsafe fn new_from_raw_offset(offset: usize, fragment: T) -> Self {
        Self { offset, fragment }
    }

    pub const fn new_extra(program: T) -> BinarySpan<T> {
        BinarySpan {
            offset: 0,
            fragment: program,
        }
    }

    #[inline]
    pub const fn location_offset(&self) -> usize {
        self.offset
    }

    #[inline]
    pub const fn fragment(&self) -> &T {
        &self.fragment
    }
}

impl<T, R> Slice<R> for BinarySpan<T>
where
    T: Slice<R> + Offset + AsBytes + Slice<RangeTo<usize>>,
{
    #[inline]
    fn slice(&self, range: R) -> Self {
        let next_fragment = self.fragment.slice(range);
        let consumed_len = self.fragment.offset(&next_fragment);
        if consumed_len == 0 {
            return BinarySpan {
                offset: self.offset,
                fragment: next_fragment,
            };
        }

        BinarySpan {
            offset: self.offset + consumed_len,
            fragment: next_fragment,
        }
    }
}

impl<T> InputIter for BinarySpan<T>
where
    T: InputIter,
{
    type Item = T::Item;
    type Iter = T::Iter;
    type IterElem = T::IterElem;
    #[inline]
    fn iter_indices(&self) -> Self::Iter {
        self.fragment.iter_indices()
    }
    #[inline]
    fn iter_elements(&self) -> Self::IterElem {
        self.fragment.iter_elements()
    }
    #[inline]
    fn position<P>(&self, predicate: P) -> Option<usize>
    where
        P: Fn(Self::Item) -> bool,
    {
        self.fragment.position(predicate)
    }
    #[inline]
    fn slice_index(&self, count: usize) -> Result<usize, nom::Needed> {
        self.fragment.slice_index(count)
    }
}

impl<T: InputLength> InputLength for BinarySpan<T> {
    #[inline]
    fn input_len(&self) -> usize {
        self.fragment.input_len()
    }
}

impl<T> InputTake for BinarySpan<T>
where
    Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
{
    #[inline]
    fn take(&self, count: usize) -> Self {
        self.slice(..count)
    }

    #[inline]
    fn take_split(&self, count: usize) -> (Self, Self) {
        (self.slice(count..), self.slice(..count))
    }
}

impl<T> core::ops::Deref for BinarySpan<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.fragment
    }
}

impl<T, U> AsRef<U> for BinarySpan<&T>
where
    T: ?Sized + AsRef<U>,
    U: ?Sized,
{
    fn as_ref(&self) -> &U {
        self.fragment.as_ref()
    }
}

impl<T: AsBytes> AsBytes for BinarySpan<T> {
    #[inline]
    fn as_bytes(&self) -> &[u8] {
        self.fragment.as_bytes()
    }
}

impl<T: AsBytes + PartialEq> PartialEq for BinarySpan<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.offset == other.offset && self.fragment == other.fragment
    }
}

impl<T: AsBytes + Eq> Eq for BinarySpan<T> {}

impl<A: Compare<B>, B: Into<BinarySpan<B>>> Compare<B> for BinarySpan<A> {
    #[inline(always)]
    fn compare(&self, t: B) -> CompareResult {
        self.fragment.compare(t.into().fragment)
    }

    #[inline(always)]
    fn compare_no_case(&self, t: B) -> CompareResult {
        self.fragment.compare_no_case(t.into().fragment)
    }
}

impl<T: AsBytes> From<T> for BinarySpan<T> {
    #[inline]
    fn from(i: T) -> Self {
        Self::new_extra(i)
    }
}

#[test]
fn it_should_implement_as_ref_for_the_underlying_type() {
    fn function_accepting_u8_slice<B: AsRef<[u8]>>(_data: B) {}
    let bytes_data = BinarySpan::new(b"some binary data");
    function_accepting_u8_slice(bytes_data);
}