ant_libp2p_swarm/handler/
pending.rs

1// Copyright 2022 Protocol Labs.
2// Copyright 2018 Parity Technologies (UK) Ltd.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22use std::{
23    convert::Infallible,
24    task::{Context, Poll},
25};
26
27use ant_libp2p_core::upgrade::PendingUpgrade;
28
29use crate::handler::{
30    ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, FullyNegotiatedInbound,
31    FullyNegotiatedOutbound, SubstreamProtocol,
32};
33
34/// Implementation of [`ConnectionHandler`] that returns a pending upgrade.
35#[derive(Clone, Debug)]
36pub struct PendingConnectionHandler {
37    protocol_name: String,
38}
39
40impl PendingConnectionHandler {
41    pub fn new(protocol_name: String) -> Self {
42        PendingConnectionHandler { protocol_name }
43    }
44}
45
46impl ConnectionHandler for PendingConnectionHandler {
47    type FromBehaviour = Infallible;
48    type ToBehaviour = Infallible;
49    type InboundProtocol = PendingUpgrade<String>;
50    type OutboundProtocol = PendingUpgrade<String>;
51    type OutboundOpenInfo = Infallible;
52    type InboundOpenInfo = ();
53
54    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
55        SubstreamProtocol::new(PendingUpgrade::new(self.protocol_name.clone()), ())
56    }
57
58    fn on_behaviour_event(&mut self, v: Self::FromBehaviour) {
59        // TODO: remove when Rust 1.82 is MSRV
60        #[allow(unreachable_patterns)]
61        ant_libp2p_core::util::unreachable(v)
62    }
63
64    fn poll(
65        &mut self,
66        _: &mut Context<'_>,
67    ) -> Poll<
68        ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
69    > {
70        Poll::Pending
71    }
72
73    fn on_connection_event(
74        &mut self,
75        event: ConnectionEvent<
76            Self::InboundProtocol,
77            Self::OutboundProtocol,
78            Self::InboundOpenInfo,
79            Self::OutboundOpenInfo,
80        >,
81    ) {
82        match event {
83            // TODO: remove when Rust 1.82 is MSRV
84            #[allow(unreachable_patterns)]
85            ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
86                protocol, ..
87            }) => ant_libp2p_core::util::unreachable(protocol),
88            // TODO: remove when Rust 1.82 is MSRV
89            #[allow(unreachable_patterns)]
90            ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
91                protocol,
92                info: _info,
93            }) => {
94                ant_libp2p_core::util::unreachable(protocol);
95                #[allow(unreachable_code, clippy::used_underscore_binding)]
96                {
97                    ant_libp2p_core::util::unreachable(_info);
98                }
99            }
100            // TODO: remove when Rust 1.82 is MSRV
101            #[allow(unreachable_patterns)]
102            ConnectionEvent::AddressChange(_)
103            | ConnectionEvent::DialUpgradeError(_)
104            | ConnectionEvent::ListenUpgradeError(_)
105            | ConnectionEvent::LocalProtocolsChange(_)
106            | ConnectionEvent::RemoteProtocolsChange(_) => {}
107        }
108    }
109}