postfix_assert

Crates.iopostfix_assert
lib.rspostfix_assert
version0.1.1
sourcesrc
created_at2021-04-04 02:33:55.438206
updated_at2021-04-04 22:43:26.837252
descriptionTrait 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.
homepage
repositoryhttps://github.com/Pat-Lafon/postfix_assert
max_upload_size
id378598
size13,978
Patrick LaFontaine (Pat-Lafon)

documentation

README

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

Commit count: 7

cargo fmt