firewood icon indicating copy to clipboard operation
firewood copied to clipboard

chore: reorganize hash_helper for ethhash

Open qusuyan opened this issue 4 months ago • 4 comments

qusuyan avatar Jul 24 '25 21:07 qusuyan

This PR depends on #1130

qusuyan avatar Jul 24 '25 21:07 qusuyan

fuzz checker failed with seed 6461963400337942007

qusuyan avatar Jul 24 '25 22:07 qusuyan

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

demosdemon avatar Jul 28 '25 17:07 demosdemon

This PR will be split into smaller PRs. First one here: #1202

qusuyan avatar Aug 12 '25 06:08 qusuyan