Skip to main content

volans_swarm/
upgrade.rs

1use volans_core::upgrade;
2
3use crate::Substream;
4
5pub trait UpgradeInfoSend: Send + 'static {
6    type Info: AsRef<str> + Clone + Send + 'static;
7    type InfoIter: Iterator<Item = Self::Info> + Send + 'static;
8
9    fn protocol_info(&self) -> Self::InfoIter;
10}
11
12impl<T> UpgradeInfoSend for T
13where
14    T: upgrade::UpgradeInfo + Send + 'static,
15    T::Info: Send + 'static,
16    <T::InfoIter as IntoIterator>::IntoIter: Send + 'static,
17{
18    type Info = T::Info;
19    type InfoIter = <T::InfoIter as IntoIterator>::IntoIter;
20
21    fn protocol_info(&self) -> Self::InfoIter {
22        upgrade::UpgradeInfo::protocol_info(self).into_iter()
23    }
24}
25
26pub trait InboundUpgradeSend: UpgradeInfoSend {
27    type Output: Send + 'static;
28    type Error: Send + 'static;
29    type Future: Future<Output = Result<Self::Output, Self::Error>> + Send + 'static;
30
31    fn upgrade_inbound(self, socket: Substream, info: Self::Info) -> Self::Future;
32}
33
34impl<T, TInfo> InboundUpgradeSend for T
35where
36    T: upgrade::InboundUpgrade<Substream, Info = TInfo> + UpgradeInfoSend<Info = TInfo>,
37    TInfo: AsRef<str> + Clone + Send + 'static,
38    T::Output: Send + 'static,
39    T::Error: Send + 'static,
40    T::Future: Send + 'static,
41{
42    type Output = T::Output;
43    type Error = T::Error;
44    type Future = T::Future;
45
46    fn upgrade_inbound(self, socket: Substream, info: TInfo) -> Self::Future {
47        upgrade::InboundUpgrade::upgrade_inbound(self, socket, info)
48    }
49}
50
51pub trait OutboundUpgradeSend: UpgradeInfoSend {
52    type Output: Send + 'static;
53    type Error: Send + 'static;
54    type Future: Future<Output = Result<Self::Output, Self::Error>> + Send + 'static;
55
56    fn upgrade_outbound(self, socket: Substream, info: Self::Info) -> Self::Future;
57}
58
59impl<T, TInfo> OutboundUpgradeSend for T
60where
61    T: upgrade::OutboundUpgrade<Substream, Info = TInfo> + UpgradeInfoSend<Info = TInfo>,
62    TInfo: AsRef<str> + Clone + Send + 'static,
63    T::Output: Send + 'static,
64    T::Error: Send + 'static,
65    T::Future: Send + 'static,
66{
67    type Output = T::Output;
68    type Error = T::Error;
69    type Future = T::Future;
70
71    fn upgrade_outbound(self, socket: Substream, info: TInfo) -> Self::Future {
72        upgrade::OutboundUpgrade::upgrade_outbound(self, socket, info)
73    }
74}
75
76pub struct SendWrapper<T>(pub T);
77
78impl<T: UpgradeInfoSend> upgrade::UpgradeInfo for SendWrapper<T> {
79    type Info = T::Info;
80    type InfoIter = T::InfoIter;
81
82    fn protocol_info(&self) -> Self::InfoIter {
83        UpgradeInfoSend::protocol_info(&self.0)
84    }
85}
86
87impl<T: OutboundUpgradeSend> upgrade::OutboundUpgrade<Substream> for SendWrapper<T> {
88    type Output = T::Output;
89    type Error = T::Error;
90    type Future = T::Future;
91
92    fn upgrade_outbound(self, socket: Substream, info: T::Info) -> Self::Future {
93        OutboundUpgradeSend::upgrade_outbound(self.0, socket, info)
94    }
95}
96
97impl<T: InboundUpgradeSend> upgrade::InboundUpgrade<Substream> for SendWrapper<T> {
98    type Output = T::Output;
99    type Error = T::Error;
100    type Future = T::Future;
101
102    fn upgrade_inbound(self, socket: Substream, info: T::Info) -> Self::Future {
103        InboundUpgradeSend::upgrade_inbound(self.0, socket, info)
104    }
105}