1
//! Utilities used for the tor protocol.
2

            
3
pub(crate) mod ct;
4
pub(crate) mod err;
5
pub(crate) mod keyed_futures_unordered;
6
pub(crate) mod notify;
7
pub(crate) mod oneshot_broadcast;
8
pub(crate) mod poll_all;
9
pub(crate) mod sink_blocker;
10
pub(crate) mod skew;
11
pub(crate) mod sometimes_unbounded_sink;
12
pub(crate) mod stream_poll_set;
13
pub(crate) mod timeout;
14
pub(crate) mod token_bucket;
15
pub(crate) mod ts;
16
pub(crate) mod tunnel_activity;
17

            
18
use futures::Sink;
19
use std::pin::Pin;
20
use std::task::{Context, Poll};
21

            
22
/// Extension trait for `Sink`
23
pub(crate) trait SinkExt<T>: Sink<T> {
24
    /// Calls `futures::Sink::poll_ready` but requires `Unpin` and returns `bool`
25
    ///
26
    /// Various gnarly places in the circuit reactor find this convenient.
27
    ///
28
    /// TODO #1397 (circuit reactor) probably remove this when the circuit reactor is rewritten.
29
44
    fn poll_ready_unpin_bool(&mut self, cx: &mut Context<'_>) -> Result<bool, Self::Error>
30
44
    where
31
44
        Self: Unpin,
32
    {
33
44
        Ok(match Sink::poll_ready(Pin::new(self), cx) {
34
44
            Poll::Ready(Ok(())) => true,
35
            Poll::Ready(Err(e)) => return Err(e),
36
            Poll::Pending => false,
37
        })
38
44
    }
39
}
40
impl<T, S: Sink<T>> SinkExt<T> for S {}
41

            
42
/// Convenience alias for
43
/// [`memquota::SpecificAccount::new_noop()`](crate::memquota::SpecificAccount::new_noop())
44
///
45
/// Available only in tests, which makes diff hunks which call this more obviously correct.
46
#[cfg(any(test, feature = "testing"))]
47
1692
pub(crate) fn fake_mq<A: crate::memquota::SpecificAccount>() -> A {
48
1692
    A::new_noop()
49
1692
}
50

            
51
/// A timeout estimator that returns dummy values.
52
///
53
/// Used in the tests where the timeout estimates aren't relevant.
54
#[cfg(test)]
55
pub(crate) struct DummyTimeoutEstimator;
56

            
57
#[cfg(test)]
58
impl crate::client::circuit::TimeoutEstimator for DummyTimeoutEstimator {
59
74
    fn circuit_build_timeout(&self, _length: usize) -> std::time::Duration {
60
        // Dummy value
61
74
        std::time::Duration::from_millis(1000)
62
74
    }
63
}