Skip to main content

FastScanner

Struct FastScanner 

Source
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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub const fn is_empty(&self) -> bool

Whether the scanner is finished.

Source

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.

Source

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.

Source

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.

Source

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>

Source

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.

Source

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.

Source

pub fn scan_int(&mut self, tag: Tag) -> Result<i32, FastScanError>

Scan an expected integer.

Source

pub fn scan_long_int(&mut self, tag: Tag) -> Result<i64, FastScanError>

Scan an expected long integer.

Source

pub fn scan_big_int(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>

Scan an expected big integer.

Source

pub fn scan_enum(&mut self, tag: Tag) -> Result<u32, FastScanError>

Scan an expected enumeration.

Source

pub fn scan_bool(&mut self, tag: Tag) -> Result<bool, FastScanError>

Scan an expected boolean.

Source

pub fn scan_text(&mut self, tag: Tag) -> Result<&'a str, FastScanError>

Scan an expected text string.

Source

pub fn scan_text_bytes(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>

Scan an expected text string as bytes.

Source

pub fn scan_bytes(&mut self, tag: Tag) -> Result<&'a [u8], FastScanError>

Scan an expected byte string.

Source

pub fn scan_date_time(&mut self, tag: Tag) -> Result<i64, FastScanError>

Scan an expected date-time.

Source

pub fn scan_interval(&mut self, tag: Tag) -> Result<u32, FastScanError>

Scan an expected interval.

Source§

impl FastScanner<'_>

Source

pub fn skip_struct(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected structure.

Source

pub fn skip_fixed_struct( &mut self, tag: Tag, length: u32, ) -> Result<(), FastScanError>

Skip an expected structure with a pre-determined length.

Source

pub fn skip_int(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected integer.

Source

pub fn skip_long_int(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected long integer.

Source

pub fn skip_big_int(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected big integer.

Source

pub fn skip_enum(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected enumeration.

Source

pub fn skip_bool(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected boolean.

Source

pub fn skip_text(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected text string.

Source

pub fn skip_bytes(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected byte string.

Source

pub fn skip_date_time(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected date-time.

Source

pub fn skip_interval(&mut self, tag: Tag) -> Result<(), FastScanError>

Skip an expected interval.

Source§

impl<'a> FastScanner<'a>

Source

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.

Source

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.

Source

pub fn scan_opt_int(&mut self, tag: Tag) -> Result<Option<i32>, FastScanError>

Scan an optional integer.

Source

pub fn scan_opt_long_int( &mut self, tag: Tag, ) -> Result<Option<i64>, FastScanError>

Scan an optional long integer.

Source

pub fn scan_opt_big_int( &mut self, tag: Tag, ) -> Result<Option<&'a [u8]>, FastScanError>

Scan an optional big integer.

Source

pub fn scan_opt_enum(&mut self, tag: Tag) -> Result<Option<u32>, FastScanError>

Scan an optional enumeration.

Source

pub fn scan_opt_bool(&mut self, tag: Tag) -> Result<Option<bool>, FastScanError>

Scan an optional boolean.

Source

pub fn scan_opt_text( &mut self, tag: Tag, ) -> Result<Option<&'a str>, FastScanError>

Scan an optional text string.

Source

pub fn scan_opt_text_bytes( &mut self, tag: Tag, ) -> Result<Option<&'a [u8]>, FastScanError>

Scan an optional text string as bytes.

Source

pub fn scan_opt_bytes( &mut self, tag: Tag, ) -> Result<Option<&'a [u8]>, FastScanError>

Scan an optional byte string.

Source

pub fn scan_opt_date_time( &mut self, tag: Tag, ) -> Result<Option<i64>, FastScanError>

Scan an optional date-time.

Source

pub fn scan_opt_interval( &mut self, tag: Tag, ) -> Result<Option<u32>, FastScanError>

Scan an optional interval.

Source§

impl<'a> FastScanner<'a>

Source

pub fn skip_opt_struct(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional structure.

Source

pub fn skip_opt_fixed_struct( &mut self, tag: Tag, length: u32, ) -> Result<bool, FastScanError>

Skip an optional structure with a pre-determined length.

Source

pub fn skip_opt_int(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional integer.

Source

pub fn skip_opt_long_int(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional long integer.

Source

pub fn skip_opt_big_int(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional big integer.

Source

pub fn skip_opt_enum(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional enumeration.

Source

pub fn skip_opt_bool(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional boolean.

Source

pub fn skip_opt_text(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional text string.

Source

pub fn skip_opt_bytes(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional byte string.

Source

pub fn skip_opt_date_time(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional date-time.

Source

pub fn skip_opt_interval(&mut self, tag: Tag) -> Result<bool, FastScanError>

Skip an optional interval.

Trait Implementations§

Source§

impl<'a> Clone for FastScanner<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<'a> Forget for FastScanner<'a>

§

impl<'a> Freeze for FastScanner<'a>

§

impl<'a> Move for FastScanner<'a>

§

impl<'a> RefUnwindSafe for FastScanner<'a>

§

impl<'a> Send for FastScanner<'a>

§

impl<'a> Sync for FastScanner<'a>

§

impl<'a> Unpin for FastScanner<'a>

§

impl<'a> UnsafeUnpin for FastScanner<'a>

§

impl<'a> UnwindSafe for FastScanner<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more