2 个版本 (1 个稳定版本)
1.0.0 | 2022 年 6 月 27 日 |
---|---|
0.1.4 | 2022 年 6 月 27 日 |
#10 在 #无限
每月 36 次下载
5KB
55 行
LoopGuard
简单开发工具,用于防止无限循环。
安装
将此内容添加到您的 Cargo.toml
[dependencies]
loop_guard = "1.0.0"
用法
use loop_guard::LoopGuard;
fn main() {
// This LoopGuard instance will prevent a loop from running more than 50 times.
let mut guard = LoopGuard::new(50);
// However, this for loop won't cause a panic, because it only loops 10 times.
for _i in 0..10 {
guard.protect()
}
// There isn't a way to "reset" a guard instance
// If you need another guarded loop, create a new instance of LoopGuard
// We'll make a new instance, this time with a custom panic message
let mut guard_2 = LoopGuard::new(100).set_panic_message("Oh no! This failed!");
// This (infinite) loop is protected by guard_2, and will panic after 100 runs
loop {
guard_2.protect();
}
}
运行此代码
$ cargo run
结果
thread 'main' panicked at 'Oh no! This failed!', ...
...基本上就是这些。
许可证
LoopGuard 在 MIT 许可证条款下分发。