Skip to content

Commit f04d6ec

Browse files
committed
Add Client and Server traits
1 parent 2d8e00b commit f04d6ec

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ edition = "2018"
1313

1414
[features]
1515
default = []
16+
unstable = []
1617
hyperium_http = ["http"]
1718

1819
[dependencies]

src/client.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::fmt::Debug;
2+
use std::future::Future;
3+
use std::pin::Pin;
4+
5+
use crate::{Request, Response, Result};
6+
7+
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>;
8+
9+
/// An HTTP client.
10+
pub trait Client: Debug + Unpin + Send + Sync + Clone + 'static {
11+
/// Send an HTTP request from the client.
12+
fn send_req(&self, req: Request) -> BoxFuture<'static, Result<Response>>;
13+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,25 @@ pub mod headers;
115115
pub mod mime;
116116

117117
mod body;
118+
mod client;
118119
mod error;
119120
mod macros;
120121
mod method;
121122
mod request;
122123
mod response;
124+
mod server;
123125
mod status;
124126
mod status_code;
125127
mod type_map;
126128
mod version;
127129

128130
pub use body::Body;
131+
pub use client::Client;
129132
pub use error::{Error, Result};
130133
pub use method::Method;
131134
pub use request::Request;
132135
pub use response::Response;
136+
pub use server::Server;
133137
pub use status::Status;
134138
pub use status_code::StatusCode;
135139
pub use version::Version;

src/request.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use async_std::io::{self, BufRead, Read};
22
use async_std::sync;
33

44
use std::convert::TryInto;
5+
use std::future::Future;
56
use std::mem;
67
use std::pin::Pin;
78
use std::task::{Context, Poll};
@@ -13,6 +14,9 @@ use crate::mime::Mime;
1314
use crate::trailers::{Trailers, TrailersSender};
1415
use crate::Cookie;
1516
use crate::{Body, Method, TypeMap, Url, Version};
17+
use crate::{Client, Error, Response, Server};
18+
19+
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>;
1620

1721
pin_project_lite::pin_project! {
1822
/// An HTTP request.
@@ -439,6 +443,22 @@ impl Request {
439443
self.receiver.recv().await
440444
}
441445

446+
/// Send a request directly to a server.
447+
///
448+
/// This is useful for sending a request to a server without needing to
449+
/// make any further HTTP requests. Examples include: HTTP endpoints in
450+
/// frameworks, or testing logic for requests.
451+
pub fn send_to<S: Server>(self, server: &S) -> BoxFuture<'static, Result<Response, Error>> {
452+
server.recv_req(self)
453+
}
454+
455+
/// Send a request through a client.
456+
///
457+
/// This will most likely create an HTTP request over the network.
458+
pub fn send_from<C: Client>(self, client: &C) -> BoxFuture<'static, Result<Response, Error>> {
459+
client.send_req(self)
460+
}
461+
442462
/// An iterator visiting all header pairs in arbitrary order.
443463
pub fn iter<'a>(&'a self) -> headers::Iter<'a> {
444464
self.headers.iter()

src/server.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::fmt::Debug;
2+
use std::future::Future;
3+
use std::pin::Pin;
4+
5+
use crate::{Request, Response, Result};
6+
7+
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>;
8+
9+
/// An HTTP server.
10+
pub trait Server: Debug + Unpin + Send + Sync + Clone + 'static {
11+
/// Receive an HTTP request on the server.
12+
fn recv_req(&self, req: Request) -> BoxFuture<'static, Result<Response>>;
13+
}

0 commit comments

Comments
 (0)