Skip to content

Commit 0a51365

Browse files
committed
Allow conversions in Request,Response constructors
We're waiting on servo/rust-url#569 to be published for URL shorthands to be practical; but once it lands it should be really nice to use
1 parent 30f982a commit 0a51365

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ cookie = "0.12.0"
3535
infer = "0.1.2"
3636
omnom = "2.1.1"
3737
pin-project-lite = "0.1.0"
38-
url = "2.1.0"
38+
url = "2.1.1"
3939
serde_json = "1.0.51"
4040
serde = { version = "1.0.106", features = ["derive"] }
4141
serde_urlencoded = "0.6.1"

src/hyperium_http.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ impl From<Request> for http::Request<Body> {
120120
impl From<http::Response<Body>> for Response {
121121
fn from(res: http::Response<Body>) -> Self {
122122
let (parts, body) = res.into_parts();
123-
let status = parts.status.into();
124-
let mut res = Response::new(status);
123+
let mut res = Response::new(parts.status);
125124
res.set_body(body);
126125
res.set_version(Some(parts.version.into()));
127126
hyperium_headers_to_headers(parts.headers, res.as_mut());

src/request.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use async_std::io::{self, BufRead, Read};
22
use async_std::sync;
33

4-
use std::convert::Into;
4+
use std::convert::{Into, TryInto};
55
use std::mem;
66
use std::ops::Index;
77
use std::pin::Pin;
@@ -45,7 +45,12 @@ pin_project_lite::pin_project! {
4545

4646
impl Request {
4747
/// Create a new request.
48-
pub fn new(method: Method, url: Url) -> Self {
48+
pub fn new<U>(method: Method, url: U) -> Self
49+
where
50+
U: TryInto<Url>,
51+
U::Error: std::fmt::Debug,
52+
{
53+
let url = url.try_into().expect("Could not convert into a valid url");
4954
let (sender, receiver) = sync::channel(1);
5055
Self {
5156
method,
@@ -687,7 +692,8 @@ mod tests {
687692
use super::*;
688693

689694
fn build_test_request() -> Request {
690-
Request::new(Method::Get, "http://irrelevant/".parse().unwrap())
695+
let url = Url::parse("http://irrelevant/").unwrap();
696+
Request::new(Method::Get, url)
691697
}
692698

693699
fn set_x_forwarded_for(request: &mut Request, client: &'static str) {

src/response.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use async_std::io::{self, BufRead, Read};
22
use async_std::sync;
33

4-
use std::convert::Into;
4+
use std::convert::{Into, TryInto};
5+
use std::fmt::Debug;
56
use std::mem;
67
use std::ops::Index;
78
use std::pin::Pin;
@@ -48,7 +49,14 @@ pin_project_lite::pin_project! {
4849

4950
impl Response {
5051
/// Create a new response.
51-
pub fn new(status: StatusCode) -> Self {
52+
pub fn new<S>(status: S) -> Self
53+
where
54+
S: TryInto<StatusCode>,
55+
S::Error: Debug,
56+
{
57+
let status = status
58+
.try_into()
59+
.expect("Could not convert into a valid `StatusCode`");
5260
let (sender, receiver) = sync::channel(1);
5361
Self {
5462
status,

src/trailers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Trailers {
121121
}
122122

123123
/// Get a reference to a header.
124-
pub fn get(&self, name: &impl Into<HeaderName>) -> Option<&HeaderValues> {
124+
pub fn get(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues> {
125125
self.headers.get(name)
126126
}
127127

0 commit comments

Comments
 (0)