pub struct FastScanner<'a> {
pub tag: Option<Tag>,
/* private fields */
}Expand description
A fast TTLV scanner.
In most cases, errors are highly unlikely. This scanner optimizes for this
by omitting error handling and pinpointing facilities – it detects errors
without tracking any details of them. In some cases, it will delay errors,
returning garbage data instead of Err. If scanning with FastScanner
fails, a different scanner (e.g. crate::ttlv::from_slice()) needs to be
used to ascertain the position and cause of the error.
§Delayed Errors
For efficiency, the scanner will sometimes return invalid data, e.g. from
Self::scan_int(). This only occurs if the input is invalid, but it is
more efficient than returning an actual Err. Once invalid data has been
returned, at any point, Self::has_delayed_error() will report true.
The caller should never “trust” the data being returned, e.g. to decide how much memory to allocate. Any field can return invalid data; the input may be well-formed but contain an unexpected value for a field.
§Streaming Operation
FastScanner can be used in streaming fashion. For example, when
receiving a stream of serialized items (e.g. KMIP requests), one cannot
wait for the end of the stream to parse. Instead, the user should store the
input in a dynamically allocated buffer. Once some data is in the buffer, a
FastScanner can be initialized over it, and Self::have_next() can be
used to check whether a complete TTLV item is available. If it is, the item
can be parsed; otherwise, more data needs to be buffered.
// Buffer data in a `Vec`.
let mut buffer = Vec::new();
// Read some data from the network.
let mut stream = TcpStream::connect("127.0.0.1:8000").unwrap();
stream.set_nonblocking(true).unwrap();
loop {
// Fill the buffer as much as possible.
// NOTE: Only ignore 'std::io::ErrorKind::WouldBlock'.
let _ = stream.read_to_end(&mut buffer);
// Try parsing whole TTLV elements from the buffer.
while let Some((mut scanner, rest)) = FastScanner::next_element(&buffer) {
// Read the TTLV element out.
//...
// Remove the parsed data from the buffer.
// NOTE: This is quite inefficient, use a better data structure.
let remaining = rest.len();
let consumed = buffer.len() - remaining;
buffer.drain(0..consumed);
}
}Fields§
§tag: Option<Tag>Implementations§
Source§impl<'a> FastScanner<'a>
impl<'a> FastScanner<'a>
Sourcepub fn new(input: &'a [u8]) -> Result<Self, FastScanError>
pub fn new(input: &'a [u8]) -> Result<Self, FastScanError>
Construct a new FastScanner.
It is expected that the input will be scanned in its entirety.
§Errors
Fails if the input is not a multiple of 8 bytes. TTLV elements are always serialized into multiples of 8 bytes, so the presence of a partial 8-byte block immediately implies the input is invalid.
Sourcepub fn next_element(input: &'a [u8]) -> Option<(Self, &'a [u8])>
pub fn next_element(input: &'a [u8]) -> Option<(Self, &'a [u8])>
Prepare to parse the next TTLV element from a buffer.
If a whole TTLV element is present in the input, Some is returned,
with a scanner ready to parse that element and the unused portion of
the buffer. Otherwise, None is returned, and more data needs to be
collected for parsing.
See the FastScanner documentation on streaming
operation for more details.
Sourcepub const fn from_blocks(input: &'a [[u8; 8]]) -> Self
pub const fn from_blocks(input: &'a [[u8; 8]]) -> Self
Construct a new FastScanner from a slice of 8-byte blocks.
TTLV elements are always serialized into multiples of 8 bytes.
FastScanner internally represents its input as slices of 8-byte
blocks so it is easier to process.
Sourcepub const fn remaining(&self) -> &'a [[u8; 8]]
pub const fn remaining(&self) -> &'a [[u8; 8]]
The remaining input to the scanner.
Note: in case an error has occurred (delayed or otherwise), the amount of remaining data is not well specified. It might not include the data causing the failure.
Sourcepub const fn has_delayed_error(&self) -> bool
pub const fn has_delayed_error(&self) -> bool
Whether an error has been delayed.
This should be checked before acting on any parsed data. If an error
has been delayed, all previously-returned data should be assumed to be
garbage. See the FastScanner documentation for
more information about delayed errors.
Sourcepub fn have_next(&self) -> Result<(), Option<u32>>
pub fn have_next(&self) -> Result<(), Option<u32>>
Whether an entire TTLV element is available.
The length of the next element will be checked;
- if it is entirely loaded in the buffer, Ok(()) will be returned.
- if the buffer is empty, Err(None) will be returned.
- if the buffer is partially full, the L (from TT_L_V) value must be available and so Err(Some(n)) is returned when n is the number of missing bytes where n > 0.
This can be used for streamed parsing, to decide whether more data needs to be buffered and, if known, how much more data needs to be fetched from the source.
Sourcepub fn finish(self) -> Result<(), FastScanError>
pub fn finish(self) -> Result<(), FastScanError>
Finish the scanner.
If there is remaining input, or an error was delayed, Err is
returned. If additional remaining input is allowed, use
Self::finish_non_exhaustive() instead.
Sourcepub fn finish_non_exhaustive(self) -> Result<(), FastScanError>
pub fn finish_non_exhaustive(self) -> Result<(), FastScanError>
Finish the scanner non-exhaustively.
Additional input is ignored. Err is returned only if an error is
delayed. If additional remaining input is not allowed, use
Self::finish() instead.
Source§impl<'a> FastScanner<'a>
impl<'a> FastScanner<'a>
Sourcepub fn scan_struct(&mut self, tag: Tag) -> Result<Self, FastScanError>
pub fn scan_struct(&mut self, tag: Tag) -> Result<Self, FastScanError>
Scan an expected structure.
The returned sub-scanner can be used to scan the fields of the
struct; self will independently continue from the end of the
struct.
Sourcepub fn scan_fixed_struct(
&mut self,
tag: Tag,
length: u32,
) -> Result<Self, FastScanError>
pub fn scan_fixed_struct( &mut self, tag: Tag, length: u32, ) -> Result<Self, FastScanError>
Scan an expected structure with a pre-determined length.
The returned sub-scanner can be used scan the fields of the struct;
self will independently continue from the end of the struct. If
the length encoded in the input is different from the pre-determined
length, the error will be delayed and both scanners will return
garbage.
Sourcepub fn scan_long_int(&mut self, tag: Tag) -> Result<i64, FastScanError>
pub fn scan_long_int(&mut self, tag: Tag) -> Result<i64, FastScanError>
Scan an expected long integer.
Sourcepub fn scan_big_int(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>
pub fn scan_big_int(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>
Scan an expected big integer.
Sourcepub fn scan_enum(&mut self, tag: Tag) -> Result<u32, FastScanError>
pub fn scan_enum(&mut self, tag: Tag) -> Result<u32, FastScanError>
Scan an expected enumeration.
Sourcepub fn scan_text(&mut self, tag: Tag) -> Result<&'a str, FastScanError>
pub fn scan_text(&mut self, tag: Tag) -> Result<&'a str, FastScanError>
Scan an expected text string.
Sourcepub fn scan_text_bytes(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>
pub fn scan_text_bytes(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>
Scan an expected text string as bytes.
Sourcepub fn scan_bytes(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>
pub fn scan_bytes(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>
Scan an expected byte string.
Sourcepub fn scan_date_time(&mut self, tag: Tag) -> Result<i64, FastScanError>
pub fn scan_date_time(&mut self, tag: Tag) -> Result<i64, FastScanError>
Scan an expected date-time.
Sourcepub fn scan_interval(&mut self, tag: Tag) -> Result<u32, FastScanError>
pub fn scan_interval(&mut self, tag: Tag) -> Result<u32, FastScanError>
Scan an expected interval.
Source§impl FastScanner<'_>
impl FastScanner<'_>
Sourcepub fn skip_struct(&mut self, tag: Tag) -> Result<(), FastScanError>
pub fn skip_struct(&mut self, tag: Tag) -> Result<(), FastScanError>
Skip an expected structure.
Sourcepub fn skip_fixed_struct(
&mut self,
tag: Tag,
length: u32,
) -> Result<(), FastScanError>
pub fn skip_fixed_struct( &mut self, tag: Tag, length: u32, ) -> Result<(), FastScanError>
Skip an expected structure with a pre-determined length.
Sourcepub fn skip_long_int(&mut self, tag: Tag) -> Result<(), FastScanError>
pub fn skip_long_int(&mut self, tag: Tag) -> Result<(), FastScanError>
Skip an expected long integer.
Sourcepub fn skip_big_int(&mut self, tag: Tag) -> Result<(), FastScanError>
pub fn skip_big_int(&mut self, tag: Tag) -> Result<(), FastScanError>
Skip an expected big integer.
Sourcepub fn skip_enum(&mut self, tag: Tag) -> Result<(), FastScanError>
pub fn skip_enum(&mut self, tag: Tag) -> Result<(), FastScanError>
Skip an expected enumeration.
Sourcepub fn skip_text(&mut self, tag: Tag) -> Result<(), FastScanError>
pub fn skip_text(&mut self, tag: Tag) -> Result<(), FastScanError>
Skip an expected text string.
Sourcepub fn skip_bytes(&mut self, tag: Tag) -> Result<(), FastScanError>
pub fn skip_bytes(&mut self, tag: Tag) -> Result<(), FastScanError>
Skip an expected byte string.
Sourcepub fn skip_date_time(&mut self, tag: Tag) -> Result<(), FastScanError>
pub fn skip_date_time(&mut self, tag: Tag) -> Result<(), FastScanError>
Skip an expected date-time.
Sourcepub fn skip_interval(&mut self, tag: Tag) -> Result<(), FastScanError>
pub fn skip_interval(&mut self, tag: Tag) -> Result<(), FastScanError>
Skip an expected interval.
Source§impl<'a> FastScanner<'a>
impl<'a> FastScanner<'a>
Sourcepub fn scan_opt_struct(
&mut self,
tag: Tag,
) -> Result<Option<Self>, FastScanError>
pub fn scan_opt_struct( &mut self, tag: Tag, ) -> Result<Option<Self>, FastScanError>
Scan an optional structure.
The returned sub-scanner can be used to scan the fields of the
struct; self will independently continue from the end of the
struct.
Sourcepub fn scan_opt_fixed_struct(
&mut self,
tag: Tag,
length: u32,
) -> Result<Option<Self>, FastScanError>
pub fn scan_opt_fixed_struct( &mut self, tag: Tag, length: u32, ) -> Result<Option<Self>, FastScanError>
Scan an optional structure with a pre-determined length.
The returned sub-scanner can be used to scan the fields of the
struct; self will independently continue from the end of the
struct. If the length encoded in the input is different from the
pre-determined length, the error will be delayed and both scanners
will return garbage.
Sourcepub fn scan_opt_int(&mut self, tag: Tag) -> Result<Option<i32>, FastScanError>
pub fn scan_opt_int(&mut self, tag: Tag) -> Result<Option<i32>, FastScanError>
Scan an optional integer.
Sourcepub fn scan_opt_long_int(
&mut self,
tag: Tag,
) -> Result<Option<i64>, FastScanError>
pub fn scan_opt_long_int( &mut self, tag: Tag, ) -> Result<Option<i64>, FastScanError>
Scan an optional long integer.
Sourcepub fn scan_opt_big_int(
&mut self,
tag: Tag,
) -> Result<Option<&'a [u8]>, FastScanError>
pub fn scan_opt_big_int( &mut self, tag: Tag, ) -> Result<Option<&'a [u8]>, FastScanError>
Scan an optional big integer.
Sourcepub fn scan_opt_enum(&mut self, tag: Tag) -> Result<Option<u32>, FastScanError>
pub fn scan_opt_enum(&mut self, tag: Tag) -> Result<Option<u32>, FastScanError>
Scan an optional enumeration.
Sourcepub fn scan_opt_bool(&mut self, tag: Tag) -> Result<Option<bool>, FastScanError>
pub fn scan_opt_bool(&mut self, tag: Tag) -> Result<Option<bool>, FastScanError>
Scan an optional boolean.
Sourcepub fn scan_opt_text(
&mut self,
tag: Tag,
) -> Result<Option<&'a str>, FastScanError>
pub fn scan_opt_text( &mut self, tag: Tag, ) -> Result<Option<&'a str>, FastScanError>
Scan an optional text string.
Sourcepub fn scan_opt_text_bytes(
&mut self,
tag: Tag,
) -> Result<Option<&'a [u8]>, FastScanError>
pub fn scan_opt_text_bytes( &mut self, tag: Tag, ) -> Result<Option<&'a [u8]>, FastScanError>
Scan an optional text string as bytes.
Sourcepub fn scan_opt_bytes(
&mut self,
tag: Tag,
) -> Result<Option<&'a [u8]>, FastScanError>
pub fn scan_opt_bytes( &mut self, tag: Tag, ) -> Result<Option<&'a [u8]>, FastScanError>
Scan an optional byte string.
Sourcepub fn scan_opt_date_time(
&mut self,
tag: Tag,
) -> Result<Option<i64>, FastScanError>
pub fn scan_opt_date_time( &mut self, tag: Tag, ) -> Result<Option<i64>, FastScanError>
Scan an optional date-time.
Sourcepub fn scan_opt_interval(
&mut self,
tag: Tag,
) -> Result<Option<u32>, FastScanError>
pub fn scan_opt_interval( &mut self, tag: Tag, ) -> Result<Option<u32>, FastScanError>
Scan an optional interval.
Source§impl<'a> FastScanner<'a>
impl<'a> FastScanner<'a>
Sourcepub fn skip_opt_struct(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_struct(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional structure.
Sourcepub fn skip_opt_fixed_struct(
&mut self,
tag: Tag,
length: u32,
) -> Result<bool, FastScanError>
pub fn skip_opt_fixed_struct( &mut self, tag: Tag, length: u32, ) -> Result<bool, FastScanError>
Skip an optional structure with a pre-determined length.
Sourcepub fn skip_opt_int(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_int(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional integer.
Sourcepub fn skip_opt_long_int(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_long_int(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional long integer.
Sourcepub fn skip_opt_big_int(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_big_int(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional big integer.
Sourcepub fn skip_opt_enum(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_enum(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional enumeration.
Sourcepub fn skip_opt_bool(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_bool(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional boolean.
Sourcepub fn skip_opt_text(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_text(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional text string.
Sourcepub fn skip_opt_bytes(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_bytes(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional byte string.
Sourcepub fn skip_opt_date_time(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_date_time(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional date-time.
Sourcepub fn skip_opt_interval(&mut self, tag: Tag) -> Result<bool, FastScanError>
pub fn skip_opt_interval(&mut self, tag: Tag) -> Result<bool, FastScanError>
Skip an optional interval.