tor_proto/
util.rs

1//! Utilities used for the tor protocol.
2
3pub(crate) mod ct;
4pub(crate) mod err;
5pub(crate) mod keyed_futures_unordered;
6pub(crate) mod notify;
7pub(crate) mod oneshot_broadcast;
8pub(crate) mod poll_all;
9pub(crate) mod sink_blocker;
10pub(crate) mod skew;
11pub(crate) mod sometimes_unbounded_sink;
12pub(crate) mod stream_poll_set;
13pub(crate) mod token_bucket;
14pub(crate) mod ts;
15pub(crate) mod tunnel_activity;
16
17use futures::Sink;
18use std::pin::Pin;
19use std::task::{Context, Poll};
20
21/// Extension trait for `Sink`
22pub(crate) trait SinkExt<T>: Sink<T> {
23    /// Calls `futures::Sink::poll_ready` but requires `Unpin` and returns `bool`
24    ///
25    /// Various gnarly places in the circuit reactor find this convenient.
26    ///
27    /// TODO #1397 (circuit reactor) probably remove this when the circuit reactor is rewritten.
28    fn poll_ready_unpin_bool(&mut self, cx: &mut Context<'_>) -> Result<bool, Self::Error>
29    where
30        Self: Unpin,
31    {
32        Ok(match Sink::poll_ready(Pin::new(self), cx) {
33            Poll::Ready(Ok(())) => true,
34            Poll::Ready(Err(e)) => return Err(e),
35            Poll::Pending => false,
36        })
37    }
38}
39impl<T, S: Sink<T>> SinkExt<T> for S {}
40
41/// Convenience alias for
42/// [`memquota::SpecificAccount::new_noop()`](crate::memquota::SpecificAccount::new_noop())
43///
44/// Available only in tests, which makes diff hunks which call this more obviously correct.
45#[cfg(any(test, feature = "testing"))]
46pub(crate) fn fake_mq<A: crate::memquota::SpecificAccount>() -> A {
47    A::new_noop()
48}
49
50/// A timeout estimator that returns dummy values.
51///
52/// Used in the tests where the timeout estimates aren't relevant.
53#[cfg(test)]
54pub(crate) struct DummyTimeoutEstimator;
55
56#[cfg(test)]
57impl crate::client::circuit::TimeoutEstimator for DummyTimeoutEstimator {
58    fn circuit_build_timeout(&self, _length: usize) -> std::time::Duration {
59        // Dummy value
60        std::time::Duration::from_millis(1000)
61    }
62}