tor_proto/tunnel/circuit/
unique_id.rs

1//! Unique identifiers for circuits.
2
3use std::fmt::{Display, Formatter};
4
5/// Process-unique identifier for a circuit.
6///
7/// We could use channel_id.circid here, but the circid can be reused
8/// over time.  This won't ever repeat on a 64-bit architecture, and
9/// is super-unlikely to repeat on a 32-bit architecture.  (If
10/// we're about to return a repeat value, we assert instead.)
11#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
12pub struct UniqId {
13    /// Channel that this circuit is on.
14    chan: usize,
15    /// ID for the circuit on the channel
16    circ: usize,
17}
18
19impl UniqId {
20    /// Construct a new circuit UniqId from its parts
21    pub(crate) fn new(chan: usize, circ: usize) -> Self {
22        UniqId { chan, circ }
23    }
24
25    /// A helper for displaying the process-unique identifiers of this circuit.
26    ///
27    /// Unlike the [`Display`] implementation, this does not display a `Circ` prefix.
28    pub fn display_chan_circ(&self) -> impl Display + '_ {
29        DisplayChanCirc(self)
30    }
31}
32
33impl Display for UniqId {
34    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35        write!(f, "Circ {}", self.display_chan_circ())
36    }
37}
38
39/// A helper for displaying the process-unique identifiers of this circuit.
40struct DisplayChanCirc<'a>(&'a UniqId);
41
42impl<'a> Display for DisplayChanCirc<'a> {
43    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44        write!(f, "{}.{}", self.0.chan, self.0.circ)
45    }
46}