feat(swarm): add ConnectionDenied::downcast_ref

Prior to this change it was only possible to downcast a `ConnectionDenied` error when you had ownership of it which isn't the case inside `NetworkBehaviour`s. This change allows downcasting by reference.

See https://github.com/libp2p/rust-libp2p/discussions/4018.

Pull-Request: #4020.
This commit is contained in:
Nathaniel Cook
2023-06-12 09:26:09 -06:00
committed by GitHub
parent 2910c985e8
commit 4a1703f046
2 changed files with 11 additions and 0 deletions

View File

@ -1730,6 +1730,14 @@ impl ConnectionDenied {
Ok(*inner)
}
/// Attempt to downcast to a particular reason for why the connection was denied.
pub fn downcast_ref<E>(&self) -> Option<&E>
where
E: error::Error + Send + Sync + 'static,
{
self.inner.downcast_ref::<E>()
}
}
impl fmt::Display for ConnectionDenied {