@@ -94,6 +94,40 @@ impl<R: Read> BufReader<R> {
94
94
pub fn with_capacity ( capacity : usize , inner : R ) -> BufReader < R > {
95
95
BufReader { inner, buf : Buffer :: with_capacity ( capacity) }
96
96
}
97
+
98
+ /// Attempt to look ahead `n` bytes.
99
+ ///
100
+ /// `n` must be less than `capacity`.
101
+ ///
102
+ /// ## Examples
103
+ ///
104
+ /// ```rust
105
+ /// #![feature(bufreader_peek)]
106
+ /// use std::io::{Read, BufReader};
107
+ ///
108
+ /// let mut bytes = &b"oh, hello"[..];
109
+ /// let mut rdr = BufReader::with_capacity(6, &mut bytes);
110
+ /// assert_eq!(rdr.peek(2).unwrap(), b"oh");
111
+ /// let mut buf = [0; 4];
112
+ /// rdr.read(&mut buf[..]).unwrap();
113
+ /// assert_eq!(&buf, b"oh, ");
114
+ /// assert_eq!(rdr.peek(2).unwrap(), b"he");
115
+ /// let mut s = String::new();
116
+ /// rdr.read_to_string(&mut s).unwrap();
117
+ /// assert_eq!(&s, "hello");
118
+ /// ```
119
+ #[ unstable( feature = "bufreader_peek" , issue = "128405" ) ]
120
+ pub fn peek ( & mut self , n : usize ) -> io:: Result < & [ u8 ] > {
121
+ assert ! ( n <= self . capacity( ) ) ;
122
+ while n > self . buf . buffer ( ) . len ( ) {
123
+ if self . buf . pos ( ) > 0 {
124
+ self . buf . backshift ( ) ;
125
+ }
126
+ self . buf . read_more ( & mut self . inner ) ?;
127
+ debug_assert_eq ! ( self . buf. pos( ) , 0 ) ;
128
+ }
129
+ Ok ( & self . buf . buffer ( ) [ ..n] )
130
+ }
97
131
}
98
132
99
133
impl < R : ?Sized > BufReader < R > {
0 commit comments