6 releases

Uses new Rust 2024

0.2.5 Dec 24, 2025
0.2.4 Dec 31, 2023
0.1.1 Dec 2, 2023

#185 in Data structures

Download history 7043/week @ 2025-10-19 7696/week @ 2025-10-26 4367/week @ 2025-11-02 4325/week @ 2025-11-09 5050/week @ 2025-11-16 5673/week @ 2025-11-23 4831/week @ 2025-11-30 5750/week @ 2025-12-07 6282/week @ 2025-12-14 6117/week @ 2025-12-21 5470/week @ 2025-12-28 9517/week @ 2026-01-04 7744/week @ 2026-01-11 8652/week @ 2026-01-18 7823/week @ 2026-01-25 8200/week @ 2026-02-01

32,809 downloads per month
Used in 33 crates (via openraft)

MIT/Apache

38KB
979 lines

validit

Discord Chat Crates.io docs.rs Crates.io Crates.io

Validate variable internal state when the variable is accessed.

  • Implement trait Validate for a type T to define how to validate internal state of T.
  • Wrapper struct Valid<T: Validate> implements Deref and DerefMut traits, and validates the internal state when the variable is accessed.

For example, If in your program you have a struct Foo(u64) and you want to make sure that a is always less than to 5, you can implement Validate trait for Foo and use less! macro to validate a.

struct Foo(u64);

impl validit::Validate for Foo {
  fn validate(&self) -> Result<(), Box<dyn std::error::Error>> {
    validit::less!(self.0, 5);
    Ok(())
  }
}

fn main() {
  let v1 = Valid::new(Foo(1));
  let _x = v1.0; // Good.

  let v6 = Foo(6);
  let _x = v6.0; // No panic without validation.
  
  let v6 = Valid::new(Foo(6));
  let _x = v6.0; // panic: panicked at 'invalid state: expect: self.0(6) < 5(5) ...
}

Contribution

Dependencies

~0–0.8MB
~17K SLoC