Skip to content

Commit 0f0b6ed

Browse files
authored
docs(examples): replace unwraps with expects explaining why (#4001)
Just avoid unwrap as much as possible. Use expect to assert that it will always succeed. Closes #3984
1 parent 79d40f3 commit 0f0b6ed

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

examples/params.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async fn param_example(
5252
return Ok(Response::builder()
5353
.status(StatusCode::UNPROCESSABLE_ENTITY)
5454
.body(full(MISSING))
55-
.unwrap());
55+
.expect("constant status won't error"));
5656
};
5757
let number = if let Some(n) = params.get("number") {
5858
if let Ok(v) = n.parse::<f64>() {
@@ -61,13 +61,13 @@ async fn param_example(
6161
return Ok(Response::builder()
6262
.status(StatusCode::UNPROCESSABLE_ENTITY)
6363
.body(full(NOTNUMERIC))
64-
.unwrap());
64+
.expect("constant status won't error"));
6565
}
6666
} else {
6767
return Ok(Response::builder()
6868
.status(StatusCode::UNPROCESSABLE_ENTITY)
6969
.body(full(MISSING))
70-
.unwrap());
70+
.expect("constant status won't error"));
7171
};
7272

7373
// Render the response. This will often involve
@@ -86,7 +86,7 @@ async fn param_example(
8686
return Ok(Response::builder()
8787
.status(StatusCode::UNPROCESSABLE_ENTITY)
8888
.body(full(MISSING))
89-
.unwrap());
89+
.expect("constant status won't error"));
9090
};
9191
let params = form_urlencoded::parse(query.as_bytes())
9292
.into_owned()
@@ -97,15 +97,15 @@ async fn param_example(
9797
return Ok(Response::builder()
9898
.status(StatusCode::UNPROCESSABLE_ENTITY)
9999
.body(full(MISSING))
100-
.unwrap());
100+
.expect("constant status won't error"));
101101
};
102102
let body = format!("You requested {}", page);
103103
Ok(Response::new(full(body)))
104104
}
105105
_ => Ok(Response::builder()
106106
.status(StatusCode::NOT_FOUND)
107107
.body(empty())
108-
.unwrap()),
108+
.expect("constant status won't error")),
109109
}
110110
}
111111

examples/send_file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn not_found() -> Response<BoxBody<Bytes, std::io::Error>> {
6161
Response::builder()
6262
.status(StatusCode::NOT_FOUND)
6363
.body(Full::new(NOTFOUND.into()).map_err(|e| match e {}).boxed())
64-
.unwrap()
64+
.expect("constant status won't error")
6565
}
6666

6767
async fn simple_file_send(filename: &str) -> Result<Response<BoxBody<Bytes, std::io::Error>>> {
@@ -85,7 +85,7 @@ async fn simple_file_send(filename: &str) -> Result<Response<BoxBody<Bytes, std:
8585
let response = Response::builder()
8686
.status(StatusCode::OK)
8787
.body(boxed_body)
88-
.unwrap();
88+
.expect("constant status won't error");
8989

9090
Ok(response)
9191
}

examples/service_struct_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Service<Request<IncomingBody>> for Svc {
5151

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

5757
if req.uri().path() != "/favicon.ico" {

examples/upgrades.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async fn client_upgrade_request(addr: SocketAddr) -> Result<()> {
100100
.uri(format!("http://{}/", addr))
101101
.header(UPGRADE, "foobar")
102102
.body(Empty::<Bytes>::new())
103-
.unwrap();
103+
.expect("uri/header parse won't error");
104104

105105
let stream = TcpStream::connect(addr).await?;
106106
let io = TokioIo::new(stream);

examples/web_api.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async fn client_request_response() -> Result<Response<BoxBody>> {
2929
.uri(URL)
3030
.header(header::CONTENT_TYPE, "application/json")
3131
.body(Full::new(Bytes::from(POST_DATA)))
32-
.unwrap();
32+
.expect("uri/header parse from constants won't error");
3333

3434
let host = req.uri().host().expect("uri has no host");
3535
let port = req.uri().port_u16().expect("uri has no port");
@@ -73,11 +73,11 @@ async fn api_get_response() -> Result<Response<BoxBody>> {
7373
Ok(json) => Response::builder()
7474
.header(header::CONTENT_TYPE, "application/json")
7575
.body(full(json))
76-
.unwrap(),
76+
.expect("header parse from constant won't error"),
7777
Err(_) => Response::builder()
7878
.status(StatusCode::INTERNAL_SERVER_ERROR)
7979
.body(full(INTERNAL_SERVER_ERROR))
80-
.unwrap(),
80+
.expect("constant status won't error"),
8181
};
8282
Ok(res)
8383
}
@@ -93,7 +93,7 @@ async fn response_examples(req: Request<IncomingBody>) -> Result<Response<BoxBod
9393
Ok(Response::builder()
9494
.status(StatusCode::NOT_FOUND)
9595
.body(full(NOTFOUND))
96-
.unwrap())
96+
.expect("constant status won't error"))
9797
}
9898
}
9999
}

0 commit comments

Comments
 (0)