Skip to content
Open
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
12 changes: 6 additions & 6 deletions examples/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn param_example(
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(MISSING))
.unwrap());
.expect("missing should be valid"));
Copy link
Author

Choose a reason for hiding this comment

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

We can also do:

                let mut response = Response::new(full(MISSING));
                *response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY;
                return Ok(response);

I am not sure which one is actually better.

};
let number = if let Some(n) = params.get("number") {
if let Ok(v) = n.parse::<f64>() {
Expand All @@ -61,13 +61,13 @@ async fn param_example(
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(NOTNUMERIC))
.unwrap());
.expect("notnumeric should be valid"));
}
} else {
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(MISSING))
.unwrap());
.expect("missing should be valid"));
};

// Render the response. This will often involve
Expand All @@ -86,7 +86,7 @@ async fn param_example(
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(MISSING))
.unwrap());
.expect("missing should be valid"));
};
let params = form_urlencoded::parse(query.as_bytes())
.into_owned()
Expand All @@ -97,15 +97,15 @@ async fn param_example(
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(MISSING))
.unwrap());
.expect("missing should be valid"));
};
let body = format!("You requested {}", page);
Ok(Response::new(full(body)))
}
_ => Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(empty())
.unwrap()),
.expect("not found should be valid")),
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/send_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn not_found() -> Response<BoxBody<Bytes, std::io::Error>> {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Full::new(NOTFOUND.into()).map_err(|e| match e {}).boxed())
.unwrap()
.expect("not found should be valid")
}

async fn simple_file_send(filename: &str) -> Result<Response<BoxBody<Bytes, std::io::Error>>> {
Expand All @@ -85,7 +85,7 @@ async fn simple_file_send(filename: &str) -> Result<Response<BoxBody<Bytes, std:
let response = Response::builder()
.status(StatusCode::OK)
.body(boxed_body)
.unwrap();
.expect("response should be valid");

Ok(response)
}
2 changes: 1 addition & 1 deletion examples/service_struct_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Service<Request<IncomingBody>> for Svc {

fn call(&self, req: Request<IncomingBody>) -> Self::Future {
fn mk_response(s: String) -> Result<Response<Full<Bytes>>, hyper::Error> {
Ok(Response::builder().body(Full::new(Bytes::from(s))).unwrap())
Ok(Response::new(Full::new(Bytes::from(s))))
}

if req.uri().path() != "/favicon.ico" {
Expand Down
2 changes: 1 addition & 1 deletion examples/upgrades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async fn client_upgrade_request(addr: SocketAddr) -> Result<()> {
.uri(format!("http://{}/", addr))
.header(UPGRADE, "foobar")
.body(Empty::<Bytes>::new())
.unwrap();
.expect("request should be valid");

let stream = TcpStream::connect(addr).await?;
let io = TokioIo::new(stream);
Expand Down
8 changes: 4 additions & 4 deletions examples/web_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn client_request_response() -> Result<Response<BoxBody>> {
.uri(URL)
.header(header::CONTENT_TYPE, "application/json")
.body(Full::new(Bytes::from(POST_DATA)))
.unwrap();
.expect("post data should be valid");

let host = req.uri().host().expect("uri has no host");
let port = req.uri().port_u16().expect("uri has no port");
Expand Down Expand Up @@ -73,11 +73,11 @@ async fn api_get_response() -> Result<Response<BoxBody>> {
Ok(json) => Response::builder()
.header(header::CONTENT_TYPE, "application/json")
.body(full(json))
.unwrap(),
.expect("json response should be valid"),
Err(_) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(full(INTERNAL_SERVER_ERROR))
.unwrap(),
.expect("internal server error response should be valid"),
};
Ok(res)
}
Expand All @@ -93,7 +93,7 @@ async fn response_examples(req: Request<IncomingBody>) -> Result<Response<BoxBod
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(full(NOTFOUND))
.unwrap())
.expect("not found response should be valid"))
}
}
}
Expand Down