Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resetting the hash #72

Closed
darleybarreto opened this issue Jul 14, 2021 · 6 comments
Closed

Resetting the hash #72

darleybarreto opened this issue Jul 14, 2021 · 6 comments
Labels

Comments

@darleybarreto
Copy link

Hi, thanks for porting this to Rust! Is there anything similar to XXH64_reset to reset/reuse the hash? Or should I make a new one using new? Thanks in advance!

Regards,

@shepmaster
Copy link
Owner

I don't see a particular benefit to having a reset function — I'd just replace the hash value directly and expect the compiler to make up the difference.

@darleybarreto
Copy link
Author

Hmm, suppose I have the following operations:

let mut hash = XxHash64::with_seed(0);
hash.write(...);
// reset here

How would I replace the internal hash value with a new one based on a seed S?

@shepmaster
Copy link
Owner

I must be missing something. Wouldn't you do

let mut hash = XxHash64::with_seed(0);
hash.write(...);
// reset here
let mut hash = XxHash64::with_seed(0);

@darleybarreto
Copy link
Author

Oh, I see, basically creating a new one. So if I need to use in a struct, that would be:

struct MyObject{
    hash: Cell<XxHash64>, // or something else with interior mutability
}

Right?

@shepmaster
Copy link
Owner

shepmaster commented Jul 14, 2021

if I need to use in a struct

I'd expect that you'd make the methods take &mut self so there's no need for interior mutability:

struct MyObject{
    hash: XxHash64,
}

impl MyObject {
    fn foo(&mut self) {
        self.hash.write(...);
        self.hash = XxHash64::new();
    }
}

but sure, if you need interior mutability for some other reason, that also works.

@darleybarreto
Copy link
Author

That's one option indeed. Thanks for your quick responses! Stay safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants