mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-18 04:21:22 +00:00
swarm/src/either: Implement NetworkBehaviour
on Either
(#2370)
Implement `NetworkBehaviour` on `either::Either<L, R>` where both L and R both implement `NetworkBehaviour`. Add NetworkBehaviour derive tests for Either and Toggle Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
@ -18,6 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
pub mod either;
|
||||
pub mod toggle;
|
||||
|
||||
use crate::dial_opts::DialOpts;
|
||||
@ -718,6 +719,50 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<TInEventOld, TOutEvent, THandlerOld> NetworkBehaviourAction<TOutEvent, THandlerOld>
|
||||
where
|
||||
THandlerOld: IntoProtocolsHandler,
|
||||
<THandlerOld as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEventOld>,
|
||||
{
|
||||
/// Map the handler and handler event.
|
||||
pub fn map_handler_and_in<THandlerNew, TInEventNew>(
|
||||
self,
|
||||
f_handler: impl FnOnce(THandlerOld) -> THandlerNew,
|
||||
f_in_event: impl FnOnce(TInEventOld) -> TInEventNew,
|
||||
) -> NetworkBehaviourAction<TOutEvent, THandlerNew>
|
||||
where
|
||||
THandlerNew: IntoProtocolsHandler,
|
||||
<THandlerNew as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEventNew>,
|
||||
{
|
||||
match self {
|
||||
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(e),
|
||||
NetworkBehaviourAction::Dial { opts, handler } => NetworkBehaviourAction::Dial {
|
||||
opts,
|
||||
handler: f_handler(handler),
|
||||
},
|
||||
NetworkBehaviourAction::NotifyHandler {
|
||||
peer_id,
|
||||
handler,
|
||||
event,
|
||||
} => NetworkBehaviourAction::NotifyHandler {
|
||||
peer_id,
|
||||
handler,
|
||||
event: f_in_event(event),
|
||||
},
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score }
|
||||
}
|
||||
NetworkBehaviourAction::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
} => NetworkBehaviourAction::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The options w.r.t. which connection handler to notify of an event.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NotifyHandler {
|
||||
|
Reference in New Issue
Block a user