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 oneshot_broadcast;
7pub(crate) mod skew;
8pub(crate) mod sometimes_unbounded_sink;
9pub(crate) mod stream_poll_set;
10pub(crate) mod token_bucket;
11pub(crate) mod ts;
12
13use futures::Sink;
14use std::pin::Pin;
15use std::task::{Context, Poll};
16
17/// Extension trait for `Sink`
18pub(crate) trait SinkExt<T>: Sink<T> {
19    /// Calls `futures::Sink::poll_ready` but requires `Unpin` and returns `bool`
20    ///
21    /// Various gnarly places in the circuit reactor find this convenient.
22    ///
23    /// TODO #1397 (circuit reactor) probably remove this when the circuit reactor is rewritten.
24    fn poll_ready_unpin_bool(&mut self, cx: &mut Context<'_>) -> Result<bool, Self::Error>
25    where
26        Self: Unpin,
27    {
28        Ok(match Sink::poll_ready(Pin::new(self), cx) {
29            Poll::Ready(Ok(())) => true,
30            Poll::Ready(Err(e)) => return Err(e),
31            Poll::Pending => false,
32        })
33    }
34}
35impl<T, S: Sink<T>> SinkExt<T> for S {}
36
37/// Convenience alias for
38/// [`memquota::SpecificAccount::new_noop()`](crate::memquota::SpecificAccount::new_noop())
39///
40/// Available only in tests, which makes diff hunks which call this more obviously correct.
41#[cfg(any(test, feature = "testing"))]
42pub(crate) fn fake_mq<A: crate::memquota::SpecificAccount>() -> A {
43    A::new_noop()
44}