2 releases

0.1.1 Sep 18, 2021
0.1.0 Sep 18, 2021

#2894 in Rust patterns

Download history 34/week @ 2024-11-15 23/week @ 2024-11-22 29/week @ 2024-11-29 56/week @ 2024-12-06 45/week @ 2024-12-13 31/week @ 2024-12-20 14/week @ 2024-12-27 68/week @ 2025-01-03 49/week @ 2025-01-10 45/week @ 2025-01-17 53/week @ 2025-01-24 72/week @ 2025-01-31 47/week @ 2025-02-07 63/week @ 2025-02-14 99/week @ 2025-02-21 66/week @ 2025-02-28

300 downloads per month
Used in 5 crates (4 directly)

Apache-2.0/MIT

13KB
180 lines

soft_assert

Various macros that return early if a given condition is false. Similar to the various assert macros in std

Usage

Add the following to your Cargo.toml:

[dependencies]
soft_assert = "0.1"

License

This project is licensed under either of

at your option.


lib.rs:

A set of macros similar to the standard library's assert_* macros, but return early instead of panicking.

Example

use soft_assert::*;

fn not_twenty(x: i32) -> Option<i32> {
    // This will return `Option::default()`, which is `None`
    soft_assert_ne!(x, 20);
    Some(x)
}

fn double_if_greater_than_5(x: i32) -> i32 {
    // But here we don't want to return `i32::default()`,
    // so we specify a return value.
    soft_assert!(x > 5, x);
    x * 2
}

fn main() {
    assert!(not_twenty(10).is_some());   
    assert!(not_twenty(20).is_none());   

    let doubled = double_if_greater_than_5(13);
    assert_eq!(doubled, 26);    
    let not_doubled = double_if_greater_than_5(2);
    assert_eq!(not_doubled, 2);   
}

This crate is #![no_std]

No runtime deps