Skip to main content

Listener

Struct Listener 

Source
pub struct Listener { /* private fields */ }
Expand description

The Listener struct holds a thread that listens for a specified string written to the console.

The Listener struct implements functions to check whether the string was typed, heard_key, and to wait for the string to get typed, wait_for_key. Create a new instance of Listener using new or new_case_insensitive and specify the string to listen for.

Implementations§

Source§

impl Listener

Source

pub fn new(key: &str) -> Listener

Creates a new Listener instance.

The key is the string that the Listener listens for.

§Panics

The new function panics if the key is an empty string

§Examples
let listener = Listener::new("q");
Source

pub fn new_case_insensitive(key: &str) -> Listener

Creates a new case insensitive Listener instance.

The key is the string that the Listener listens for.

§Panics

The new_case_insensitive function panics if the key is an empty string

§Examples
let listener = Listener::new_case_insensitive("q");
Source

pub fn heard_key(&self) -> bool

Returns true if the Listener has heard the key.

§Examples
let listener = Listener::new("q");
 
while !listener.heard_key() {
    println!("Waiting for key.");
}
Source

pub fn wait_for_key(self) -> Result<bool, Box<dyn Any + Send>>

Blocks the current thread until the listening thread ends. This consumes the Listener.

Returns Ok containing a boolean indicating whether the key was heard.

§Errors

Returns Err if the listening thread panicked.

§Examples
let listener = Listener::new("q");
 
heard_key = listener.wait_for_key().unwrap();
 
assert!(heard_key);

Auto Trait Implementations§

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> 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, 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.