Skip to main content

volans_core/upgrade/
ready.rs

1use std::{convert::Infallible, iter};
2
3use futures::future;
4
5use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
6
7#[derive(Debug, Copy, Clone)]
8pub struct ReadyUpgrade<P> {
9    protocol_name: P,
10}
11
12impl<P> ReadyUpgrade<P> {
13    pub const fn new(protocol_name: P) -> Self {
14        Self { protocol_name }
15    }
16}
17
18impl<P> UpgradeInfo for ReadyUpgrade<P>
19where
20    P: AsRef<str> + Clone,
21{
22    type Info = P;
23    type InfoIter = iter::Once<P>;
24
25    fn protocol_info(&self) -> Self::InfoIter {
26        iter::once(self.protocol_name.clone())
27    }
28}
29
30impl<C, P> InboundUpgrade<C> for ReadyUpgrade<P>
31where
32    P: AsRef<str> + Clone,
33{
34    type Output = C;
35    type Error = Infallible;
36    type Future = future::Ready<Result<Self::Output, Self::Error>>;
37
38    fn upgrade_inbound(self, stream: C, _: Self::Info) -> Self::Future {
39        future::ready(Ok(stream))
40    }
41}
42
43impl<C, P> OutboundUpgrade<C> for ReadyUpgrade<P>
44where
45    P: AsRef<str> + Clone,
46{
47    type Output = C;
48    type Error = Infallible;
49    type Future = future::Ready<Result<Self::Output, Self::Error>>;
50
51    fn upgrade_outbound(self, stream: C, _: Self::Info) -> Self::Future {
52        future::ready(Ok(stream))
53    }
54}