slvm/heap/
io.rs

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::fs::File;
use std::io;
use std::io::{BufReader, BufWriter, ErrorKind, Read, Seek, SeekFrom, Write};
use std::sync::{Arc, Mutex, MutexGuard};

#[derive(Copy, Clone, Debug)]
pub enum HeapIoError {
    Closed,
    NotFile,
}

#[derive(Clone)]
pub struct HeapIo {
    io: Arc<Mutex<Io>>,
}

impl HeapIo {
    pub fn from_file(file: File) -> Self {
        let io = Arc::new(Mutex::new(Io::File(Some(file))));
        Self { io }
    }

    pub fn stdin() -> Self {
        let io = Arc::new(Mutex::new(Io::StdIn));
        Self { io }
    }

    pub fn stdout() -> Self {
        let io = Arc::new(Mutex::new(Io::StdOut));
        Self { io }
    }

    pub fn stderr() -> Self {
        let io = Arc::new(Mutex::new(Io::StdErr));
        Self { io }
    }

    pub fn close(&self) {
        if let Ok(mut guard) = self.io.lock() {
            *guard = Io::Closed
        }
    }

    pub fn to_buf_reader(&self) -> Result<(), HeapIoError> {
        if let Ok(mut guard) = self.io.lock() {
            match &mut *guard {
                Io::File(f) => match f.take() {
                    Some(f) => *guard = Io::FileReadBuf(BufReader::new(f)),
                    None => panic!("file without a file"),
                },
                Io::FileReadBuf(_) => return Err(HeapIoError::NotFile),
                Io::FileWriteBuf(_) => return Err(HeapIoError::NotFile),
                Io::StdIn => return Err(HeapIoError::NotFile),
                Io::StdOut => return Err(HeapIoError::NotFile),
                Io::StdErr => return Err(HeapIoError::NotFile),
                Io::Closed => return Err(HeapIoError::Closed),
            }
        }
        Ok(())
    }

    pub fn to_buf_writer(&self) -> Result<(), HeapIoError> {
        if let Ok(mut guard) = self.io.lock() {
            match &mut *guard {
                Io::File(f) => match f.take() {
                    Some(f) => *guard = Io::FileWriteBuf(BufWriter::new(f)),
                    None => panic!("file without a file"),
                },
                Io::FileReadBuf(_) => return Err(HeapIoError::NotFile),
                Io::FileWriteBuf(_) => return Err(HeapIoError::NotFile),
                Io::StdIn => return Err(HeapIoError::NotFile),
                Io::StdOut => return Err(HeapIoError::NotFile),
                Io::StdErr => return Err(HeapIoError::NotFile),
                Io::Closed => return Err(HeapIoError::Closed),
            }
        }
        Ok(())
    }

    pub fn get_io(&self) -> IoGuard {
        let io = self.io.lock().unwrap();
        IoGuard { io }
    }
}

impl Read for HeapIo {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.io.lock().unwrap().read(buf)
    }
}

pub struct IoGuard<'a> {
    io: MutexGuard<'a, Io>,
}

impl Read for IoGuard<'_> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.io.read(buf)
    }
}

impl Write for IoGuard<'_> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.io.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.io.flush()
    }
}

impl Seek for IoGuard<'_> {
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        self.io.seek(pos)
    }
}

enum Io {
    File(Option<File>),
    FileReadBuf(BufReader<File>),
    FileWriteBuf(BufWriter<File>),
    StdIn,
    StdOut,
    StdErr,
    Closed,
}

impl Read for Io {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        match self {
            Io::File(Some(f)) => f.read(buf),
            Io::File(None) => panic!("file is missing a file"),
            Io::FileReadBuf(io) => io.read(buf),
            Io::FileWriteBuf(_) => Err(io::Error::new(
                ErrorKind::Unsupported,
                "read not supported for a write buffer",
            )),
            Io::StdIn => io::stdin().read(buf),
            Io::StdOut => Err(io::Error::new(
                ErrorKind::Unsupported,
                "read not supported for stdout",
            )),
            Io::StdErr => Err(io::Error::new(
                ErrorKind::Unsupported,
                "read not supported for stderr",
            )),
            Io::Closed => Err(io::Error::new(
                ErrorKind::Unsupported,
                "read not supported for closed",
            )),
        }
    }
}

impl Write for Io {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        match self {
            Io::File(Some(f)) => f.write(buf),
            Io::File(None) => panic!("file is missing a file"),
            Io::FileReadBuf(_) => Err(io::Error::new(
                ErrorKind::Unsupported,
                "write not supported for a read buffer",
            )),
            Io::FileWriteBuf(io) => io.write(buf),
            Io::StdIn => Err(io::Error::new(
                ErrorKind::Unsupported,
                "write not supported for stdin",
            )),
            Io::StdOut => io::stdout().write(buf),
            Io::StdErr => io::stderr().write(buf),
            Io::Closed => Err(io::Error::new(
                ErrorKind::Unsupported,
                "write not supported for closed",
            )),
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        match self {
            Io::File(Some(f)) => f.flush(),
            Io::File(None) => panic!("file is missing a file"),
            Io::FileReadBuf(_) => Err(io::Error::new(
                ErrorKind::Unsupported,
                "flush not supported for a read buffer",
            )),
            Io::FileWriteBuf(io) => io.flush(),
            Io::StdIn => Err(io::Error::new(
                ErrorKind::Unsupported,
                "flush not supported for stdin",
            )),
            Io::StdOut => io::stdout().flush(),
            Io::StdErr => io::stderr().flush(),
            Io::Closed => Err(io::Error::new(
                ErrorKind::Unsupported,
                "flush not supported for closed",
            )),
        }
    }
}

impl Seek for Io {
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        match self {
            Io::File(Some(f)) => f.seek(pos),
            Io::File(None) => panic!("file is missing a file"),
            Io::FileReadBuf(io) => io.seek(pos),
            Io::FileWriteBuf(io) => io.seek(pos),
            Io::StdIn => Err(io::Error::new(
                ErrorKind::Unsupported,
                "seek not supported for stdin",
            )),
            Io::StdOut => Err(io::Error::new(
                ErrorKind::Unsupported,
                "seek not supported for stdout",
            )),
            Io::StdErr => Err(io::Error::new(
                ErrorKind::Unsupported,
                "seek not supported for stderr",
            )),
            Io::Closed => Err(io::Error::new(
                ErrorKind::Unsupported,
                "seek not supported for closed",
            )),
        }
    }
}