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 skew;
9
pub(crate) mod sometimes_unbounded_sink;
10
pub(crate) mod stream_poll_set;
11
pub(crate) mod token_bucket;
12
pub(crate) mod ts;
13

            
14
use futures::Sink;
15
use std::pin::Pin;
16
use std::task::{Context, Poll};
17

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

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