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