|
| 1 | +// Winnow integration for anybytes |
| 2 | + |
| 3 | +use crate::Bytes; |
| 4 | +use std::num::NonZeroUsize; |
| 5 | +use winnow::error::{ErrMode, Needed, ParserError}; |
| 6 | +use winnow::stream::{ |
| 7 | + AsBytes, Compare, CompareResult, FindSlice, Offset, SliceLen, Stream, StreamIsPartial, |
| 8 | + UpdateSlice, |
| 9 | +}; |
| 10 | + |
| 11 | +#[cfg(feature = "zerocopy")] |
| 12 | +use crate::view::View; |
| 13 | +#[cfg(feature = "zerocopy")] |
| 14 | +use zerocopy::{Immutable, KnownLayout, TryFromBytes}; |
| 15 | + |
| 16 | +/// Checkpoint for [`Bytes`] parsing with winnow. |
| 17 | +#[derive(Clone, Debug)] |
| 18 | +pub struct BytesCheckpoint(Bytes); |
| 19 | + |
| 20 | +/// Iterator yielding `(offset, byte)` pairs for [`Bytes`]. |
| 21 | +#[derive(Clone, Debug)] |
| 22 | +pub struct BytesIterOffsets { |
| 23 | + bytes: Bytes, |
| 24 | + offset: usize, |
| 25 | +} |
| 26 | + |
| 27 | +impl Iterator for BytesIterOffsets { |
| 28 | + type Item = (usize, u8); |
| 29 | + |
| 30 | + fn next(&mut self) -> Option<Self::Item> { |
| 31 | + let token = self.bytes.pop_front()?; |
| 32 | + let offset = self.offset; |
| 33 | + self.offset += 1; |
| 34 | + Some((offset, token)) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl SliceLen for Bytes { |
| 39 | + #[inline(always)] |
| 40 | + fn slice_len(&self) -> usize { |
| 41 | + self.as_slice().len() |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl Stream for Bytes { |
| 46 | + type Token = u8; |
| 47 | + type Slice = Bytes; |
| 48 | + |
| 49 | + type IterOffsets = BytesIterOffsets; |
| 50 | + |
| 51 | + type Checkpoint = BytesCheckpoint; |
| 52 | + |
| 53 | + #[inline(always)] |
| 54 | + fn iter_offsets(&self) -> Self::IterOffsets { |
| 55 | + BytesIterOffsets { |
| 56 | + bytes: self.clone(), |
| 57 | + offset: 0, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + #[inline(always)] |
| 62 | + fn eof_offset(&self) -> usize { |
| 63 | + self.as_slice().len() |
| 64 | + } |
| 65 | + |
| 66 | + #[inline(always)] |
| 67 | + fn next_token(&mut self) -> Option<Self::Token> { |
| 68 | + self.pop_front() |
| 69 | + } |
| 70 | + |
| 71 | + #[inline(always)] |
| 72 | + fn peek_token(&self) -> Option<Self::Token> { |
| 73 | + self.as_slice().first().copied() |
| 74 | + } |
| 75 | + |
| 76 | + #[inline(always)] |
| 77 | + fn offset_for<P>(&self, predicate: P) -> Option<usize> |
| 78 | + where |
| 79 | + P: Fn(Self::Token) -> bool, |
| 80 | + { |
| 81 | + self.as_slice().iter().position(|b| predicate(*b)) |
| 82 | + } |
| 83 | + |
| 84 | + #[inline(always)] |
| 85 | + fn offset_at(&self, tokens: usize) -> Result<usize, Needed> { |
| 86 | + let remaining = self.as_slice().len(); |
| 87 | + if let Some(needed) = tokens.checked_sub(remaining).and_then(NonZeroUsize::new) { |
| 88 | + Err(Needed::Size(needed)) |
| 89 | + } else { |
| 90 | + Ok(tokens) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + #[inline(always)] |
| 95 | + fn next_slice(&mut self, offset: usize) -> Self::Slice { |
| 96 | + self.take_prefix(offset).expect("offset within bounds") |
| 97 | + } |
| 98 | + |
| 99 | + #[inline(always)] |
| 100 | + fn peek_slice(&self, offset: usize) -> Self::Slice { |
| 101 | + self.slice(..offset) |
| 102 | + } |
| 103 | + |
| 104 | + #[inline(always)] |
| 105 | + fn checkpoint(&self) -> Self::Checkpoint { |
| 106 | + BytesCheckpoint(self.clone()) |
| 107 | + } |
| 108 | + |
| 109 | + #[inline(always)] |
| 110 | + fn reset(&mut self, checkpoint: &Self::Checkpoint) { |
| 111 | + *self = checkpoint.0.clone(); |
| 112 | + } |
| 113 | + |
| 114 | + #[allow(deprecated)] |
| 115 | + #[inline(always)] |
| 116 | + fn raw(&self) -> &dyn core::fmt::Debug { |
| 117 | + self |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl StreamIsPartial for Bytes { |
| 122 | + type PartialState = (); |
| 123 | + |
| 124 | + #[inline] |
| 125 | + fn complete(&mut self) -> Self::PartialState {} |
| 126 | + |
| 127 | + #[inline] |
| 128 | + fn restore_partial(&mut self, _state: Self::PartialState) {} |
| 129 | + |
| 130 | + #[inline(always)] |
| 131 | + fn is_partial_supported() -> bool { |
| 132 | + false |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl Offset for Bytes { |
| 137 | + #[inline(always)] |
| 138 | + fn offset_from(&self, start: &Self) -> usize { |
| 139 | + let self_ptr = self.as_slice().as_ptr() as usize; |
| 140 | + let start_ptr = start.as_slice().as_ptr() as usize; |
| 141 | + self_ptr - start_ptr |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl Offset<BytesCheckpoint> for Bytes { |
| 146 | + #[inline(always)] |
| 147 | + fn offset_from(&self, other: &BytesCheckpoint) -> usize { |
| 148 | + self.offset_from(&other.0) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl Offset for BytesCheckpoint { |
| 153 | + #[inline(always)] |
| 154 | + fn offset_from(&self, start: &Self) -> usize { |
| 155 | + self.0.offset_from(&start.0) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +impl AsBytes for Bytes { |
| 160 | + #[inline(always)] |
| 161 | + fn as_bytes(&self) -> &[u8] { |
| 162 | + self.as_slice() |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl<T> Compare<T> for Bytes |
| 167 | +where |
| 168 | + for<'a> &'a [u8]: Compare<T>, |
| 169 | +{ |
| 170 | + #[inline(always)] |
| 171 | + fn compare(&self, t: T) -> CompareResult { |
| 172 | + self.as_slice().compare(t) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +impl<S> FindSlice<S> for Bytes |
| 177 | +where |
| 178 | + for<'a> &'a [u8]: FindSlice<S>, |
| 179 | +{ |
| 180 | + #[inline(always)] |
| 181 | + fn find_slice(&self, substr: S) -> Option<core::ops::Range<usize>> { |
| 182 | + self.as_slice().find_slice(substr) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +impl UpdateSlice for Bytes { |
| 187 | + #[inline(always)] |
| 188 | + fn update_slice(self, inner: Self::Slice) -> Self { |
| 189 | + inner |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +#[cfg(feature = "zerocopy")] |
| 194 | +/// Parse a `View` of `T` from the beginning of the input. |
| 195 | +pub fn view<T, E>(input: &mut Bytes) -> Result<View<T>, ErrMode<E>> |
| 196 | +where |
| 197 | + T: ?Sized + TryFromBytes + KnownLayout + Immutable, |
| 198 | + E: ParserError<Bytes>, |
| 199 | +{ |
| 200 | + input |
| 201 | + .view_prefix::<T>() |
| 202 | + .map_err(|_| ErrMode::Backtrack(E::from_input(input))) |
| 203 | +} |
| 204 | + |
| 205 | +#[cfg(feature = "zerocopy")] |
| 206 | +/// Return a parser producing a slice-like `View` with `count` elements. |
| 207 | +pub fn view_elems<T, E>(count: usize) -> impl winnow::Parser<Bytes, View<T>, ErrMode<E>> |
| 208 | +where |
| 209 | + T: ?Sized + TryFromBytes + KnownLayout<PointerMetadata = usize> + Immutable, |
| 210 | + E: ParserError<Bytes>, |
| 211 | +{ |
| 212 | + move |input: &mut Bytes| { |
| 213 | + input |
| 214 | + .view_prefix_with_elems::<T>(count) |
| 215 | + .map_err(|_| ErrMode::Backtrack(E::from_input(input))) |
| 216 | + } |
| 217 | +} |
0 commit comments