forked from pydantic/pydantic
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding recursion_guard argument * fix linting, start on logic * basic recursion implementation working * make recursion guard option-al * more tests * move RecursionGuard, optimise recursion check * tests for recursion across a wrap validator * bump * tweaks
- Loading branch information
1 parent
771c928
commit b95b3d2
Showing
36 changed files
with
565 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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,36 @@ | ||
use std::collections::HashSet; | ||
use std::hash::BuildHasherDefault; | ||
|
||
use nohash_hasher::NoHashHasher; | ||
|
||
/// This is used to avoid cyclic references in input data causing recursive validation and a nasty segmentation fault. | ||
/// It's used in `validators/recursive.rs` to detect when a reference is reused within itself. | ||
#[derive(Debug, Clone, Default)] | ||
pub struct RecursionGuard(Option<HashSet<usize, BuildHasherDefault<NoHashHasher<usize>>>>); | ||
|
||
impl RecursionGuard { | ||
// insert a new id into the set, return whether the set already had the id in it | ||
pub fn contains_or_insert(&mut self, id: usize) -> bool { | ||
match self.0 { | ||
// https://doc.rust-lang.org/std/collections/struct.HashSet.html#method.insert | ||
// "If the set did not have this value present, `true` is returned." | ||
Some(ref mut set) => !set.insert(id), | ||
None => { | ||
let mut set: HashSet<usize, BuildHasherDefault<NoHashHasher<usize>>> = | ||
HashSet::with_capacity_and_hasher(10, BuildHasherDefault::default()); | ||
set.insert(id); | ||
self.0 = Some(set); | ||
false | ||
} | ||
} | ||
} | ||
|
||
pub fn remove(&mut self, id: &usize) { | ||
match self.0 { | ||
Some(ref mut set) => { | ||
set.remove(id); | ||
} | ||
None => unreachable!(), | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.