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