Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more From conversions #101

Merged
merged 5 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ impl From<Vec<u8>> for Body {
}
}

impl<'a> From<&'a [u8]> for Body {
fn from(b: &'a [u8]) -> Self {
Self {
length: Some(b.len()),
reader: Box::new(io::Cursor::new(b.to_owned())),
mime: mime::BYTE_STREAM,
}
}
}

impl Read for Body {
#[allow(missing_doc_code_examples)]
fn poll_read(
Expand Down
3 changes: 2 additions & 1 deletion src/hyperium_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl From<Response> for http::Response<Body> {
.status(status)
.version(version);
headers_to_hyperium_headers(res.as_mut(), builder.headers_mut().unwrap());
builder.body(res.into()).unwrap()
let body = res.take_body();
builder.body(body).unwrap()
}
}
32 changes: 13 additions & 19 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,32 +491,26 @@ impl AsMut<Headers> for Response {
}
}

impl From<Response> for Body {
fn from(res: Response) -> Body {
res.body
impl From<()> for Response {
fn from(_: ()) -> Self {
Response::new(StatusCode::NoContent)
}
}

impl From<String> for Response {
fn from(s: String) -> Self {
let mut res = Response::new(StatusCode::Ok);
res.set_body(s);
res
}
}

impl<'a> From<&'a str> for Response {
fn from(s: &'a str) -> Self {
let mut res = Response::new(StatusCode::Ok);
res.set_body(s);
res
impl From<StatusCode> for Response {
fn from(s: StatusCode) -> Self {
Response::new(s)
}
}

impl From<Vec<u8>> for Response {
fn from(b: Vec<u8>) -> Self {
impl<T> From<T> for Response
where
T: Into<Body>,
{
fn from(value: T) -> Self {
let body: Body = value.into();
let mut res = Response::new(StatusCode::Ok);
res.set_body(b);
res.set_body(body);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor tweak, but this could be

impl<T> From<T> for Response
where
    T: Into<Body>,
{
    fn from(value: T) -> Self {
        let mut res = Response::new(StatusCode::Ok);
        res.set_body(value);
        res
    }
}

since replace_body does the .into::<Body>()

meta: is this sort of review useful?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbr thanks heaps for suggesting this; yeah this is very useful!

res
}
}
Expand Down