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

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

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

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