tor_netdoc/util.rs
1//! Misc helper functions and types for use in parsing network documents
2
3pub(crate) mod intern;
4pub(crate) mod str;
5
6pub mod batching_split_before;
7
8use std::iter::Peekable;
9
10/// An iterator with a `.peek()` method
11///
12/// We make this a trait to avoid entangling all the types with `Peekable`.
13/// Ideally we would do this with `Itertools::PeekingNext`
14/// but that was not implemented for `&mut PeekingNext`
15/// when we wrote this code,
16/// and we need that because we use a lot of `&mut NetdocReader`.
17/// <https://github.com/rust-itertools/itertools/issues/678>
18///
19/// TODO: As of itertools 0.11.0, `PeekingNext` _is_ implemented for
20/// `&'a mut I where I: PeekingNext`, so we can remove this type some time.
21///
22/// # **UNSTABLE**
23///
24/// This type is UNSTABLE and not part of the semver guarantees.
25/// You'll only see it if you ran rustdoc with `--document-private-items`.
26// This is needed because this is a trait bound for batching_split_before.
27#[doc(hidden)]
28pub trait PeekableIterator: Iterator {
29 /// Inspect the next item, if there is one
30 fn peek(&mut self) -> Option<&Self::Item>;
31}
32
33impl<I: Iterator> PeekableIterator for Peekable<I> {
34 fn peek(&mut self) -> Option<&Self::Item> {
35 self.peek()
36 }
37}
38
39impl<I: PeekableIterator> PeekableIterator for &mut I {
40 fn peek(&mut self) -> Option<&Self::Item> {
41 <I as PeekableIterator>::peek(*self)
42 }
43}
44
45/// A Private module for declaring a "sealed" trait.
46pub(crate) mod private {
47 /// A non-exported trait, used to prevent others from implementing a trait.
48 ///
49 /// For more information on this pattern, see [the Rust API
50 /// guidelines](https://rust-lang.github.io/api-guidelines/future-proofing.html#c-sealed).
51 pub trait Sealed {}
52}