Skip to content

Commit 353f9c8

Browse files
authored
Merge pull request #463 from http-rs/remove-extra-trait
Remove IntoResponse trait
2 parents ee4a707 + 4fafbee commit 353f9c8

File tree

8 files changed

+44
-128
lines changed

8 files changed

+44
-128
lines changed

examples/nested.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ async fn main() -> Result<(), std::io::Error> {
55
app.at("/api").nest({
66
let mut api = tide::new();
77
api.at("/hello").get(|_| async move { Ok("Hello, world") });
8-
api.at("/goodbye").get(|_| async move { Ok("Goodbye, world") });
8+
api.at("/goodbye")
9+
.get(|_| async move { Ok("Goodbye, world") });
910
api
1011
});
1112
app.listen("127.0.0.1:8080").await?;

src/endpoint.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
use crate::response::IntoResponse;
21
use async_std::future::Future;
32
use async_std::sync::Arc;
43
use http_types::Result;
54

65
use crate::middleware::Next;
76
use crate::utils::BoxFuture;
8-
use crate::{Middleware, Request};
7+
use crate::{Middleware, Request, Response};
98

109
/// An HTTP request handler.
1110
///
1211
/// This trait is automatically implemented for `Fn` types, and so is rarely implemented
1312
/// directly by Tide users.
1413
///
1514
/// In practice, endpoints are functions that take a `Request<State>` as an argument and
16-
/// return a type `T` that implements [`IntoResponse`].
15+
/// return a type `T` that implements `Into<Response>`.
1716
///
1817
/// # Examples
1918
///
@@ -60,13 +59,13 @@ impl<State, F: Send + Sync + 'static, Fut, Res> Endpoint<State> for F
6059
where
6160
F: Fn(Request<State>) -> Fut,
6261
Fut: Future<Output = Result<Res>> + Send + 'static,
63-
Res: IntoResponse,
62+
Res: Into<Response>,
6463
{
6564
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, crate::Result> {
6665
let fut = (self)(req);
6766
Box::pin(async move {
6867
let res = fut.await?;
69-
Ok(res.into_response())
68+
Ok(res.into())
7069
})
7170
}
7271
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub use http_types::{Body, Error, Status, StatusCode};
209209
#[doc(inline)]
210210
pub use middleware::{Middleware, Next};
211211
#[doc(inline)]
212-
pub use response::{IntoResponse, Response};
212+
pub use response::Response;
213213
#[doc(inline)]
214214
pub use server::{Route, Server};
215215

src/middleware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::Request;
1515
// pub use compression::{Compression, Decompression};
1616
// pub use default_headers::DefaultHeaders;
1717

18-
/// Middleware that wraps around remaining middleware chain.
18+
/// Middleware that wraps around the remaining middleware chain.
1919
pub trait Middleware<State>: 'static + Send + Sync {
2020
/// Asynchronously handle the request, and return a response.
2121
fn handle<'a>(

src/request.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ use http_types::{
66
use route_recognizer::Params;
77
use serde::Deserialize;
88

9-
use async_std::io::{self, prelude::*};
9+
use async_std::io::{self, prelude::*, BufReader};
1010
use async_std::task::{Context, Poll};
1111

1212
use std::pin::Pin;
1313
use std::{str::FromStr, sync::Arc};
1414

1515
use crate::cookies::CookieData;
16+
use crate::Response;
1617

1718
/// An HTTP request.
1819
///
@@ -319,6 +320,14 @@ impl<State> Read for Request<State> {
319320
}
320321
}
321322

323+
// NOTE: From cannot be implemented for this conversion because `State` needs to
324+
// be constrained by a type.
325+
impl<State: Send + Sync + 'static> Into<Response> for Request<State> {
326+
fn into(self) -> Response {
327+
Response::new(StatusCode::Ok).body(BufReader::new(self))
328+
}
329+
}
330+
322331
impl<State> IntoIterator for Request<State> {
323332
type Item = (HeaderName, Vec<HeaderValue>);
324333
type IntoIter = http_types::headers::IntoIter;

src/response/mod.rs renamed to src/response.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ use http_types::{
1010
use mime::Mime;
1111
use serde::Serialize;
1212

13-
pub use into_response::IntoResponse;
14-
15-
mod into_response;
16-
1713
#[derive(Debug)]
1814
pub(crate) enum CookieEvent {
1915
Added(Cookie<'static>),
@@ -245,14 +241,12 @@ impl Response {
245241
}
246242
}
247243

248-
#[doc(hidden)]
249244
impl Into<http_service::Response> for Response {
250245
fn into(self) -> http_service::Response {
251246
self.res
252247
}
253248
}
254249

255-
#[doc(hidden)]
256250
impl From<http_service::Response> for Response {
257251
fn from(res: http_service::Response) -> Self {
258252
Self {
@@ -262,6 +256,30 @@ impl From<http_service::Response> for Response {
262256
}
263257
}
264258

259+
impl From<String> for Response {
260+
fn from(s: String) -> Self {
261+
let mut res = http_types::Response::new(StatusCode::Ok);
262+
res.set_content_type(http_types::mime::PLAIN);
263+
res.set_body(s);
264+
Self {
265+
res,
266+
cookie_events: vec![],
267+
}
268+
}
269+
}
270+
271+
impl<'a> From<&'a str> for Response {
272+
fn from(s: &'a str) -> Self {
273+
let mut res = http_types::Response::new(StatusCode::Ok);
274+
res.set_content_type(http_types::mime::PLAIN);
275+
res.set_body(String::from(s));
276+
Self {
277+
res,
278+
cookie_events: vec![],
279+
}
280+
}
281+
}
282+
265283
impl IntoIterator for Response {
266284
type Item = (HeaderName, Vec<HeaderValue>);
267285
type IntoIter = http_types::headers::IntoIter;

src/response/into_response.rs

Lines changed: 0 additions & 111 deletions
This file was deleted.

tests/querystring.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use async_std::prelude::*;
22
use futures::executor::block_on;
33
use http_service_mock::{make_server, TestBackend};
44
use serde::Deserialize;
5-
use tide::{IntoResponse, Request, Response, Server, StatusCode};
5+
use tide::{Request, Response, Server, StatusCode};
66

77
#[derive(Deserialize)]
88
struct Params {
@@ -18,7 +18,7 @@ struct OptionalParams {
1818
async fn handler(cx: Request<()>) -> tide::Result {
1919
let p = cx.query::<Params>();
2020
match p {
21-
Ok(params) => Ok(params.msg.into_response()),
21+
Ok(params) => Ok(params.msg.into()),
2222
Err(error) => Ok(err_to_res(error)),
2323
}
2424
}

0 commit comments

Comments
 (0)