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
use std::fs::File;
use std::io::{
    self, BufRead, BufReader, Chain, Cursor, Empty, Read, Repeat, Seek, SeekFrom, StdinLock, Take,
};
use std::net::TcpStream;
#[cfg(unix)]
use std::os::unix::net::UnixStream;

/// A trait providing the `peek` function for reading without consuming.
///
/// Many common `Read` implementations have `Peek` implementations, however
/// [`Stdin`], [`ChildStderr`], [`ChildStdout`], [`PipeReader`], and
/// [`CharDevice`] do not, since they are unbuffered and pipes and character
/// devices don't support any form of peeking.
///
/// [`Stdin`]: std::io::Stdin
/// [`ChildStdout`]: std::process::ChildStdout
/// [`ChildStderr`]: std::process::ChildStderr
/// [`PipeReader`]: https://docs.rs/os_pipe/latest/os_pipe/struct.PipeReader.html
/// [`CharDevice`]: https://docs.rs/char-device/latest/char_device/struct.CharDevice.html
pub trait Peek {
    /// Reads data from a stream without consuming it; subsequent reads will
    /// re-read the data. May return fewer bytes than requested; `Ok(0)`
    /// indicates that seeking is not possible (but reading may still be).
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize>;
}

impl Peek for File {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let pos = self.seek(SeekFrom::Current(0))?;
        crate::fs::FileIoExt::read_at(self, buf, pos)
    }
}

#[cfg(feature = "cap_std_impls")]
impl Peek for cap_std::fs::File {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let pos = self.seek(SeekFrom::Current(0))?;
        crate::fs::FileIoExt::read_at(self, buf, pos)
    }
}

#[cfg(feature = "cap_std_impls_fs_utf8")]
impl Peek for cap_std::fs_utf8::File {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let pos = self.seek(SeekFrom::Current(0))?;
        crate::fs::FileIoExt::read_at(self, buf, pos)
    }
}

impl Peek for TcpStream {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        TcpStream::peek(self, buf)
    }
}

#[cfg(unix)]
impl Peek for UnixStream {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        #[cfg(unix_socket_peek)]
        {
            UnixStream::peek(self, buf)
        }

        #[cfg(not(unix_socket_peek))]
        {
            // Return the conservatively correct value.
            let _ = buf;
            Ok(0)
        }
    }
}

impl Peek for Repeat {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        // Repeat just reads the same byte repeatedly, so we can just read
        // from it.
        self.read(buf)
    }
}

#[cfg(feature = "socketpair")]
impl Peek for socketpair::SocketpairStream {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        socketpair::SocketpairStream::peek(self, buf)
    }
}

impl Peek for Empty {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

impl<'a> Peek for &'a [u8] {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

impl<'a> Peek for StdinLock<'a> {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

impl<B: BufRead + ?Sized> Peek for Box<B> {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

impl<'a, B: BufRead + ?Sized> Peek for &'a mut B {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

impl<R: Read> Peek for BufReader<R> {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

impl<T> Peek for Cursor<T>
where
    T: AsRef<[u8]>,
{
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

impl<T: BufRead> Peek for Take<T> {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

impl<T: BufRead, U: BufRead> Peek for Chain<T, U> {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        peek_from_bufread(self, buf)
    }
}

/// Implement `peek` for types that implement `BufRead`.
#[inline]
pub fn peek_from_bufread<BR: BufRead>(buf_read: &mut BR, buf: &mut [u8]) -> io::Result<usize> {
    // Call `fill_buf` to read the bytes, but don't call `consume`.
    Read::read(&mut buf_read.fill_buf()?, buf)
}

#[cfg(feature = "socket2")]
impl Peek for socket2::Socket {
    #[inline]
    fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        use io_lifetimes::AsSocketlike;
        self.as_socketlike_view::<std::net::TcpStream>().peek(buf)
    }
}