#macro #assertions #poke #out

rassert-rs

Simple macro for expressing Result-returning assertions

3 stable releases

Uses new Rust 2021

3.0.0 Aug 27, 2022
2.0.0 Aug 27, 2022
1.0.0 Mar 30, 2022

#511 in Rust patterns

Download history 2/week @ 2022-08-19 34/week @ 2022-08-26 8/week @ 2022-09-02 5/week @ 2022-09-09 2/week @ 2022-09-16 1/week @ 2022-09-23 5/week @ 2022-09-30 3/week @ 2022-10-07 2/week @ 2022-10-14 4/week @ 2022-10-21 16/week @ 2022-10-28 25/week @ 2022-11-04 16/week @ 2022-11-11 16/week @ 2022-11-18 4/week @ 2022-11-25

63 downloads per month
Used in 2 crates (via vin-macros)

MIT license

6KB
86 lines

rassert

Simple macro for expressing Result-returning assertions and notifying, hard-error assertions (useful for functions where you can't propagate an error upstream so you want to log it).

Usage

use rassert::{rassert, rassert_notify};

enum MyError {
    NotAnswerToLife,
}

struct SomeOutput;

pub fn foo(input: usize) -> Result<SomeOutput, MyError> {
    rassert!(input == 42, MyError::NotAnswerToLife);

    let output = ...;
    Ok(output)
}

pub fn bar(input: usize) {
    rassert_notify!(1 != 1, error!("Well, that's not true."));
    
    println!("Hi everyone"); // Never reached since the above rassert_notify fails and returns
}

Why

Because the alternative is rather ugly and does not obviously express that the expression is a precondition.

enum MyError {
    NotAnswerToLife,
}

struct SomeOutput;

pub fn foo(input: usize) -> Result<SomeOutput, MyError> {
    if input != 42 {
        return Err(MyError::NotAnswerToLife);
    }

    let output = ...;
    Ok(output)
}

I found myself just copy-pasting the same rassert macro over and over in my projects, so might as well put it on Cargo.

No runtime deps