Crates.io | loop-guard |
lib.rs | loop-guard |
version | 1.0.0 |
source | src |
created_at | 2022-06-27 04:48:39.200414 |
updated_at | 2022-06-27 17:09:35.463401 |
description | A dev dependency for preventing infinte loops. |
homepage | https://github.com/nbry/loop-guard |
repository | https://github.com/nbry/loop-guard |
max_upload_size | |
id | 613918 |
size | 6,433 |
Simple development utility to prevent infinite loops.
Add this to your 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();
}
}
Run this code:
$ cargo run
Result:
thread 'main' panicked at 'Oh no! This failed!', ...
...that's pretty much about it.
LoopGuard is distributed under the terms of the MIT license.