-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): introduce
Accept
trait
The `Accept` trait is used by the server types to asynchronously accept incoming connections. This replaces the previous usage of `Stream`. BREAKING CHANGE: Passing a `Stream` to `Server::builder` or `Http::serve_incoming` must be changed to pass an `Accept` instead. The `stream` optional feature can be enabled, and the a stream can be converted using `hyper::server::accept::from_stream`.
- Loading branch information
1 parent
0867ad5
commit b3e5506
Showing
7 changed files
with
189 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//! The `Accept` trait and supporting types. | ||
//! | ||
//! This module contains: | ||
//! | ||
//! - The [`Accept`](Accept) trait used to asynchronously accept incoming | ||
//! connections. | ||
//! - Utilities like `poll_fn` to ease creating a custom `Accept`. | ||
#[cfg(feature = "stream")] | ||
use futures_core::Stream; | ||
|
||
use crate::common::{Pin, task::{self, Poll}}; | ||
|
||
/// Asynchronously accept incoming connections. | ||
pub trait Accept { | ||
/// The connection type that can be accepted. | ||
type Conn; | ||
/// The error type that can occur when accepting a connection. | ||
type Error; | ||
|
||
/// Poll to accept the next connection. | ||
fn poll_accept(self: Pin<&mut Self>, cx: &mut task::Context<'_>) | ||
-> Poll<Option<Result<Self::Conn, Self::Error>>>; | ||
} | ||
|
||
/// Create an `Accept` with a polling function. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use std::task::Poll; | ||
/// use hyper::server::{accept, Server}; | ||
/// | ||
/// # let mock_conn = (); | ||
/// // If we created some mocked connection... | ||
/// let mut conn = Some(mock_conn); | ||
/// | ||
/// // And accept just the mocked conn once... | ||
/// let once = accept::poll_fn(move |cx| { | ||
/// Poll::Ready(conn.take().map(Ok::<_, ()>)) | ||
/// }); | ||
/// | ||
/// let builder = Server::builder(once); | ||
/// ``` | ||
pub fn poll_fn<F, IO, E>(func: F) -> impl Accept<Conn = IO, Error = E> | ||
where | ||
F: FnMut(&mut task::Context<'_>) -> Poll<Option<Result<IO, E>>>, | ||
{ | ||
struct PollFn<F>(F); | ||
|
||
impl<F, IO, E> Accept for PollFn<F> | ||
where | ||
F: FnMut(&mut task::Context<'_>) -> Poll<Option<Result<IO, E>>>, | ||
{ | ||
type Conn = IO; | ||
type Error = E; | ||
fn poll_accept(self: Pin<&mut Self>, cx: &mut task::Context<'_>) | ||
-> Poll<Option<Result<Self::Conn, Self::Error>>> | ||
{ | ||
unsafe { | ||
(self.get_unchecked_mut().0)(cx) | ||
} | ||
} | ||
} | ||
|
||
PollFn(func) | ||
} | ||
|
||
/// Adapt a `Stream` of incoming connections into an `Accept`. | ||
/// | ||
/// # Unstable | ||
/// | ||
/// This function requires enabling the unstable `stream` feature in your | ||
/// `Cargo.toml`. | ||
#[cfg(feature = "stream")] | ||
pub fn from_stream<S, IO, E>(stream: S) -> impl Accept<Conn = IO, Error = E> | ||
where | ||
S: Stream<Item = Result<IO, E>>, | ||
{ | ||
struct FromStream<S>(S); | ||
|
||
impl<S, IO, E> Accept for FromStream<S> | ||
where | ||
S: Stream<Item = Result<IO, E>>, | ||
{ | ||
type Conn = IO; | ||
type Error = E; | ||
fn poll_accept(self: Pin<&mut Self>, cx: &mut task::Context<'_>) | ||
-> Poll<Option<Result<Self::Conn, Self::Error>>> | ||
{ | ||
unsafe { | ||
Pin::new_unchecked(&mut self.get_unchecked_mut().0) | ||
.poll_next(cx) | ||
} | ||
} | ||
} | ||
|
||
FromStream(stream) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.