seq_watcher

Struct SequenceWatcher

Source
pub struct SequenceWatcher<T>
where T: PartialEq + Clone + Debug,
{ /* private fields */ }
Expand description

Monitors a stream of data for a sequence.

§Examples

use seq_watcher::SequenceWatcher;

const CTRL_C: u8 = 3;

// Watch the input stream for two consecutive <CTRL-C> characters.
let watcher = SequenceWatcher::new(&[CTRL_C, CTRL_C]);

for b in b'a'..=b'z' {
    assert_eq!(false, watcher.check(&b));   // Send single ASCII byte
    assert_eq!(false, watcher.check(&CTRL_C));   // Send single <Ctrl-C>
}
assert_eq!(true, watcher.check(&CTRL_C));    // Send a second <Ctrl-C>

Implementations§

Source§

impl<T: PartialEq + Clone + Debug> SequenceWatcher<T>

Source

pub fn new(seq: &[T]) -> Self

Create a new SequenceWatcher from a slice of data. The data type can be anything with PartialEq, Debug, and Clone traits.

Internally, the SequenceWatcher structure contains a vector of the sequence it monitors and an index indicating where in the sequence it is expecting next.

§Examples
let watcher = SequenceWatcher::new(&['q', 'u', 'i', 't']);

assert_eq!(false, watcher.check(&'q'));
assert_eq!(false, watcher.check(&'u'));
assert_eq!(false, watcher.check(&'i'));
assert_eq!(true, watcher.check(&'t'));
§Panics

SequenceWatcher::new will panic if the sequence slice is empty.

Source

pub fn check(&self, value: &T) -> bool

Tells the SequenceWatcher of a new input item and checks to see if a sequence has been completed. Returns true when the sequence is completed.

§Examples
let watcher = SequenceWatcher::new(&[Some("found")]);

assert_eq!(false, watcher.check(&None));
assert_eq!(false, watcher.check(&Some("something")));
assert_eq!(false, watcher.check(&Some("anything")));
assert_eq!(true, watcher.check(&Some("found")));
Source

pub fn expects(&self) -> &T

Returns the a reference to the data value that the SequenceWatcher is expecting next.

§Examples
let watcher = SequenceWatcher::new(&[1, 2]);

assert_eq!(1, *watcher.expects());
assert_eq!(false, watcher.check(&0));
assert_eq!(1, *watcher.expects());
assert_eq!(false, watcher.check(&1));
assert_eq!(2, *watcher.expects());
assert_eq!(true, watcher.check(&2));
assert_eq!(1, *watcher.expects());
Source

pub fn index(&self) -> usize

Returns the index in the sequence that the SequenceWatcher is expecting next. The index will never be greater than or equal to the sequence length.

§Examples
let watcher = SequenceWatcher::new(&[1, 2]);

assert_eq!(0, watcher.index());
assert_eq!(false, watcher.check(&0));
assert_eq!(0, watcher.index());
assert_eq!(false, watcher.check(&1));
assert_eq!(1, watcher.index());
assert_eq!(true, watcher.check(&2));
assert_eq!(0, watcher.index());
Source

pub fn reset(&self)

Resets the SequenceWatcher so it doesn’t remember if it has seen any protions of the sequence.

§Examples
let watcher = SequenceWatcher::new(&[1, 2]);

assert_eq!(false, watcher.check(&1));
assert_eq!(1, watcher.index());
watcher.reset();
assert_eq!(false, watcher.check(&2));
assert_eq!(false, watcher.check(&1));
assert_eq!(true, watcher.check(&2));
Source

pub fn sequence(&self) -> &[T]

Returns a reference to the sequence.

§Examples
let watcher = SequenceWatcher::new(&[Ok(()), Err(())]);

assert_eq!(&[Ok(()), Err(())], watcher.sequence());
Source

pub fn len(&self) -> usize

Returns the length of the sequence.

§Examples
let watcher = SequenceWatcher::new(&[false, true, false, true]);

assert_eq!(4, watcher.len());

Trait Implementations§

Source§

impl<T> Clone for SequenceWatcher<T>
where T: PartialEq + Clone + Debug + Clone,

Source§

fn clone(&self) -> SequenceWatcher<T>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for SequenceWatcher<T>
where T: PartialEq + Clone + Debug + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq + Debug + Clone> From<&[T]> for SequenceWatcher<T>

Creates a new SequenceWatcher from a slice.

§Examples

let watcher = SequenceWatcher::from(&[b'X'][..]);   // This is treated as a slice.

assert_eq!(true, watcher.check(&b'X'));

This also works for array references for arrays of up to 32 itmes.

let watcher = SequenceWatcher::from(&[b'X']);   // This is treated as an array reference.

assert_eq!(true, watcher.check(&b'X'));

There is no support for a zero length array reference, so when using the array version, it will fail to compile.

let watcher: SequenceWatcher<u8> = SequenceWatcher::from(&[]);
Source§

fn from(src: &[T]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 1]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 1]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 10]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 10]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 11]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 11]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 12]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 12]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 13]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 13]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 14]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 14]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 15]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 15]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 16]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 16]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 17]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 17]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 18]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 18]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 19]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 19]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 2]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 2]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 20]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 20]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 21]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 21]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 22]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 22]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 23]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 23]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 24]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 24]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 25]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 25]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 26]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 26]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 27]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 27]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 28]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 28]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 29]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 29]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 3]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 3]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 30]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 30]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 31]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 31]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 32]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 32]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 4]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 4]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 5]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 5]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 6]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 6]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 7]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 7]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 8]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 8]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<&[T; 9]> for SequenceWatcher<T>

Creates a new SequenceWatcher from an array.

Source§

fn from(src: &[T; 9]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq + Debug + Clone> From<Vec<T>> for SequenceWatcher<T>

Creates a new SequenceWatcher from a Vec.

§Examples

let seq = vec![b'X'];
let watcher = SequenceWatcher::from(seq);

assert_eq!(true, watcher.check(&b'X'));
Source§

fn from(src: Vec<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !Freeze for SequenceWatcher<T>

§

impl<T> !RefUnwindSafe for SequenceWatcher<T>

§

impl<T> Send for SequenceWatcher<T>
where T: Send,

§

impl<T> !Sync for SequenceWatcher<T>

§

impl<T> Unpin for SequenceWatcher<T>
where T: Unpin,

§

impl<T> UnwindSafe for SequenceWatcher<T>
where T: UnwindSafe,

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, dst: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.