Crates.io | postfix_assert |
lib.rs | postfix_assert |
version | 0.1.1 |
source | src |
created_at | 2021-04-04 02:33:55.438206 |
updated_at | 2021-04-04 22:43:26.837252 |
description | 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. |
homepage | |
repository | https://github.com/Pat-Lafon/postfix_assert |
max_upload_size | |
id | 378598 |
size | 13,978 |
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.
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()
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.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