#assertions #testing #assert #debug-assert

postfix_assert

Trait implementations of common assertions in (postfix) method style for the Rust standard library. This allows for assertions to be chained and integrated as part of an expression.

2 releases

0.1.1 Apr 4, 2021
0.1.0 Apr 4, 2021

#462 in Debugging

26 downloads per month

MIT license

12KB
151 lines

postfix_assert

This crate provides trait implementations for standard library structs with properties that are commonly asserted. These assertions are different than other crates in that they abandon the common assert!(x.is_none()); statement style in favor of an inline, readable x.assert_none();. Importantly, these assert_* methods return the reference that they are called on so that you can interweave your assertions with your code instead of them needing to be on their separate lines.

Example

fn computation_producing_some() -> Option<u32> {
    Some(1)
}

fn computation_producing_none_from_some(x: Option<u32>) -> Option<u32> {
    None
}
computation_producing_some()
    .assert_some()
    .and(computation_producing_none_from_some)
    .assert_none()

Crate Features

  • debug - When enabled, this will cause change all assertions to debug_assertions which only run in debug mode but not in release mode. The overhead for assertions is generally small, especially if the compiler can optimize them away. When they are costly, this feature only enables asserts when you are trying to debug your code. When this code runs in release mode, all methods in this library are optimized away.

Other Assertion Crates

I am starting to compile a list of crates that empower assertions in some form. I use this for my benefit in two ways: I learn about new crates, which I may want to use in my code and, I can steal borrow ideas for useful assertions from these other crates. This list will always be incomplete and may or may not have my comments alongside each crate.

License: MIT

No runtime deps

Features