loop-guard

Crates.ioloop-guard
lib.rsloop-guard
version1.0.0
sourcesrc
created_at2022-06-27 04:48:39.200414
updated_at2022-06-27 17:09:35.463401
descriptionA dev dependency for preventing infinte loops.
homepagehttps://github.com/nbry/loop-guard
repositoryhttps://github.com/nbry/loop-guard
max_upload_size
id613918
size6,433
Nathan Bryant (nbry)

documentation

https://github.com/nbry/loop-guard

README

LoopGuard

Crate Test Status

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.

Commit count: 10

cargo fmt