Skip to content

Commit dc2118c

Browse files
committed
Allow casting to Response types from Server::respond
1 parent f0981c2 commit dc2118c

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/server.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,30 +302,30 @@ impl<State: Send + Sync + 'static> Server<State> {
302302
/// # #[async_std::main]
303303
/// # async fn main() -> http_types::Result<()> {
304304
/// #
305-
/// use tide::http::{self, Url, Method};
305+
/// use tide::http::{Url, Method, Request, Response};
306306
///
307307
/// // Initialize the application with state.
308308
/// let mut app = tide::new();
309309
/// app.at("/").get(|_| async move { Ok("hello world") });
310310
///
311-
/// let req = http::Request::new(Method::Get, Url::parse("https://example.com")?);
312-
/// let res = app.respond(req).await?;
311+
/// let req = Request::new(Method::Get, Url::parse("https://example.com")?);
312+
/// let res: Response = app.respond(req).await?;
313313
/// assert_eq!(res.status(), 200);
314314
/// #
315315
/// # Ok(()) }
316316
/// ```
317-
pub async fn respond(
318-
&self,
319-
req: impl Into<http_types::Request>,
320-
) -> http_types::Result<http_types::Response> {
317+
pub async fn respond<R>(&self, req: impl Into<http_types::Request>) -> http_types::Result<R>
318+
where
319+
R: From<http_types::Response>,
320+
{
321321
let req = Request::new(self.state.clone(), req.into(), Vec::new());
322322
match self.call(req).await {
323323
Ok(value) => {
324-
let res = value.into();
324+
let res: http_types::Response = value.into();
325325
// We assume that if an error was manually cast to a
326326
// Response that we actually want to send the body to the
327327
// client. At this point we don't scrub the message.
328-
Ok(res)
328+
Ok(res.into())
329329
}
330330
Err(err) => {
331331
let mut res = http_types::Response::new(err.status());
@@ -336,7 +336,7 @@ impl<State: Send + Sync + 'static> Server<State> {
336336
if !res.status().is_server_error() {
337337
res.set_body(err.to_string());
338338
}
339-
Ok(res)
339+
Ok(res.into())
340340
}
341341
}
342342
}

0 commit comments

Comments
 (0)