5 releases

0.2.0 Jan 23, 2024
0.1.3 Apr 7, 2023
0.1.2 Feb 4, 2021
0.1.1 Jan 26, 2021
0.1.0 Jan 26, 2021

#48 in Debugging

Download history 18985/week @ 2023-12-23 23925/week @ 2023-12-30 33814/week @ 2024-01-06 41490/week @ 2024-01-13 40559/week @ 2024-01-20 41524/week @ 2024-01-27 44386/week @ 2024-02-03 45948/week @ 2024-02-10 55269/week @ 2024-02-17 51511/week @ 2024-02-24 45118/week @ 2024-03-02 49058/week @ 2024-03-09 48581/week @ 2024-03-16 47473/week @ 2024-03-23 58241/week @ 2024-03-30 47828/week @ 2024-04-06

209,678 downloads per month
Used in 83 crates (6 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

~84KB