#assert

always-assert

Recoverable assertions for long-running robust applications

3 releases

0.1.2 Feb 4, 2021
0.1.1 Jan 26, 2021
0.1.0 Jan 26, 2021

#168 in Debugging

Download history 19693/week @ 2022-11-28 23615/week @ 2022-12-05 19246/week @ 2022-12-12 20555/week @ 2022-12-19 14861/week @ 2022-12-26 19018/week @ 2023-01-02 18740/week @ 2023-01-09 24345/week @ 2023-01-16 21396/week @ 2023-01-23 20288/week @ 2023-01-30 20060/week @ 2023-02-06 23746/week @ 2023-02-13 20924/week @ 2023-02-20 25066/week @ 2023-02-27 22212/week @ 2023-03-06 22905/week @ 2023-03-13

92,750 downloads per month
Used in 53 crates (3 directly)

MIT/Apache

8KB

always-assert

Recoverable assertions, inspired by the use of assert() in SQLite.

use always_assert::never;

fn apply_transaction(&mut self, tx: Transaction) -> Result<(), TransactionAborted> {
    let delta = self.compute_delta(&tx);

    if never!(!self.check_internal_invariant(&delta)) {
        // Ok, something in this transaction messed up our internal state.
        // This really shouldn't be happening, and this signifies a bug.
        // Luckily, we can recover by just rejecting the transaction.
        return abort_transaction(tx);
    }
    self.commit(delta);
    Ok(())
}

lib.rs:

Recoverable assertions, inspired by the use of assert() in SQLite.

never! and always! return the actual value of the condition if debug_assertions are disabled.

Use them when terminating on assertion failure is worse than continuing.

One example would be a critical application like a database:

use always_assert::never;

fn apply_transaction(&mut self, tx: Transaction) -> Result<(), TransactionAborted> {
    let delta = self.compute_delta(&tx);

    if never!(!self.check_internal_invariant(&delta)) {
        // Ok, something in this transaction messed up our internal state.
        // This really shouldn't be happening, and this signifies a bug.
        // Luckily, we can recover by just rejecting the transaction.
        return abort_transaction(tx);
    }
    self.commit(delta);
    Ok(())
}

Another example is assertions about non-critical functionality in usual apps

use always_assert::never;

let english_message = "super app installed!"
let mut local_message = localize(english_message);
if never!(local_message.is_empty(), "missing localization for {}", english_message) {
    // We localized all the messages but this one slipper through the cracks?
    // Better to show the english one then than to fail outright;
    local_message = english_message;
}
println!("{}", local_message);

Dependencies

~21KB