5 个版本

0.2.0 2024年1月23日
0.1.3 2023年4月7日
0.1.2 2021年2月4日
0.1.1 2021年1月26日
0.1.0 2021年1月26日

#44 in 调试

Download history · Rust 包仓库 53882/week @ 2024-04-11 · Rust 包仓库 52820/week @ 2024-04-18 · Rust 包仓库 46068/week @ 2024-04-25 · Rust 包仓库 44571/week @ 2024-05-02 · Rust 包仓库 42900/week @ 2024-05-09 · Rust 包仓库 42480/week @ 2024-05-16 · Rust 包仓库 54972/week @ 2024-05-23 · Rust 包仓库 50898/week @ 2024-05-30 · Rust 包仓库 43042/week @ 2024-06-06 · Rust 包仓库 45581/week @ 2024-06-13 · Rust 包仓库 48070/week @ 2024-06-20 · Rust 包仓库 45524/week @ 2024-06-27 · Rust 包仓库 44956/week @ 2024-07-04 · Rust 包仓库 47989/week @ 2024-07-11 · Rust 包仓库 55045/week @ 2024-07-18 · Rust 包仓库 47225/week @ 2024-07-25 · Rust 包仓库

204,565 每月下载量
用于 82 个代码包(6 直接使用)

MIT/Apache

8KB

always-assert

可恢复断言,受 SQLite 中 assert 函数的使用 启发。

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:

可恢复断言,受 SQLite 中 assert 函数的使用 启发。

never!always! 在禁用 debug_assertions 时返回条件的实际值。

当断言失败终止比继续运行更糟糕时使用它们。

一个例子是一个关键应用程序,如数据库

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(())
}

另一个例子是普通应用程序中关于非关键功能的断言

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);

依赖

~83KB