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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
use std::borrow::Cow;
use std::io::{BufRead, BufReader, Read};
use std::ops::Deref;
use crate::parse::{ParseError, ParseResult, Parser};
used_in_docs!(Parser);
/// A continuous chunk of data read from a [`ParseBuf`].
///
/// A `ParseBufChunk` has two variants:
/// - [`Temporary`] is for when the data being referenced is owned by the
/// [`ParseBuf`] instance itself. This cannot be kept around and will not be
/// kept in borrowed form while parsing.
/// - [`External`] is for when the data being referenced is borrowed from
/// elsewhere. This allows record parsing to avoid having to copy the data
/// and, if possible, should be slightly faster.
///
/// When implmenting a [`ParseBuf`] instance, you should return [`External`] if
/// possible.
///
/// [`Temporary`]: ParseBufChunk::Temporary
/// [`External`]: ParseBufChunk::External
#[derive(Copy, Clone, Debug)]
pub enum ParseBufChunk<'tmp, 'ext: 'tmp> {
/// Data owned by the current [`ParseBuf`] instance. Will only remain valid
/// until [`ParseBuf::advance`] is called.
Temporary(&'tmp [u8]),
/// Data not owned by the [`ParseBuf`] instance. Will remain valid even
/// after the [`ParseBuf`] is dropped.
External(&'ext [u8]),
}
impl<'tmp, 'ext: 'tmp> ParseBufChunk<'tmp, 'ext> {
#[inline]
pub(crate) fn to_cow(self) -> Cow<'ext, [u8]> {
match self {
Self::Temporary(data) => Cow::Owned(data.to_vec()),
Self::External(data) => Cow::Borrowed(data),
}
}
#[inline]
pub(crate) fn truncate(&mut self, len: usize) {
if self.len() <= len {
return;
}
match self {
Self::Temporary(data) => *data = data.split_at(len).0,
Self::External(data) => *data = data.split_at(len).0,
}
}
}
impl<'tmp, 'ext: 'tmp> Deref for ParseBufChunk<'tmp, 'ext> {
type Target = [u8];
#[inline]
fn deref(&self) -> &Self::Target {
match *self {
Self::Temporary(bytes) => bytes,
Self::External(bytes) => bytes,
}
}
}
/// A data source from which [`Parser`] can parse data.
///
/// A [`ParseBuf`] has two main components:
/// - An internal buffer that stores some amount of data. [`chunk`] returns a
/// view into this buffer.
/// - A position, [`advance`] moves this forward.
///
/// # Safety
/// - If [`remaining_hint`] returns `Some` then the returned value must be
/// accurate.
///
/// [`chunk`]: ParseBuf::chunk
/// [`advance`]: ParseBuf::advance
/// [`remaining_hint`]: ParseBuf::remaining_hint
pub unsafe trait ParseBuf<'p> {
/// Returns a chunk starting at the current position.
///
/// This method must never return an empty chunk. If an empty chunk would be
/// returned, it should return an error instead. [`ParseError::eof`] has
/// been provided for this, though it is not required to use it.
///
/// This method must keep returning the same data until [`advance`] has been
/// called to move past it.
///
/// See the documentation for [`ParseBufChunk`] for an explanation on when
/// to use [`ParseBufChunk::Temporary`] vs [`ParseBufChunk::External`].
///
/// [`advance`]: ParseBuf::advance
fn chunk(&mut self) -> ParseResult<ParseBufChunk<'_, 'p>>;
/// Advance this buffer past `count` bytes.
fn advance(&mut self, count: usize);
/// An indicator of how many bytes are left, if supported.
///
/// This is used for some optimizations within [`Parser`], if `Some` is
/// returned then the value must be accurate.
fn remaining_hint(&self) -> Option<usize> {
None
}
}
unsafe impl<'p> ParseBuf<'p> for &'p [u8] {
#[inline]
fn chunk(&mut self) -> ParseResult<ParseBufChunk<'_, 'p>> {
if self.is_empty() {
return Err(ParseError::eof());
}
Ok(ParseBufChunk::External(self))
}
#[inline]
fn advance(&mut self, count: usize) {
*self = self.split_at(count).1;
}
#[inline]
fn remaining_hint(&self) -> Option<usize> {
Some(self.len())
}
}
// This impl would work for any type that implements BufRead. Unfortunately,
// that conflicts with the implementation of ParseBuf for &[u8]
unsafe impl<'p, R> ParseBuf<'p> for BufReader<R>
where
R: Read,
{
#[inline]
fn chunk(&mut self) -> ParseResult<ParseBufChunk<'_, 'p>> {
let buf = self.fill_buf()?;
if buf.is_empty() {
Err(ParseError::eof())
} else {
Ok(ParseBufChunk::Temporary(buf))
}
}
#[inline]
fn advance(&mut self, count: usize) {
self.consume(count)
}
}
pub(crate) struct ParseBufCursor<'p> {
chunks: Vec<Cow<'p, [u8]>>,
offset: usize,
len: usize,
}
impl<'p> ParseBufCursor<'p> {
pub(crate) fn new<B>(buf: &mut B, mut len: usize) -> ParseResult<Self>
where
B: ParseBuf<'p>,
{
let mut chunks = Vec::with_capacity(2);
let total_len = len;
while len > 0 {
let mut chunk = buf.chunk()?;
chunk.truncate(len);
if chunk.len() > 0 {
chunks.push(chunk.to_cow());
}
let chunk_len = chunk.len();
len -= chunk_len;
buf.advance(chunk_len);
}
chunks.reverse();
Ok(Self {
chunks,
offset: 0,
len: total_len,
})
}
pub(crate) fn as_slice(&self) -> Option<&'p [u8]> {
if self.chunks.len() != 1 {
return None;
}
match &self.chunks[0] {
Cow::Borrowed(data) => Some(*data),
_ => None,
}
}
}
impl<'p> ParseBufCursor<'p> {
#[cold]
fn advance_slow(&mut self) {
while let Some(chunk) = self.chunks.last() {
if self.offset < chunk.len() {
break;
}
self.offset -= chunk.len();
self.chunks.pop();
}
if self.chunks.is_empty() {
assert_eq!(self.offset, 0, "advanced past the end of the buffer");
}
}
}
unsafe impl<'p> ParseBuf<'p> for ParseBufCursor<'p> {
#[inline]
fn chunk(&mut self) -> ParseResult<ParseBufChunk<'_, 'p>> {
match self.chunks.last().ok_or_else(ParseError::eof)? {
Cow::Borrowed(data) => Ok(ParseBufChunk::External(&data[self.offset..])),
Cow::Owned(data) => Ok(ParseBufChunk::Temporary(&data[self.offset..])),
}
}
#[inline]
fn advance(&mut self, count: usize) {
self.offset = self
.offset
.checked_add(count)
.expect("advanced past the end of the buffer");
self.len
.checked_sub(count)
.expect("advanced past the end of the buffer");
match self.chunks.last() {
Some(chunk) if chunk.len() > self.offset => (),
_ => self.advance_slow(),
}
}
#[inline]
fn remaining_hint(&self) -> Option<usize> {
Some(self.len)
}
}
/// A [`ParseBuf`] impl that tracks how many bytes it has been advanced by.
#[derive(Clone)]
pub(crate) struct TrackingParseBuf<B> {
buf: B,
offset: usize,
}
impl<B> TrackingParseBuf<B> {
pub fn new(buf: B) -> Self {
Self { buf, offset: 0 }
}
pub fn offset(&self) -> usize {
self.offset
}
}
impl<'p> TrackingParseBuf<ParseBufCursor<'p>> {
pub(crate) fn as_slice(&self) -> Option<&'p [u8]> {
self.buf.as_slice()
}
}
unsafe impl<'p, B> ParseBuf<'p> for TrackingParseBuf<B>
where
B: ParseBuf<'p>,
{
fn chunk(&mut self) -> ParseResult<ParseBufChunk<'_, 'p>> {
self.buf.chunk()
}
fn advance(&mut self, count: usize) {
self.offset += count;
self.buf.advance(count);
}
fn remaining_hint(&self) -> Option<usize> {
self.buf.remaining_hint()
}
}
#[cfg(test)]
mod tests {
use super::*;
struct ChunkBuf<'a>(Vec<&'a [u8]>);
unsafe impl<'p> ParseBuf<'p> for ChunkBuf<'p> {
fn chunk(&mut self) -> ParseResult<ParseBufChunk<'_, 'p>> {
self.0
.first()
.copied()
.map(ParseBufChunk::External)
.ok_or_else(ParseError::eof)
}
fn advance(&mut self, mut count: usize) {
while let Some(chunk) = self.0.first_mut() {
if count < chunk.len() {
chunk.advance(count);
break;
} else {
count -= chunk.len();
self.0.remove(0);
}
}
}
}
#[test]
fn cursor_over_split() {
let mut buf = ChunkBuf(vec![b"abcdef", b"012456789"]);
let _cursor = ParseBufCursor::new(&mut buf, 8);
}
#[test]
fn cursor_zero_split() {
let mut buf = ChunkBuf(vec![b"", b"01234"]);
let _cursor = ParseBufCursor::new(&mut buf, 4);
}
}