hyper/server/
accept.rs
1#[cfg(feature = "stream")]
10use futures_core::Stream;
11#[cfg(feature = "stream")]
12use pin_project_lite::pin_project;
13
14use crate::common::{
15 task::{self, Poll},
16 Pin,
17};
18
19pub trait Accept {
21 type Conn;
23 type Error;
25
26 fn poll_accept(
28 self: Pin<&mut Self>,
29 cx: &mut task::Context<'_>,
30 ) -> Poll<Option<Result<Self::Conn, Self::Error>>>;
31}
32
33pub fn poll_fn<F, IO, E>(func: F) -> impl Accept<Conn = IO, Error = E>
53where
54 F: FnMut(&mut task::Context<'_>) -> Poll<Option<Result<IO, E>>>,
55{
56 struct PollFn<F>(F);
57
58 impl<F> Unpin for PollFn<F> {}
60
61 impl<F, IO, E> Accept for PollFn<F>
62 where
63 F: FnMut(&mut task::Context<'_>) -> Poll<Option<Result<IO, E>>>,
64 {
65 type Conn = IO;
66 type Error = E;
67 fn poll_accept(
68 self: Pin<&mut Self>,
69 cx: &mut task::Context<'_>,
70 ) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
71 (self.get_mut().0)(cx)
72 }
73 }
74
75 PollFn(func)
76}
77
78#[cfg(feature = "stream")]
85pub fn from_stream<S, IO, E>(stream: S) -> impl Accept<Conn = IO, Error = E>
86where
87 S: Stream<Item = Result<IO, E>>,
88{
89 pin_project! {
90 struct FromStream<S> {
91 #[pin]
92 stream: S,
93 }
94 }
95
96 impl<S, IO, E> Accept for FromStream<S>
97 where
98 S: Stream<Item = Result<IO, E>>,
99 {
100 type Conn = IO;
101 type Error = E;
102 fn poll_accept(
103 self: Pin<&mut Self>,
104 cx: &mut task::Context<'_>,
105 ) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
106 self.project().stream.poll_next(cx)
107 }
108 }
109
110 FromStream { stream }
111}