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
45
46
47
48
49
50
use async_trait::async_trait;
use crate::value::Value;

#[async_trait(?Send)]
pub trait Console {
    fn print(&mut self, v: Value);
    fn new_line(&mut self);
    async fn wait(&mut self);
}

#[async_trait(?Send)]
impl<'a, C: Console> Console for &'a mut C {
    #[inline]
    fn print(&mut self, v: Value) {
        (**self).print(v);
    }
    #[inline]
    fn new_line(&mut self) {
        (**self).new_line();
    }
    #[inline]
    async fn wait(&mut self) {
        (**self).wait().await;
    }
}

#[cfg(test)]
pub struct RecordConsole(pub String);

#[cfg(test)]
impl RecordConsole {
    pub fn new() -> Self {
        Self(String::with_capacity(8196))
    }
}

#[cfg(test)]
#[async_trait(?Send)]
impl Console for RecordConsole {
    fn print(&mut self, v: Value) {
        self.0 += &v.to_string();
    }
    fn new_line(&mut self) {
        self.0.push('@');
    }
    async fn wait(&mut self) {
        self.0.push('#');
    }
}