#cache #hash #proc-macro #derive #struct

const-struct-version

A trait plus proc-macro to generate a hash based on the fields of a struct. Useful for intelligently expiring a cache when the stored object changes.

2 unstable releases

new 0.2.0 Mar 9, 2025
0.1.3 Mar 9, 2025

#97 in Caching

26 downloads per month

MIT/Apache

24KB
282 lines

Const-struct-version

CI Crates.io Docs.rs

This crate has a trait + proc-macro to generate a hash based on the fields of a struct. Useful for intelligently expiring a cache when the stored object changes.

Note that this crate is NOT perfect and may not work for all use cases, especially given our limitations with proc-macros.

Known limitations:

  1. Custom derivations that have special logic we can't see.
  2. Objects that are inherently unstable/mutable.
  3. Struct/Enum names that are significant to the versioning.

Feel free to open an issue if you have a use case that doesn't work with this crate.

Features

  • serde-attributes - add #[serde(XXX)] attributes into the generated hash.
  • derive - derive the StructVersion trait for an object automatically.
  • uuid - Support for uuid crate.
  • chrono - Support for chrono crate.
  • indexmap - Support for indexmap crate.
  • url - Support for url crate.

Usage

[dependencies]
const_struct_version = "0.1"
use const_struct_version::StructVersion;

#[derive(StructVersion)]
pub struct Cart {
    items: Vec<String>,
    owner: String,
}

fn main() {
    println!("Cart version: {}", Cart::version_cached());
}

Contribution

Contribution are welcome! Please feel free to open an issue or a pull request.

License

Licensed under either of

at your option.

Dependencies

~0.5–1.7MB
~34K SLoC