2 releases (1 stable)
1.0.0 | Jun 27, 2022 |
---|---|
0.1.4 | Jun 27, 2022 |
#12 in #infinite
5KB
55 lines
LoopGuard
Simple development utility to prevent infinite loops.
Installation
Add this to your Cargo.toml
:
[dependencies]
loop_guard = "1.0.0"
Usage
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();
}
}
Run this code:
$ cargo run
Result:
thread 'main' panicked at 'Oh no! This failed!', ...
...that's pretty much about it.
License
LoopGuard is distributed under the terms of the MIT license.