tor_proto/tunnel/reactor/
syncview.rs

1//! Implement synchronous views of circuit internals.
2
3use super::circuit::CircHop;
4
5/// A view of a [`ClientCirc`](crate::tunnel::circuit::ClientCirc)'s internals, usable in a
6/// synchronous callback.
7//
8// TODO: I would rather have this type have a mutable reference to the reactor itself,
9// rather than just an immutable reference to a piece of it.
10// But that creates borrow-checker problems, so instead for now,
11// we only hold references to the pieces we need.
12//
13// If we need to hold more info in the future,
14// we'll need to decide whether to create additional types for the more complex variants,
15// or whether to try to stuff everything inside this type.
16pub struct ClientCircSyncView<'a> {
17    /// The hops of the circuit used to implement this view.
18    pub(super) hops: &'a Vec<CircHop>,
19}
20
21impl<'a> ClientCircSyncView<'a> {
22    /// Construct a new view of a circuit, given a mutable reference to its
23    /// reactor.
24    pub(super) fn new(reactor: &'a Vec<CircHop>) -> Self {
25        Self { hops: reactor }
26    }
27
28    /// Return the number of streams currently open on this circuit.
29    pub fn n_open_streams(&self) -> usize {
30        self.hops
31            .iter()
32            .map(|hop| hop.n_open_streams())
33            // No need to worry about overflow; max streams per hop is U16_MAX
34            .sum()
35    }
36
37    // TODO: We will eventually want to add more functionality here, but we
38    // should do so judiciously.
39}