//! Utilities for working with and combining the results of //! [`Arbitrary::size_hint`][crate::Arbitrary::size_hint].
/// Protects against potential infinite recursion when calculating size hints /// due to indirect type recursion. /// /// When the depth is not too deep, calls `f` with `depth + 1` to calculate the /// size hint. /// /// Otherwise, returns the default size hint: `(0, None)`. #[inline] pubfn recursion_guard(
depth: usize,
f: impl FnOnce(usize) -> (usize, Option<usize>),
) -> (usize, Option<usize>) { const MAX_DEPTH: usize = 20; if depth > MAX_DEPTH {
(0, None)
} else {
f(depth + 1)
}
}
/// Take the sum of the `lhs` and `rhs` size hints. #[inline] pubfn and(lhs: (usize, Option<usize>), rhs: (usize, Option<usize>)) -> (usize, Option<usize>) { let lower = lhs.0 + rhs.0; let upper = lhs.1.and_then(|lhs| rhs.1.map(|rhs| lhs + rhs));
(lower, upper)
}
/// Take the sum of all of the given size hints. /// /// If `hints` is empty, returns `(0, Some(0))`, aka the size of consuming /// nothing. #[inline] pubfn and_all(hints: &[(usize, Option<usize>)]) -> (usize, Option<usize>) {
hints.iter().copied().fold((0, Some(0)), and)
}
/// Take the minimum of the lower bounds and maximum of the upper bounds in the /// `lhs` and `rhs` size hints. #[inline] pubfn or(lhs: (usize, Option<usize>), rhs: (usize, Option<usize>)) -> (usize, Option<usize>) { let lower = std::cmp::min(lhs.0, rhs.0); let upper = lhs
.1
.and_then(|lhs| rhs.1.map(|rhs| std::cmp::max(lhs, rhs)));
(lower, upper)
}
/// Take the maximum of the `lhs` and `rhs` size hints. /// /// If `hints` is empty, returns `(0, Some(0))`, aka the size of consuming /// nothing. #[inline] pubfn or_all(hints: &[(usize, Option<usize>)]) -> (usize, Option<usize>) { iflet Some(head) = hints.first().copied() {
hints[1..].iter().copied().fold(head, or)
} else {
(0, Some(0))
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.