Skip to content

Commit 742081c

Browse files
retep998seanmonstar
authored andcommitted
fix(rustup): update FromStr
Signed-off-by: Peter Atashian <retep998@gmail.com>
1 parent c983ebf commit 742081c

18 files changed

+90
-79
lines changed

benches/client_mock_tcp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(core, collections, io, test)]
1+
#![feature(collections, io, test)]
22
extern crate hyper;
33

44
extern crate test;

src/header/common/authorization.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ impl<S: Scheme> Header for Authorization<S> {
3232
match (from_utf8(unsafe { &raw[].get_unchecked(0)[] }), Scheme::scheme(None::<S>)) {
3333
(Ok(header), Some(scheme))
3434
if header.starts_with(scheme) && header.len() > scheme.len() + 1 => {
35-
header[scheme.len() + 1..].parse::<S>().map(|s| Authorization(s))
35+
header[scheme.len() + 1..].parse::<S>().map(|s| Authorization(s)).ok()
3636
},
37-
(Ok(header), None) => header.parse::<S>().map(|s| Authorization(s)),
37+
(Ok(header), None) => header.parse::<S>().map(|s| Authorization(s)).ok(),
3838
_ => None
3939
}
4040
} else {
@@ -108,32 +108,33 @@ impl Scheme for Basic {
108108
}
109109

110110
impl FromStr for Basic {
111-
fn from_str(s: &str) -> Option<Basic> {
111+
type Err = ();
112+
fn from_str(s: &str) -> Result<Basic, ()> {
112113
match s.from_base64() {
113114
Ok(decoded) => match String::from_utf8(decoded) {
114115
Ok(text) => {
115116
let mut parts = &mut text[].split(':');
116117
let user = match parts.next() {
117118
Some(part) => part.to_string(),
118-
None => return None
119+
None => return Err(())
119120
};
120121
let password = match parts.next() {
121122
Some(part) => Some(part.to_string()),
122123
None => None
123124
};
124-
Some(Basic {
125+
Ok(Basic {
125126
username: user,
126127
password: password
127128
})
128129
},
129130
Err(e) => {
130131
debug!("Basic::from_utf8 error={:?}", e);
131-
None
132+
Err(())
132133
}
133134
},
134135
Err(e) => {
135136
debug!("Basic::from_base64 error={:?}", e);
136-
None
137+
Err(())
137138
}
138139
}
139140
}

src/header/common/cache_control.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,29 @@ impl fmt::Display for CacheDirective {
9696
}
9797

9898
impl FromStr for CacheDirective {
99-
fn from_str(s: &str) -> Option<CacheDirective> {
99+
type Err = Option<<u32 as FromStr>::Err>;
100+
fn from_str(s: &str) -> Result<CacheDirective, Option<<u32 as FromStr>::Err>> {
100101
use self::CacheDirective::*;
101102
match s {
102-
"no-cache" => Some(NoCache),
103-
"no-store" => Some(NoStore),
104-
"no-transform" => Some(NoTransform),
105-
"only-if-cached" => Some(OnlyIfCached),
106-
"must-revalidate" => Some(MustRevalidate),
107-
"public" => Some(Public),
108-
"private" => Some(Private),
109-
"proxy-revalidate" => Some(ProxyRevalidate),
110-
"" => None,
103+
"no-cache" => Ok(NoCache),
104+
"no-store" => Ok(NoStore),
105+
"no-transform" => Ok(NoTransform),
106+
"only-if-cached" => Ok(OnlyIfCached),
107+
"must-revalidate" => Ok(MustRevalidate),
108+
"public" => Ok(Public),
109+
"private" => Ok(Private),
110+
"proxy-revalidate" => Ok(ProxyRevalidate),
111+
"" => Err(None),
111112
_ => match s.find('=') {
112113
Some(idx) if idx+1 < s.len() => match (&s[..idx], &s[idx+1..].trim_matches('"')) {
113-
("max-age" , secs) => secs.parse().map(MaxAge),
114-
("max-stale", secs) => secs.parse().map(MaxStale),
115-
("min-fresh", secs) => secs.parse().map(MinFresh),
116-
("s-maxage", secs) => secs.parse().map(SMaxAge),
117-
(left, right) => Some(Extension(left.to_string(), Some(right.to_string())))
114+
("max-age" , secs) => secs.parse().map(MaxAge).map_err(|x| Some(x)),
115+
("max-stale", secs) => secs.parse().map(MaxStale).map_err(|x| Some(x)),
116+
("min-fresh", secs) => secs.parse().map(MinFresh).map_err(|x| Some(x)),
117+
("s-maxage", secs) => secs.parse().map(SMaxAge).map_err(|x| Some(x)),
118+
(left, right) => Ok(Extension(left.to_string(), Some(right.to_string())))
118119
},
119-
Some(_) => None,
120-
None => Some(Extension(s.to_string(), None))
120+
Some(_) => Err(None),
121+
None => Ok(Extension(s.to_string(), None))
121122
}
122123
}
123124
}

src/header/common/connection.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ pub enum ConnectionOption {
3131
}
3232

3333
impl FromStr for ConnectionOption {
34-
fn from_str(s: &str) -> Option<ConnectionOption> {
34+
type Err = ();
35+
fn from_str(s: &str) -> Result<ConnectionOption, ()> {
3536
match s {
36-
"keep-alive" => Some(KeepAlive),
37-
"close" => Some(Close),
38-
s => Some(ConnectionHeader(UniCase(s.to_string())))
37+
"keep-alive" => Ok(KeepAlive),
38+
"close" => Ok(Close),
39+
s => Ok(ConnectionHeader(UniCase(s.to_string())))
3940
}
4041
}
4142
}

src/header/common/cookie.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ impl Header for Cookie {
3030
Ok(cookies_str) => {
3131
for cookie_str in cookies_str.split(';') {
3232
match cookie_str.trim().parse() {
33-
Some(cookie) => cookies.push(cookie),
34-
None => return None
33+
Ok(cookie) => cookies.push(cookie),
34+
Err(_) => return None
3535
}
3636
}
3737
},

src/header/common/date.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ impl HeaderFormat for Date {
3535
}
3636

3737
impl FromStr for Date {
38-
fn from_str(s: &str) -> Option<Date> {
39-
tm_from_str(s).map(Date)
38+
type Err = ();
39+
fn from_str(s: &str) -> Result<Date, ()> {
40+
tm_from_str(s).map(Date).ok_or(())
4041
}
4142
}
4243

src/header/common/expires.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ impl HeaderFormat for Expires {
3434
}
3535

3636
impl FromStr for Expires {
37-
fn from_str(s: &str) -> Option<Expires> {
38-
tm_from_str(s).map(Expires)
37+
type Err = ();
38+
fn from_str(s: &str) -> Result<Expires, ()> {
39+
tm_from_str(s).map(Expires).ok_or(())
3940
}
4041
}
4142

src/header/common/host.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Header for Host {
4646
};
4747

4848
let port = match idx {
49-
Some(idx) => s[idx + 1..].parse(),
49+
Some(idx) => s[idx + 1..].parse().ok(),
5050
None => None
5151
};
5252

src/header/common/if_modified_since.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ impl HeaderFormat for IfModifiedSince {
3434
}
3535

3636
impl FromStr for IfModifiedSince {
37-
fn from_str(s: &str) -> Option<IfModifiedSince> {
38-
tm_from_str(s).map(IfModifiedSince)
37+
type Err = ();
38+
fn from_str(s: &str) -> Result<IfModifiedSince, ()> {
39+
tm_from_str(s).map(IfModifiedSince).ok_or(())
3940
}
4041
}
4142

src/header/common/last_modified.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ impl HeaderFormat for LastModified {
3434
}
3535

3636
impl FromStr for LastModified {
37-
fn from_str(s: &str) -> Option<LastModified> {
38-
tm_from_str(s).map(LastModified)
37+
type Err = ();
38+
fn from_str(s: &str) -> Result<LastModified, ()> {
39+
tm_from_str(s).map(LastModified).ok_or(())
3940
}
4041
}
4142

src/header/common/set_cookie.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ impl Header for SetCookie {
2626
match from_utf8(&set_cookies_raw[]) {
2727
Ok(s) if !s.is_empty() => {
2828
match s.parse() {
29-
Some(cookie) => set_cookies.push(cookie),
30-
None => ()
29+
Ok(cookie) => set_cookies.push(cookie),
30+
Err(_) => ()
3131
}
3232
},
3333
_ => ()

src/header/common/upgrade.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ pub enum Protocol {
2222
}
2323

2424
impl FromStr for Protocol {
25-
fn from_str(s: &str) -> Option<Protocol> {
25+
type Err = ();
26+
fn from_str(s: &str) -> Result<Protocol, ()> {
2627
if UniCase(s) == UniCase("websocket") {
27-
Some(WebSocket)
28+
Ok(WebSocket)
2829
}
2930
else {
30-
Some(ProtocolExt(s.to_string()))
31+
Ok(ProtocolExt(s.to_string()))
3132
}
3233
}
3334
}

src/header/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<'a> fmt::Debug for HeaderView<'a> {
349349
}
350350

351351
impl<'a> Extend<HeaderView<'a>> for Headers {
352-
fn extend<I: Iterator<Item=HeaderView<'a>>>(&mut self, mut iter: I) {
352+
fn extend<I: Iterator<Item=HeaderView<'a>>>(&mut self, iter: I) {
353353
for header in iter {
354354
self.data.insert((*header.0).clone(), (*header.1).clone());
355355
}
@@ -571,7 +571,7 @@ mod tests {
571571
}
572572
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
573573
match from_utf8(unsafe { &raw[].get_unchecked(0)[] }) {
574-
Ok(s) => FromStr::from_str(s),
574+
Ok(s) => FromStr::from_str(s).ok(),
575575
Err(_) => None
576576
}.map(|u| CrazyLength(Some(false), u))
577577
}

src/header/parsing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn from_one_raw_str<T: str::FromStr>(raw: &[Vec<u8>]) -> Option<T> {
1212
}
1313
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
1414
match str::from_utf8(&raw[0][]) {
15-
Ok(s) => str::FromStr::from_str(s),
15+
Ok(s) => str::FromStr::from_str(s).ok(),
1616
Err(_) => None
1717
}
1818
}
@@ -34,7 +34,7 @@ pub fn from_one_comma_delimited<T: str::FromStr>(raw: &[u8]) -> Option<Vec<T>> {
3434
Some(s.as_slice()
3535
.split(',')
3636
.map(|x| x.trim())
37-
.filter_map(str::FromStr::from_str)
37+
.filter_map(|x| x.parse().ok())
3838
.collect())
3939
}
4040
Err(_) => None

src/header/shared/encoding.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ impl fmt::Display for Encoding {
3737
}
3838

3939
impl str::FromStr for Encoding {
40-
fn from_str(s: &str) -> Option<Encoding> {
40+
type Err = ();
41+
fn from_str(s: &str) -> Result<Encoding, ()> {
4142
match s {
42-
"chunked" => Some(Chunked),
43-
"deflate" => Some(Deflate),
44-
"gzip" => Some(Gzip),
45-
"compress" => Some(Compress),
46-
"identity" => Some(Identity),
47-
_ => Some(EncodingExt(s.to_string()))
43+
"chunked" => Ok(Chunked),
44+
"deflate" => Ok(Deflate),
45+
"gzip" => Ok(Gzip),
46+
"compress" => Ok(Compress),
47+
"identity" => Ok(Identity),
48+
_ => Ok(EncodingExt(s.to_string()))
4849
}
4950
}
5051
}

src/header/shared/quality_item.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ impl<T: fmt::Display> fmt::Display for QualityItem<T> {
3838
}
3939

4040
impl<T: str::FromStr> str::FromStr for QualityItem<T> {
41-
fn from_str(s: &str) -> Option<Self> {
41+
type Err = ();
42+
fn from_str(s: &str) -> Result<Self, ()> {
4243
// Set defaults used if parsing fails.
4344
let mut raw_item = s;
4445
let mut quality = 1f32;
@@ -49,28 +50,28 @@ impl<T: str::FromStr> str::FromStr for QualityItem<T> {
4950
if start == "q=" || start == "Q=" {
5051
let q_part = &parts[0][2..parts[0].len()];
5152
if q_part.len() > 5 {
52-
return None;
53+
return Err(());
5354
}
54-
let x: Option<f32> = q_part.parse();
55+
let x: Result<f32, _> = q_part.parse();
5556
match x {
56-
Some(q_value) => {
57+
Ok(q_value) => {
5758
if 0f32 <= q_value && q_value <= 1f32 {
5859
quality = q_value;
5960
raw_item = parts[1];
6061
} else {
61-
return None;
62+
return Err(());
6263
}
6364
},
64-
None => return None,
65+
Err(_) => return Err(()),
6566
}
6667
}
6768
}
68-
let x: Option<T> = raw_item.parse();
69+
let x: Result<T, _> = raw_item.parse();
6970
match x {
70-
Some(item) => {
71-
Some(QualityItem{ item: item, quality: quality, })
71+
Ok(item) => {
72+
Ok(QualityItem{ item: item, quality: quality, })
7273
},
73-
None => return None,
74+
Err(_) => return Err(()),
7475
}
7576
}
7677
}
@@ -103,26 +104,26 @@ fn test_quality_item_show3() {
103104

104105
#[test]
105106
fn test_quality_item_from_str1() {
106-
let x: Option<QualityItem<Encoding>> = "chunked".parse();
107+
let x: Result<QualityItem<Encoding>, ()> = "chunked".parse();
107108
assert_eq!(x.unwrap(), QualityItem{ item: Chunked, quality: 1f32, });
108109
}
109110
#[test]
110111
fn test_quality_item_from_str2() {
111-
let x: Option<QualityItem<Encoding>> = "chunked; q=1".parse();
112+
let x: Result<QualityItem<Encoding>, ()> = "chunked; q=1".parse();
112113
assert_eq!(x.unwrap(), QualityItem{ item: Chunked, quality: 1f32, });
113114
}
114115
#[test]
115116
fn test_quality_item_from_str3() {
116-
let x: Option<QualityItem<Encoding>> = "gzip; q=0.5".parse();
117+
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.5".parse();
117118
assert_eq!(x.unwrap(), QualityItem{ item: Gzip, quality: 0.5f32, });
118119
}
119120
#[test]
120121
fn test_quality_item_from_str4() {
121-
let x: Option<QualityItem<Encoding>> = "gzip; q=0.273".parse();
122+
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.273".parse();
122123
assert_eq!(x.unwrap(), QualityItem{ item: Gzip, quality: 0.273f32, });
123124
}
124125
#[test]
125126
fn test_quality_item_from_str5() {
126-
let x: Option<QualityItem<Encoding>> = "gzip; q=0.2739999".parse();
127-
assert_eq!(x, None);
127+
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.2739999".parse();
128+
assert_eq!(x, Err(()));
128129
}

src/http.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::borrow::IntoCow;
44
use std::cmp::min;
55
use std::old_io::{self, Reader, IoResult, BufWriter};
66
use std::num::from_u16;
7-
use std::str::{self, FromStr};
7+
use std::str;
88
use std::string::CowString;
99

1010
use url::Url;
@@ -625,7 +625,7 @@ pub fn read_status<R: Reader>(stream: &mut R) -> HttpResult<RawStatus> {
625625
try!(stream.read_byte()),
626626
];
627627

628-
let code = match str::from_utf8(code.as_slice()).ok().and_then(FromStr::from_str) {
628+
let code = match str::from_utf8(code.as_slice()).ok().and_then(|x| x.parse().ok()) {
629629
Some(num) => num,
630630
None => return Err(HttpStatusError)
631631
};

0 commit comments

Comments
 (0)