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 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> {
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        libp2p_core::util::unreachable(v)
62    }
63
64    fn poll(
65        &mut self,
66        _: &mut Context<'_>,
67    ) -> Poll<ConnectionHandlerEvent<Self::OutboundProtocol, Infallible, Self::ToBehaviour>> {
68        Poll::Pending
69    }
70
71    fn on_connection_event(
72        &mut self,
73        event: ConnectionEvent<Self::InboundProtocol, Self::OutboundProtocol, (), Infallible>,
74    ) {
75        match event {
76            // TODO: remove when Rust 1.82 is MSRV
77            #[allow(unreachable_patterns)]
78            ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
79                protocol, ..
80            }) => libp2p_core::util::unreachable(protocol),
81            // TODO: remove when Rust 1.82 is MSRV
82            #[allow(unreachable_patterns)]
83            ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
84                protocol,
85                info: _info,
86            }) => {
87                libp2p_core::util::unreachable(protocol);
88                #[allow(unreachable_code, clippy::used_underscore_binding)]
89                {
90                    libp2p_core::util::unreachable(_info);
91                }
92            }
93            // TODO: remove when Rust 1.82 is MSRV
94            #[allow(unreachable_patterns)]
95            ConnectionEvent::AddressChange(_)
96            | ConnectionEvent::DialUpgradeError(_)
97            | ConnectionEvent::ListenUpgradeError(_)
98            | ConnectionEvent::LocalProtocolsChange(_)
99            | ConnectionEvent::RemoteProtocolsChange(_) => {}
100        }
101    }
102}