Skip to content

Commit e7d999c

Browse files
committed
Convert to unboxed closures
See rust-lang/rust#20578
1 parent 435b629 commit e7d999c

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

src/http/client/response.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<S: Stream> ResponseReader<S> {
4242
//let mut b = [0u8, ..4096];
4343
//let len = stream.read(b);
4444
//println!("{}", ::std::str::from_bytes(b[..len.unwrap()]));
45-
let http_version = match read_http_version(&mut stream, |b| b == SP) {
45+
let http_version = match read_http_version(&mut stream, &mut |b| b == SP) {
4646
Ok(nums) => nums,
4747
Err(_) => return Err((request, bad_response_err())),
4848
};

src/http/common.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const ASCII_UPPER_F: u8 = b'F';
4343
*
4444
* Should everything work as designed (i.e. none of these conditions occur) a `Some` is returned.
4545
*/
46-
pub fn read_decimal<R: Reader, N: UnsignedInt + NumCast + Int>
47-
(reader: &mut R, expected_end: |u8| -> bool)
46+
pub fn read_decimal<R: Reader, N: UnsignedInt + NumCast + Int, F: FnMut(u8) -> bool>
47+
(reader: &mut R, expected_end: &mut F)
4848
-> IoResult<N> {
4949
// Here and in `read_hexadecimal` there is the possibility of infinite sequence of zeroes. The
5050
// spec allows this, but it may not be a good thing to allow. It's not a particularly good
@@ -64,8 +64,7 @@ pub fn read_decimal<R: Reader, N: UnsignedInt + NumCast + Int>
6464
None => return Err(bad_input()), // overflow
6565
}
6666
},
67-
Ok(b) if got_content && expected_end(b) => return Ok(n),
68-
Ok(_) => return Err(bad_input()), // not a valid number
67+
Ok(b) => return if got_content && expected_end(b) { Ok(n) } else { Err(bad_input()) }, // not a valid number
6968
Err(err) => return Err(err), // I/O error
7069
};
7170
got_content = true;
@@ -89,8 +88,8 @@ pub fn read_decimal<R: Reader, N: UnsignedInt + NumCast + Int>
8988
*
9089
* Should everything work as designed (i.e. none of these conditions occur) a `Some` is returned.
9190
*/
92-
pub fn read_hexadecimal<R: Reader, N: UnsignedInt + NumCast + Int>
93-
(reader: &mut R, expected_end: |u8| -> bool)
91+
pub fn read_hexadecimal<R: Reader, N: UnsignedInt + NumCast + Int, F: FnMut(u8) -> bool>
92+
(reader: &mut R, expected_end: &mut F)
9493
-> IoResult<N> {
9594
let mut n: N = Int::zero();
9695
let mut got_content = false;
@@ -118,8 +117,7 @@ pub fn read_hexadecimal<R: Reader, N: UnsignedInt + NumCast + Int>
118117
None => return Err(bad_input()), // overflow
119118
}
120119
},
121-
Ok(b) if got_content && expected_end(b) => return Ok(n),
122-
Ok(_) => return Err(bad_input()), // not a valid number
120+
Ok(b) => return if got_content && expected_end(b) { Ok(n) } else { Err(bad_input()) }, // not a valid number
123121
Err(err) => return Err(err), // I/O error
124122
};
125123
got_content = true;
@@ -142,9 +140,9 @@ pub fn read_hexadecimal<R: Reader, N: UnsignedInt + NumCast + Int>
142140
* - A `Some`, if all goes well.
143141
*/
144142
#[inline]
145-
pub fn read_http_version<R: Reader>
146-
(reader: &mut R, expected_end: |u8| -> bool)
147-
-> IoResult<(uint, uint)> {
143+
pub fn read_http_version<R: Reader, F: FnMut(u8) -> bool>
144+
(reader: &mut R, expected_end: &mut F)
145+
-> IoResult<(usize, usize)> {
148146
// I'd read into a [0u8, ..5], but that buffer is not guaranteed to be
149147
// filled, so I must read it byte by byte to guarantee correctness.
150148
// (Sure, no sane person/library would send the first packet with "HTT"
@@ -162,7 +160,7 @@ pub fn read_http_version<R: Reader>
162160
return Err(bad_input());
163161
}
164162

165-
let major = try!(read_decimal(reader, |b| b == b'.'));
163+
let major = try!(read_decimal(reader, &mut |b| b == b'.'));
166164
let minor = try!(read_decimal(reader, expected_end));
167165
Ok((major, minor))
168166
}
@@ -173,7 +171,7 @@ macro_rules! test_reads {
173171
$(
174172
assert_eq!(
175173
concat_idents!(read_, $func)(&mut MemReader::new($value.bytes().collect::<Vec<_>>()),
176-
|b| b == 0).ok(),
174+
&mut |b| b == 0).ok(),
177175
$expected);
178176
)*
179177
}}

src/http/server/request.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a, S: Stream> RequestBuffer<'a, S> {
110110
let mut read_b = 0;
111111

112112
// FIXME: we still have one inconsistency here: this isn't trimming *SP.
113-
match read_http_version(self.stream, |b| { read_b = b; b == CR || b == LF }) {
113+
match read_http_version(self.stream, &mut |b| { read_b = b; b == CR || b == LF }) {
114114
Ok(vv) => {
115115
if read_b == LF || self.stream.read_byte() == Ok(LF) {
116116
Ok((method, request_uri, vv)) // LF or CR LF: valid

0 commit comments

Comments
 (0)