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
impl Listener
Sourcepub fn new_case_insensitive(key: &str) -> Listener
pub fn new_case_insensitive(key: &str) -> Listener
Sourcepub fn heard_key(&self) -> bool
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.");
}Sourcepub fn wait_for_key(self) -> Result<bool, Box<dyn Any + Send>>
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§
impl Freeze for Listener
impl !RefUnwindSafe for Listener
impl Send for Listener
impl Sync for Listener
impl Unpin for Listener
impl UnsafeUnpin for Listener
impl !UnwindSafe for Listener
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more