Skip to content

Commit ad39b28

Browse files
Tiago Marquespatrickelectric
Tiago Marques
authored andcommitted
added documentation to `PeekReader
1 parent 5679e26 commit ad39b28

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

mavlink-core/src/peek_reader.rs

+80-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1+
//! This module implements a buffered/peekable reader.
2+
//!
3+
//! The purpose of the buffered/peekable reader is to allow for backtracking parsers.
4+
//!
5+
//! A reader implementing the standard librairy's [`std::io::BufRead`] trait seems like a good fit, but
6+
//! it does not allow for peeking a specific number of bytes, so it provides no way to request
7+
//! more data from the underlying reader without consuming the existing data.
8+
//!
9+
//! This API still tries to adhere to the [`std::io::BufRead`]'s trait philosophy.
10+
//!
11+
//! The main type `PeekReader`does not implement [`std::io::Read`] itself, as there is no added benefit
12+
//! in doing so.
13+
//!
14+
115
use std::io::{self, ErrorKind, Read};
216

17+
// The default chunk size to read from the underlying reader
318
const DEFAULT_CHUNK_SIZE: usize = 1024;
419

20+
/// A buffered/peekable reader
21+
///
22+
/// This reader wraps a type implementing [`std::io::Read`] and adds buffering via an internal buffer.
23+
///
24+
/// It allows the user to `peek` a specified number of bytes (without consuming them),
25+
/// to `read` bytes (consuming them), or to `consume` them after `peek`ing.
26+
///
527
pub struct PeekReader<R> {
628
// Internal buffer
729
buffer: Vec<u8>,
@@ -13,15 +35,17 @@ pub struct PeekReader<R> {
1335
reader: R,
1436
// Stashed error, if any.
1537
error: Option<io::Error>,
16-
/// Whether we hit EOF on the underlying reader.
38+
// Whether we hit EOF on the underlying reader.
1739
eof: bool,
1840
}
1941

2042
impl<R: Read> PeekReader<R> {
43+
/// Instanciates a new [`PeekReader`], wrapping the provided [`std::io::Read`]er and using the default chunk size
2144
pub fn new(reader: R) -> Self {
2245
Self::with_chunk_size(reader, DEFAULT_CHUNK_SIZE)
2346
}
2447

48+
/// Instanciates a new [`PeekReader`], wrapping the provided [`std::io::Read`]er and using the supplied chunk size
2549
pub fn with_chunk_size(reader: R, preferred_chunk_size: usize) -> Self {
2650
Self {
2751
buffer: Vec::with_capacity(preferred_chunk_size),
@@ -33,41 +57,95 @@ impl<R: Read> PeekReader<R> {
3357
}
3458
}
3559

60+
/// Peeks a specified amount of bytes from the internal buffer
61+
///
62+
/// If the internal buffer does not contain enough data, this function will read
63+
/// from the underlying [`std::io::Read`]er until it does, an error occurs or no more data can be read (EOF).
64+
///
65+
/// This function does not consume data from the buffer, so subsequent calls to `peek` or `read` functions
66+
/// will still return the peeked data.
67+
///
3668
pub fn peek(&mut self, amount: usize) -> io::Result<&[u8]> {
3769
self.fetch(amount, false, false)
3870
}
3971

72+
/// Peeks an exact amount of bytes from the internal buffer
73+
///
74+
/// If the internal buffer does not contain enough data, this function will read
75+
/// from the underlying [`std::io::Read`]er until it does, an error occurs or no more data can be read (EOF).
76+
///
77+
/// If an EOF occurs and the specified amount could not be read, this function will return an [`ErrorKind::UnexpectedEof`].
78+
///
79+
/// This function does not consume data from the buffer, so subsequent calls to `peek` or `read` functions
80+
/// will still return the peeked data.
81+
///
4082
pub fn peek_exact(&mut self, amount: usize) -> io::Result<&[u8]> {
4183
self.fetch(amount, true, false)
4284
}
4385

86+
/// Consumes a specified amount of bytes from the buffer
87+
///
88+
/// If the internal buffer does not contain enough data, this function will consume as much data as is buffered.
89+
///
4490
pub fn consume(&mut self, amount: usize) -> usize {
4591
let amount = amount.min(self.buffer.len() - self.cursor);
4692
self.cursor += amount;
4793
amount
4894
}
4995

96+
/// Reads a specified amount of bytes from the internal buffer
97+
///
98+
/// If the internal buffer does not contain enough data, this function will read
99+
/// from the underlying [`std::io::Read`]er until it does, an error occurs or no more data can be read (EOF).
100+
///
101+
/// This function consumes the data from the buffer, unless an error occurs, in which case no data is consumed.
102+
///
50103
pub fn read(&mut self, amount: usize) -> io::Result<&[u8]> {
51104
self.fetch(amount, false, true)
52105
}
53106

107+
/// Reads a specified amount of bytes from the internal buffer
108+
///
109+
/// If the internal buffer does not contain enough data, this function will read
110+
/// from the underlying [`std::io::Read`]er until it does, an error occurs or no more data can be read (EOF).
111+
///
112+
/// If an EOF occurs and the specified amount could not be read, this function will return an [`ErrorKind::UnexpectedEof`].
113+
///
114+
/// This function consumes the data from the buffer, unless an error occurs, in which case no data is consumed.
115+
///
54116
pub fn read_exact(&mut self, amount: usize) -> io::Result<&[u8]> {
55117
self.fetch(amount, true, true)
56118
}
57119

120+
/// Reads a byte from the internal buffer
121+
///
122+
/// If the internal buffer does not contain enough data, this function will read
123+
/// from the underlying [`std::io::Read`]er until it does, an error occurs or no more data can be read (EOF).
124+
///
125+
/// If an EOF occurs and the specified amount could not be read, this function will return an [`ErrorKind::UnexpectedEof`].
126+
///
127+
/// This function consumes the data from the buffer, unless an error occurs, in which case no data is consumed.
128+
///
58129
pub fn read_u8(&mut self) -> io::Result<u8> {
59130
let buf = self.read_exact(1)?;
60131
Ok(buf[0])
61132
}
62133

134+
/// Returns an immutable reference to the underlying [`std::io::Read`]er
135+
///
136+
/// Reading directly from the underlying stream will cause data loss
63137
pub fn reader_ref(&mut self) -> &R {
64138
&self.reader
65139
}
66140

141+
/// Returns a mutable reference to the underlying [`std::io::Read`]er
142+
///
143+
/// Reading directly from the underlying stream will cause data loss
67144
pub fn reader_mut(&mut self) -> &mut R {
68145
&mut self.reader
69146
}
70147

148+
/// Internal function to fetch data from the internal buffer and/or reader
71149
fn fetch(&mut self, amount: usize, exact: bool, consume: bool) -> io::Result<&[u8]> {
72150
let previous_len = self.buffer.len();
73151
let mut buffered = previous_len - self.cursor;
@@ -109,6 +187,7 @@ impl<R: Read> PeekReader<R> {
109187
}
110188
}
111189
}
190+
// if some bytes were read, add them to the buffer
112191
if read > 0 {
113192
if self.buffer.capacity() - previous_len < read {
114193
// reallocate

0 commit comments

Comments
 (0)