Skip to content

Commit 22a80a9

Browse files
committed
Merge pull request #292 from hyperium/rustup
fix(rustup): update FromStr
2 parents eee4625 + 742081c commit 22a80a9

18 files changed

+90
-79
lines changed

benches/client_mock_tcp.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 7 deletions
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

Lines changed: 18 additions & 17 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

0 commit comments

Comments
 (0)