My understanding is that they are using volatile to prevent compiler from optimizing so that while loop visits all members, this way we are not leaking any info on how different the arrays are by returning early upon first non-equal bytes.
Can the following rust snippet achieve the same goal without using ptr::read/write_volatile?
In theory, with your snippet, there's nothing preventing the compiler from optimizing the loop to return early if any of the a ^ bs are nonzero. I'm not sure whether any current compilers actually implement such an optimization – I did some quick testing on gcc.godbolt.org and didn't find anything interesting – but a future compiler could. By contrast, the C version uses volatile to ensure the compiler treats the intermediate result as a 'black box'.
Thanks for pointing out that crate.
I don't need to avoid std::ptr, question was more on how possibly Rust facilities could be used to avoid using C-style volatile trick to stop compiler from optimizing and getting constant time.
Additionally in the context I was doing this, avoiding that unsafe keyword would be a big bonus. However based on above answers it seems this is a problem without a 100 reliable solution.