swarm/behaviour: Replace inject_* with on_event (#3011)

This commit is contained in:
João Oliveira
2022-11-17 09:28:40 +00:00
committed by GitHub
parent a714864885
commit 3df3c88f3d
38 changed files with 2482 additions and 1652 deletions

View File

@ -29,6 +29,8 @@ use libp2p_core::upgrade::{EitherUpgrade, UpgradeError};
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use std::task::{Context, Poll};
/// Auxiliary type to allow implementing [`IntoConnectionHandler`]. As [`IntoConnectionHandler`] is
/// already implemented for T, we cannot implement it for Either<A, B>.
pub enum IntoEitherHandler<L, R> {
Left(L),
Right(R),
@ -64,6 +66,29 @@ where
}
}
// Taken from https://github.com/bluss/either.
impl<L, R> IntoEitherHandler<L, R> {
/// Returns the left value.
pub fn unwrap_left(self) -> L {
match self {
IntoEitherHandler::Left(l) => l,
IntoEitherHandler::Right(_) => {
panic!("called `IntoEitherHandler::unwrap_left()` on a `Right` value.",)
}
}
}
/// Returns the right value.
pub fn unwrap_right(self) -> R {
match self {
IntoEitherHandler::Right(r) => r,
IntoEitherHandler::Left(_) => {
panic!("called `IntoEitherHandler::unwrap_right()` on a `Left` value.",)
}
}
}
}
/// Implementation of a [`ConnectionHandler`] that represents either of two [`ConnectionHandler`]
/// implementations.
impl<L, R> ConnectionHandler for Either<L, R>