firewood
firewood copied to clipboard
chore: reorganize hash_helper for ethhash
This PR depends on #1130
fuzz checker failed with seed 6461963400337942007
We appear to have an opportunity to use a drop guard to ensure the path is properly reset to the correct length preventing any bugs from accidentally forgetting to truncate:
struct PathGuard<'a> {
path: &'a mut Path,
original_len: usize,
}
impl<'a> PathGuard<'a> {
pub fn new(path: &'a mut Path) -> Self {
Self {
original_len: path.len(),
path,
}
}
// note: cannot use `Self` as it's bound to `'a` and we need a new PathGuard bound to `'_`.
pub fn fork(&mut self) -> PathGuard<'_> {
PathGuard::new(self.path)
}
}
impl Drop for PathGuard<'_> {
fn drop(&mut self) {
self.path.0.truncate(self.original_len);
}
}
impl std::ops::Deref for PathGuard<'_> {
type Target = Path;
fn deref(&self) -> &Self::Target {
self.path
}
}
impl std::ops::DerefMut for PathGuard<'_> {
fn deref_mut(&self) -> &mut Self::Target {
self.path
}
}
complete example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=6f106d75a0119d7783469810682ca91c
This PR will be split into smaller PRs. First one here: #1202