1use std::{
2 convert::Infallible,
3 future::{ready, Ready},
4};
5
6use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
7use libp2p_swarm::{Stream, StreamProtocol};
8
9pub struct Upgrade {
10 pub(crate) supported_protocols: Vec<StreamProtocol>,
11}
12
13impl UpgradeInfo for Upgrade {
14 type Info = StreamProtocol;
15
16 type InfoIter = std::vec::IntoIter<StreamProtocol>;
17
18 fn protocol_info(&self) -> Self::InfoIter {
19 self.supported_protocols.clone().into_iter()
20 }
21}
22
23impl InboundUpgrade<Stream> for Upgrade {
24 type Output = (Stream, StreamProtocol);
25
26 type Error = Infallible;
27
28 type Future = Ready<Result<Self::Output, Self::Error>>;
29
30 fn upgrade_inbound(self, socket: Stream, info: Self::Info) -> Self::Future {
31 ready(Ok((socket, info)))
32 }
33}
34
35impl OutboundUpgrade<Stream> for Upgrade {
36 type Output = (Stream, StreamProtocol);
37
38 type Error = Infallible;
39
40 type Future = Ready<Result<Self::Output, Self::Error>>;
41
42 fn upgrade_outbound(self, socket: Stream, info: Self::Info) -> Self::Future {
43 ready(Ok((socket, info)))
44 }
45}