mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-12 09:31:20 +00:00
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:
@ -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))
|
||||
|
Reference in New Issue
Block a user