Skip to main content

crossterm/event/sys/unix/waker/
tty.rs

1use std::{
2    io::{self, Write},
3    os::unix::net::UnixStream,
4    sync::{Arc, Mutex},
5};
6
7/// Allows to wake up the EventSource::try_read() method.
8#[derive(Clone, Debug)]
9pub(crate) struct Waker {
10    inner: Arc<Mutex<UnixStream>>,
11}
12
13impl Waker {
14    /// Create a new `Waker`.
15    pub(crate) fn new(writer: UnixStream) -> Self {
16        Self {
17            inner: Arc::new(Mutex::new(writer)),
18        }
19    }
20
21    /// Wake up the [`Poll`] associated with this `Waker`.
22    ///
23    /// Readiness is set to `Ready::readable()`.
24    pub(crate) fn wake(&self) -> io::Result<()> {
25        self.inner.lock().unwrap().write_all(&[0])?;
26        Ok(())
27    }
28}