Skip to content

Commit

Permalink
feat(swarm): add ConnectionDenied::downcast_ref
Browse files Browse the repository at this point in the history
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 #4018.

Pull-Request: #4020.
  • Loading branch information
nathanielc committed Jun 12, 2023
1 parent 2910c98 commit 4a1703f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@

- Remove deprecated items. See [PR 3956].

- Add ability to `downcast_ref` ConnectionDenied errors. See [PR 4020].

[PR 3292]: https://github.com/libp2p/rust-libp2p/pull/3292
[PR 3605]: https://github.com/libp2p/rust-libp2p/pull/3605
[PR 3651]: https://github.com/libp2p/rust-libp2p/pull/3651
Expand All @@ -80,6 +82,7 @@
[PR 3927]: https://github.com/libp2p/rust-libp2p/pull/3927
[PR 3955]: https://github.com/libp2p/rust-libp2p/pull/3955
[PR 3956]: https://github.com/libp2p/rust-libp2p/pull/3956
[PR 4020]: https://github.com/libp2p/rust-libp2p/pull/4020
[PR 4037]: https://github.com/libp2p/rust-libp2p/pull/4037

## 0.42.2
Expand Down
8 changes: 8 additions & 0 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4a1703f

Please sign in to comment.