Monitors a stream of data looking for a sequence (or multiple sequences)
Stream Sequence Watcher
Includes a pair of structures, [SequenceWatcher] and [SequenceWatchers]. They monitor a stream of data looking for a specific sequence of values.
The original purpose for this crate was to monitor bytes of data recieved from stdin for a specific sequence that indicated that the input thread should shut down. Originally, only a single sequence was monitored for, but I added a simple way to look for multiple sequences simultaneously.
Examples
Intended Behavior and Limitations
- [SequenceWatcher] and [SequenceWatchers] both continue monitoring the stream after a check returns true. For example, looking for the sequence (A, A), would return false, true, true when given the input (A, A, A).
use SequenceWatchers;
let test_stream = vec!;
let watchers = new;
for in test_stream
[SequenceWatcher] and [SequenceWatchers] that are given empty sequences always return false.
# use SequenceWatchers;
let mut watcher = new; // Create empty watchers.
let mut watchers = new; // Create empty watchers.
let mut all_bytes = Vec with_capacity;
for b in 0..=u8MAX
watchers.add; // Add of empty sequence does nothing.
watchers.add; // Add sequuence with all bytes.
for b in 0..u8MAX
assert_eq!; // With last byte in sequence, returns true.
- Datatypes compatible are slightly more restrictive for [SequenceWatchers] than for [SequenceWatcher]. [SequenceWatchers] requires the datatype to be [Eq], wheras [SequenceWatcher] only needs [PartialEq].
Float types are [PartialEq] but not [Eq].
# use seq_watcher::SequenceWatchers;
let watchers = SequenceWatchers::new(&[0.0]); // Float values are not Eq
# use SequenceWatcher;
let watcher = new; // Float values are PartialEq.
Performance
The [SequenceWatcher] structure is resonably performant, but the [SequenceWatchers] structure needs work. [SequenceWatchers] is currently implemented as a [HashMap] of [SequenceWatcher] structures, but it would be better implemented with some sort of multi-state-aware trie. [SequenceWatchers] was created as an afterthought, since I mainly needed the [SequenceWatcher] for another project.