summaryrefslogtreecommitdiff
path: root/src/devices/stream.rs
blob: 3d376b6e2c4dea449f340989d5f77766b7151ba6 (plain) (blame)
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
use std::io::{Read, Write, Stdin, Stdout};

pub struct StreamDevice {
    pub wake_flag: bool,

    pub input_control: bool,
    pub output_control: bool,
    pub read_queue: Vec<u8>,

    pub stdin: Stdin,
    pub stdout: Stdout,
}

impl StreamDevice {
    pub fn new() -> Self {
        Self {
            wake_flag: false,

            input_control: true,
            output_control: true,
            read_queue: Vec::with_capacity(256),

            stdin: std::io::stdin(),
            stdout: std::io::stdout(),
        }
    }

    // pub fn fetch_stdin(&mut self) {
    //     match self.stdin.read(&mut self.read_queue) {
    //         Ok() => (),
    //         Err() => (),
    //     }
    // }

    pub fn read_stdin(&mut self) -> u8 {
        let mut buffer = [0; 1];
        match self.stdin.read_exact(&mut buffer) {
            Ok(()) => buffer[0],
            Err(_) => 0,
        }
    }

    pub fn write_stdout(&mut self, val: u8) {
        self.stdout.write_all(&[val]).unwrap();
    }
}