feat(swarm): remove deprecated IntoConnectionHandler

This removes the deprecated `IntoConnectionHandler` trait and all its implementations. Consequently, `NetworkBehaviour::new_handler` and `NetworkBehaviour::addresses_of_peer` are now gone and the two `handle_` functions are now required to implement.

Related: #3647.

Pull-Request: #3884.
This commit is contained in:
Thomas Eizinger
2023-05-08 10:30:29 +02:00
committed by GitHub
parent b4e724dd72
commit 0e36c7c072
15 changed files with 38 additions and 371 deletions

View File

@ -18,8 +18,6 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#[allow(deprecated)]
use crate::handler::IntoConnectionHandler;
use crate::handler::{
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, FullyNegotiatedInbound,
InboundUpgradeSend, KeepAlive, ListenUpgradeError, SubstreamProtocol,
@ -27,71 +25,8 @@ use crate::handler::{
use crate::upgrade::SendWrapper;
use either::Either;
use futures::future;
use libp2p_core::ConnectedPoint;
use libp2p_identity::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),
}
/// Implementation of a [`IntoConnectionHandler`] that represents either of two [`IntoConnectionHandler`]
/// implementations.
#[allow(deprecated)]
impl<L, R> IntoConnectionHandler for IntoEitherHandler<L, R>
where
L: IntoConnectionHandler,
R: IntoConnectionHandler,
{
type Handler = Either<L::Handler, R::Handler>;
fn into_handler(self, p: &PeerId, c: &ConnectedPoint) -> Self::Handler {
match self {
IntoEitherHandler::Left(into_handler) => Either::Left(into_handler.into_handler(p, c)),
IntoEitherHandler::Right(into_handler) => {
Either::Right(into_handler.into_handler(p, c))
}
}
}
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
match self {
IntoEitherHandler::Left(into_handler) => {
Either::Left(SendWrapper(into_handler.inbound_protocol()))
}
IntoEitherHandler::Right(into_handler) => {
Either::Right(SendWrapper(into_handler.inbound_protocol()))
}
}
}
}
// 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.",)
}
}
}
}
impl<LIP, RIP, LIOI, RIOI>
FullyNegotiatedInbound<Either<SendWrapper<LIP>, SendWrapper<RIP>>, Either<LIOI, RIOI>>
where

View File

@ -21,8 +21,6 @@
//! A [`ConnectionHandler`] implementation that combines multiple other [`ConnectionHandler`]s
//! indexed by some key.
#[allow(deprecated)]
use crate::handler::IntoConnectionHandler;
use crate::handler::{
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, ListenUpgradeError,
@ -31,8 +29,6 @@ use crate::handler::{
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, UpgradeInfoSend};
use crate::NegotiatedSubstream;
use futures::{future::BoxFuture, prelude::*};
use libp2p_core::ConnectedPoint;
use libp2p_identity::PeerId;
use rand::Rng;
use std::{
cmp,
@ -280,76 +276,6 @@ impl<K, H> IntoIterator for MultiHandler<K, H> {
}
}
/// A [`IntoConnectionHandler`] for multiple other `IntoConnectionHandler`s.
#[derive(Clone)]
#[deprecated(note = "Use `MultiHandler` directly.")]
pub struct IntoMultiHandler<K, H> {
handlers: HashMap<K, H>,
}
#[allow(deprecated)]
impl<K, H> fmt::Debug for IntoMultiHandler<K, H>
where
K: fmt::Debug + Eq + Hash,
H: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IntoMultiHandler")
.field("handlers", &self.handlers)
.finish()
}
}
#[allow(deprecated)]
impl<K, H> IntoMultiHandler<K, H>
where
K: Hash + Eq,
H: IntoConnectionHandler,
{
/// Create and populate an `IntoMultiHandler` from the given iterator.
///
/// It is an error for any two protocols handlers to share the same protocol name.
pub fn try_from_iter<I>(iter: I) -> Result<Self, DuplicateProtonameError>
where
I: IntoIterator<Item = (K, H)>,
{
let m = IntoMultiHandler {
handlers: HashMap::from_iter(iter),
};
uniq_proto_names(m.handlers.values().map(|h| h.inbound_protocol()))?;
Ok(m)
}
}
#[allow(deprecated)]
impl<K, H> IntoConnectionHandler for IntoMultiHandler<K, H>
where
K: Debug + Clone + Eq + Hash + Send + 'static,
H: IntoConnectionHandler,
{
type Handler = MultiHandler<K, H::Handler>;
fn into_handler(self, p: &PeerId, c: &ConnectedPoint) -> Self::Handler {
MultiHandler {
handlers: self
.handlers
.into_iter()
.map(|(k, h)| (k, h.into_handler(p, c)))
.collect(),
}
}
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
Upgrade {
upgrades: self
.handlers
.iter()
.map(|(k, h)| (k.clone(), h.inbound_protocol()))
.collect(),
}
}
}
/// Index and protocol name pair used as `UpgradeInfo::Info`.
#[derive(Debug, Clone)]
pub struct IndexedProtoName<H>(usize, H);

View File

@ -18,8 +18,6 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#[allow(deprecated)]
use crate::handler::IntoConnectionHandler;
use crate::handler::{
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent,
ConnectionHandlerUpgrErr, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
@ -28,60 +26,9 @@ use crate::handler::{
use crate::upgrade::SendWrapper;
use either::Either;
use futures::future;
use libp2p_core::{
upgrade::{SelectUpgrade, UpgradeError},
ConnectedPoint,
};
use libp2p_identity::PeerId;
use libp2p_core::upgrade::{SelectUpgrade, UpgradeError};
use std::{cmp, task::Context, task::Poll};
/// Implementation of `IntoConnectionHandler` that combines two protocols into one.
#[derive(Debug, Clone)]
pub struct IntoConnectionHandlerSelect<TProto1, TProto2> {
/// The first protocol.
proto1: TProto1,
/// The second protocol.
proto2: TProto2,
}
impl<TProto1, TProto2> IntoConnectionHandlerSelect<TProto1, TProto2> {
/// Builds a `IntoConnectionHandlerSelect`.
pub(crate) fn new(proto1: TProto1, proto2: TProto2) -> Self {
IntoConnectionHandlerSelect { proto1, proto2 }
}
pub fn into_inner(self) -> (TProto1, TProto2) {
(self.proto1, self.proto2)
}
}
#[allow(deprecated)]
impl<TProto1, TProto2> IntoConnectionHandler for IntoConnectionHandlerSelect<TProto1, TProto2>
where
TProto1: IntoConnectionHandler,
TProto2: IntoConnectionHandler,
{
type Handler = ConnectionHandlerSelect<TProto1::Handler, TProto2::Handler>;
fn into_handler(
self,
remote_peer_id: &PeerId,
connected_point: &ConnectedPoint,
) -> Self::Handler {
ConnectionHandlerSelect {
proto1: self.proto1.into_handler(remote_peer_id, connected_point),
proto2: self.proto2.into_handler(remote_peer_id, connected_point),
}
}
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
SelectUpgrade::new(
SendWrapper(self.proto1.inbound_protocol()),
SendWrapper(self.proto2.inbound_protocol()),
)
}
}
/// Implementation of [`ConnectionHandler`] that combines two protocols into one.
#[derive(Debug, Clone)]
pub struct ConnectionHandlerSelect<TProto1, TProto2> {