feat(swarm): introduce ConnectionId::new_unchecked constructor

In earlier iterations of the design for generic connection management, we removed the `ConnectionId::new` constructor because it would have allowed users to create `ConnectionId`s that are already taken, thus breaking invariants that `NetworkBehaviour`s rely on. Later, we incorporated the creation of `ConnectionId` in `DialOpts` which mitigates this risk altogether.

Thus, it is reasonably safe to introduce a public, non-deprecated constructor for `ConnectionId` that can be used for tests.

Related https://github.com/libp2p/rust-libp2p/pull/3327#issuecomment-1469870307.

Pull-Request: #3652.
This commit is contained in:
Thomas Eizinger
2023-03-21 20:28:46 +01:00
committed by GitHub
parent ab9555c8b6
commit 3fa10be0d5
4 changed files with 30 additions and 26 deletions

View File

@ -67,6 +67,16 @@ impl ConnectionId {
)]
pub const DUMMY: ConnectionId = ConnectionId(0);
/// Creates an _unchecked_ [`ConnectionId`].
///
/// [`Swarm`](crate::Swarm) enforces that [`ConnectionId`]s are unique and not reused.
/// This constructor does not, hence the _unchecked_.
///
/// It is primarily meant for allowing manual tests of [`NetworkBehaviour`](crate::NetworkBehaviour)s.
pub fn new_unchecked(id: usize) -> Self {
Self(id)
}
/// Returns the next available [`ConnectionId`].
pub(crate) fn next() -> Self {
Self(NEXT_CONNECTION_ID.fetch_add(1, Ordering::SeqCst))