1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use {super::*, crate::*};

#[derive(Clone)]
/// A control type for writing messages to the client, or stopping it.
pub struct Control {
    pub(super) writer: Writer,
    pub(super) stop: abort::Abort,
}

impl Control {
    /// Get a mutable reference to the [Writer](./encode/struct.AsyncEncoder.html)
    ///
    /// You can clone this to pass around it around
    pub fn writer(&mut self) -> &mut Writer {
        &mut self.writer
    }

    /// Signal the client to stop
    ///
    /// # Example
    /// ```rust
    /// # use twitchchat::{Runner, Status, RateLimit, Dispatcher};
    /// # use tokio::spawn;
    /// # let conn = tokio_test::io::Builder::new().wait(std::time::Duration::from_millis(10000)).build();
    /// # let fut = async move {
    /// let (runner, control) = Runner::new(Dispatcher::default(), RateLimit::default());
    ///
    /// // calling stop will cause 'run' to return Ok(Status::Canceled)
    /// spawn(async move { control.stop() });
    ///
    /// assert_eq!(runner.run(conn).await.unwrap(), Status::Canceled);
    /// # };
    /// # tokio::runtime::Runtime::new().unwrap().block_on(fut);
    /// ```
    pub fn stop(&self) {
        self.stop.cancel()
    }
}

impl std::fmt::Debug for Control {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Control").finish()
    }
}