-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add faillible version of size_hint to properly handle recursive struc…
…tures The default implementation forwards to the current size_hint, so that it can be relied upon also for structures that still use the default implementation. The default implementation of `size_hint` is not changed, so that it does not break existing implementations
- Loading branch information
1 parent
8fff98e
commit b5e2b9c
Showing
5 changed files
with
510 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! Utilities for working with and combining the results of | ||
//! [`Arbitrary::faillible_size_hint`][crate::Arbitrary::faillible_size_hint]. | ||
|
||
use crate::{size_hint::MAX_DEPTH, MaxRecursionReached}; | ||
|
||
/// 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 an error. | ||
#[inline] | ||
pub fn recursion_guard( | ||
depth: usize, | ||
f: impl FnOnce(usize) -> Result<(usize, Option<usize>), MaxRecursionReached>, | ||
) -> Result<(usize, Option<usize>), MaxRecursionReached> { | ||
if depth > MAX_DEPTH { | ||
Err(MaxRecursionReached) | ||
} else { | ||
f(depth + 1) | ||
} | ||
} |
Oops, something went wrong.