1use std::sync::atomic::{AtomicU64, Ordering};
4
5use crate::circuit::UniqId;
6use derive_more::Display;
7
8#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Display)]
10#[display("Tunnel {}", _0)]
11#[cfg_attr(feature = "relay", visibility::make(pub))]
12#[allow(unreachable_pub)] pub(crate) struct TunnelId(u64);
14
15impl TunnelId {
16 pub(crate) fn next() -> TunnelId {
22 static NEXT_TUNNEL_ID: AtomicU64 = AtomicU64::new(1);
24 let id = NEXT_TUNNEL_ID.fetch_add(1, Ordering::Relaxed);
25 assert!(id != 0, "Exhausted Tunnel ID space?!");
26 TunnelId(id)
27 }
28}
29
30#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Display)]
36#[display("{} ({})", circ_uniq_id, tunnel_id)]
37pub(crate) struct TunnelScopedCircId {
38 tunnel_id: TunnelId,
40 circ_uniq_id: UniqId,
42}
43
44impl TunnelScopedCircId {
45 pub(crate) fn new(tunnel_id: TunnelId, circ_uniq_id: UniqId) -> Self {
47 Self {
48 tunnel_id,
49 circ_uniq_id,
50 }
51 }
52
53 pub(crate) fn unique_id(&self) -> UniqId {
55 self.circ_uniq_id
56 }
57}