How to get a hash of the source code in Rust

Viewed 339

For checking compatibility between a client and a server, I'd like to compare the versions of their shared code. I have implemented this by a build.rs that creates a hash of the content of all files under src/, but it seems brittle. I want to make sure I calculate a hash of the files that are actually used in the build. Surely this is already done at some point during the build process.

There is a feature to Add hash of source files in debug info, which sounds promising, but it adds the data to the debug information, and I want to get it at runtime.

1 Answers

If you want to check for compatibility, then I'm afraid just checking your source code is not enough (at least, this isn't always true). You might rely on some crate for some feature and if you change the version of it, it might still fail.

So if you want to go the hashing route, then you'll need to hash the entire project including the Cargo.lock file. And yes, you would indeed do it through the build.rs script.

However, I would not go that route.

Version numbers are usually used for this. In Rust, you're supposed to use semantic versioning. With this system you can see from the numbers whether two crate versions are compatible.

Related