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
use std::io;
use std::io::{Error, ErrorKind, Read};
use std::fmt;

use crate::BufferedReader;

/// Always returns EOF.
pub struct EOF<C> {
    cookie: C,
}

impl<C> fmt::Display for EOF<C> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "EOF")
    }
}

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

impl EOF<()> {
    /// Instantiates a new `EOF`.
    pub fn new() -> Self {
        EOF {
            cookie: (),
        }
    }
}

impl<C> EOF<C> {
    /// Instantiates a new `EOF` with a cookie.
    pub fn with_cookie(cookie: C) -> Self {
        EOF {
            cookie: cookie,
        }
    }
}

impl<C> Read for EOF<C> {
    fn read(&mut self, _buf: &mut [u8]) -> Result<usize, io::Error> {
        return Ok(0);
    }
}

impl<C> BufferedReader<C> for EOF<C> {
    fn buffer(&self) -> &[u8] {
        return &b""[..];
    }

    fn data(&mut self, _amount: usize) -> Result<&[u8], io::Error> {
        return Ok(&b""[..]);
    }

    fn consume(&mut self, amount: usize) -> &[u8] {
        assert_eq!(amount, 0);
        return &b""[..];
    }

    fn data_consume(&mut self, _amount: usize) -> Result<&[u8], io::Error> {
        return Ok(&b""[..]);
    }

    fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], io::Error> {
        if amount == 0 {
            Ok(&b""[..])
        } else {
            Err(Error::new(ErrorKind::UnexpectedEof, "unexpected EOF"))
        }
    }

    fn into_inner<'a>(self: Box<Self>) -> Option<Box<dyn BufferedReader<C> + 'a>>
        where Self: 'a
    {
        return None;
    }

    fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<C>>
    {
        return None;
    }

    fn get_ref(&self) -> Option<&dyn BufferedReader<C>>
    {
        return None;
    }


    fn cookie_set(&mut self, cookie: C) -> C {
        use std::mem;

        mem::replace(&mut self.cookie, cookie)
    }

    fn cookie_ref(&self) -> &C {
        &self.cookie
    }

    fn cookie_mut(&mut self) -> &mut C {
        &mut self.cookie
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn basics() {
        let mut reader = EOF::new();

        assert_eq!(reader.buffer(), &b""[..]);
        assert_eq!(reader.data(100).unwrap(), &b""[..]);
        assert_eq!(reader.buffer(), &b""[..]);
        assert_eq!(reader.consume(0), &b""[..]);
        assert_eq!(reader.data_hard(0).unwrap(), &b""[..]);
        assert!(reader.data_hard(1).is_err());
    }
}