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 skew;
9pub(crate) mod sometimes_unbounded_sink;
10pub(crate) mod stream_poll_set;
11pub(crate) mod token_bucket;
12pub(crate) mod ts;
13
14use futures::Sink;
15use std::pin::Pin;
16use std::task::{Context, Poll};
17
18/// Extension trait for `Sink`
19pub(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    fn poll_ready_unpin_bool(&mut self, cx: &mut Context<'_>) -> Result<bool, Self::Error>
26    where
27        Self: Unpin,
28    {
29        Ok(match Sink::poll_ready(Pin::new(self), cx) {
30            Poll::Ready(Ok(())) => true,
31            Poll::Ready(Err(e)) => return Err(e),
32            Poll::Pending => false,
33        })
34    }
35}
36impl<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"))]
43pub(crate) fn fake_mq<A: crate::memquota::SpecificAccount>() -> A {
44    A::new_noop()
45}