Skip to main content

volans_swarm/handler/
either.rs

1use std::task::{Context, Poll};
2
3use either::Either;
4use futures::future;
5
6use crate::{
7    ConnectionHandler, ConnectionHandlerEvent, InboundStreamHandler, InboundUpgradeSend,
8    OutboundStreamHandler, OutboundUpgradeSend, StreamUpgradeError, SubstreamProtocol,
9    upgrade::SendWrapper,
10};
11
12impl<L, R> ConnectionHandler for Either<L, R>
13where
14    L: ConnectionHandler,
15    R: ConnectionHandler,
16{
17    type Action = Either<L::Action, R::Action>;
18    type Event = Either<L::Event, R::Event>;
19
20    fn handle_action(&mut self, action: Self::Action) {
21        match (self, action) {
22            (Either::Left(left), Either::Left(action)) => left.handle_action(action),
23            (Either::Right(right), Either::Right(action)) => right.handle_action(action),
24            _ => unreachable!(),
25        }
26    }
27
28    fn connection_keep_alive(&self) -> bool {
29        match self {
30            Either::Left(left) => left.connection_keep_alive(),
31            Either::Right(right) => right.connection_keep_alive(),
32        }
33    }
34
35    fn poll_close(&mut self, _: &mut Context<'_>) -> Poll<Option<Self::Event>> {
36        Poll::Ready(None)
37    }
38
39    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<ConnectionHandlerEvent<Self::Event>> {
40        match self {
41            Either::Left(left) => left.poll(cx).map(|e| e.map_event(Either::Left)),
42            Either::Right(right) => right.poll(cx).map(|e| e.map_event(Either::Right)),
43        }
44    }
45}
46
47impl<L, R> InboundStreamHandler for Either<L, R>
48where
49    L: InboundStreamHandler,
50    R: InboundStreamHandler,
51{
52    type InboundUpgrade = Either<SendWrapper<L::InboundUpgrade>, SendWrapper<R::InboundUpgrade>>;
53    type InboundUserData = Either<L::InboundUserData, R::InboundUserData>;
54
55    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundUpgrade, Self::InboundUserData> {
56        match self {
57            Either::Left(left) => left
58                .listen_protocol()
59                .map_upgrade(|u| Either::Left(SendWrapper(u)))
60                .map_user_data(Either::Left),
61            Either::Right(right) => right
62                .listen_protocol()
63                .map_upgrade(|u| Either::Right(SendWrapper(u)))
64                .map_user_data(Either::Right),
65        }
66    }
67
68    fn on_fully_negotiated(
69        &mut self,
70        user_data: Self::InboundUserData,
71        protocol: <Self::InboundUpgrade as InboundUpgradeSend>::Output,
72    ) {
73        match (self, user_data, protocol) {
74            (Either::Left(left), Either::Left(data), future::Either::Left(protocol)) => {
75                left.on_fully_negotiated(data, protocol)
76            }
77            (Either::Right(right), Either::Right(data), future::Either::Right(protocol)) => {
78                right.on_fully_negotiated(data, protocol)
79            }
80            (_, _, _) => unreachable!("Invalid fully negotiated protocol for either handler"),
81        }
82    }
83
84    fn on_upgrade_error(
85        &mut self,
86        user_data: Self::InboundUserData,
87        error: <Self::InboundUpgrade as InboundUpgradeSend>::Error,
88    ) {
89        match (self, user_data, error) {
90            (Either::Left(left), Either::Left(data), Either::Left(error)) => {
91                left.on_upgrade_error(data, error)
92            }
93            (Either::Right(right), Either::Right(data), Either::Right(error)) => {
94                right.on_upgrade_error(data, error)
95            }
96            (_, _, _) => unreachable!("Invalid upgrade error for either handler"),
97        }
98    }
99}
100
101impl<L, R> OutboundStreamHandler for Either<L, R>
102where
103    L: OutboundStreamHandler,
104    R: OutboundStreamHandler,
105{
106    type OutboundUpgrade = Either<SendWrapper<L::OutboundUpgrade>, SendWrapper<R::OutboundUpgrade>>;
107    type OutboundUserData = Either<L::OutboundUserData, R::OutboundUserData>;
108
109    fn on_fully_negotiated(
110        &mut self,
111        user_data: Self::OutboundUserData,
112        protocol: <Self::OutboundUpgrade as OutboundUpgradeSend>::Output,
113    ) {
114        match (self, user_data, protocol) {
115            (Either::Left(left), Either::Left(data), future::Either::Left(protocol)) => {
116                left.on_fully_negotiated(data, protocol)
117            }
118            (Either::Right(right), Either::Right(data), future::Either::Right(protocol)) => {
119                right.on_fully_negotiated(data, protocol)
120            }
121            (_, _, _) => unreachable!("Invalid fully negotiated protocol for either handler"),
122        }
123    }
124
125    fn on_upgrade_error(
126        &mut self,
127        user_data: Self::OutboundUserData,
128        error: StreamUpgradeError<<Self::OutboundUpgrade as OutboundUpgradeSend>::Error>,
129    ) {
130        match (self, user_data, error) {
131            (Either::Left(left), Either::Left(data), error) => {
132                left.on_upgrade_error(data, error.transpose_left())
133            }
134            (Either::Right(right), Either::Right(data), error) => {
135                right.on_upgrade_error(data, error.transpose_right())
136            }
137            (_, _, _) => unreachable!("Invalid upgrade error for either handler"),
138        }
139    }
140
141    fn poll_outbound_request(
142        &mut self,
143        cx: &mut Context<'_>,
144    ) -> Poll<SubstreamProtocol<Self::OutboundUpgrade, Self::OutboundUserData>> {
145        match self {
146            Either::Left(left) => left.poll_outbound_request(cx).map(|p| {
147                p.map_upgrade(|u| Either::Left(SendWrapper(u)))
148                    .map_user_data(Either::Left)
149            }),
150            Either::Right(right) => right.poll_outbound_request(cx).map(|p| {
151                p.map_upgrade(|u| Either::Right(SendWrapper(u)))
152                    .map_user_data(Either::Right)
153            }),
154        }
155    }
156}