libp2p_core/upgrade/
either.rs

1// Copyright 2018 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use crate::{
22    either::EitherFuture,
23    upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo},
24};
25use either::Either;
26use futures::future;
27use std::iter::Map;
28
29impl<A, B> UpgradeInfo for Either<A, B>
30where
31    A: UpgradeInfo,
32    B: UpgradeInfo,
33{
34    type Info = Either<A::Info, B::Info>;
35    type InfoIter = Either<
36        Map<<A::InfoIter as IntoIterator>::IntoIter, fn(A::Info) -> Self::Info>,
37        Map<<B::InfoIter as IntoIterator>::IntoIter, fn(B::Info) -> Self::Info>,
38    >;
39
40    fn protocol_info(&self) -> Self::InfoIter {
41        match self {
42            Either::Left(a) => Either::Left(a.protocol_info().into_iter().map(Either::Left)),
43            Either::Right(b) => Either::Right(b.protocol_info().into_iter().map(Either::Right)),
44        }
45    }
46}
47
48impl<C, A, B, TA, TB, EA, EB> InboundUpgrade<C> for Either<A, B>
49where
50    A: InboundUpgrade<C, Output = TA, Error = EA>,
51    B: InboundUpgrade<C, Output = TB, Error = EB>,
52{
53    type Output = future::Either<TA, TB>;
54    type Error = Either<EA, EB>;
55    type Future = EitherFuture<A::Future, B::Future>;
56
57    fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
58        match (self, info) {
59            (Either::Left(a), Either::Left(info)) => {
60                EitherFuture::First(a.upgrade_inbound(sock, info))
61            }
62            (Either::Right(b), Either::Right(info)) => {
63                EitherFuture::Second(b.upgrade_inbound(sock, info))
64            }
65            _ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound"),
66        }
67    }
68}
69
70impl<C, A, B, TA, TB, EA, EB> OutboundUpgrade<C> for Either<A, B>
71where
72    A: OutboundUpgrade<C, Output = TA, Error = EA>,
73    B: OutboundUpgrade<C, Output = TB, Error = EB>,
74{
75    type Output = future::Either<TA, TB>;
76    type Error = Either<EA, EB>;
77    type Future = EitherFuture<A::Future, B::Future>;
78
79    fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
80        match (self, info) {
81            (Either::Left(a), Either::Left(info)) => {
82                EitherFuture::First(a.upgrade_outbound(sock, info))
83            }
84            (Either::Right(b), Either::Right(info)) => {
85                EitherFuture::Second(b.upgrade_outbound(sock, info))
86            }
87            _ => panic!("Invalid invocation of EitherUpgrade::upgrade_outbound"),
88        }
89    }
90}