angsd_io_core/lib.rs
1use std::io;
2
3/// A read status.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum ReadStatus {
6 /// The operation did not read any data from the underlying reader, and
7 /// reading has finished.
8 Done,
9 /// The operation read data from the underlying reader, and reading may
10 /// not be finished.
11 NotDone,
12}
13
14impl ReadStatus {
15 /// Returns the read status of a reader.
16 ///
17 /// # Returns
18 ///
19 /// Returns [`ReadStatus::Done`] if no more data remains to be read in the
20 /// reader, otherwise returns [`ReadStatus::NotDone`] if any data remains.
21 /// The reader may attempt to fill the underlying buffer to check for more
22 /// data. An error in this processed is returned.
23 pub fn check<R>(reader: &mut R) -> io::Result<Self>
24 where
25 R: io::BufRead,
26 {
27 // TODO: This can use io::BufRead::has_data_left if/when it stabilizes, see
28 // tracking issue github.com/rust-lang/rust/issues/86423
29 reader.fill_buf().map(|b| match b.is_empty() {
30 true => Self::Done,
31 false => Self::NotDone,
32 })
33 }
34
35 /// Returns `true` if read status is [`ReadStatus::Done`].
36 pub fn is_done(&self) -> bool {
37 matches!(self, Self::Done)
38 }
39
40 /// Returns `true` if read status is [`ReadStatus::NotDone`].
41 pub fn is_not_done(&self) -> bool {
42 matches!(self, Self::NotDone)
43 }
44}