#macro #stdlib #lib #std #convenient

benri

Convenient macros wrapping the standard library

13 releases

0.1.12 May 18, 2023
0.1.11 May 15, 2023
0.1.5 Apr 27, 2023

#478 in Rust patterns

Download history 110/week @ 2024-06-14 61/week @ 2024-06-21 202/week @ 2024-06-28 71/week @ 2024-07-05 332/week @ 2024-07-12 309/week @ 2024-07-19 200/week @ 2024-07-26 111/week @ 2024-08-02 43/week @ 2024-08-09 426/week @ 2024-08-16 369/week @ 2024-08-23 269/week @ 2024-08-30 75/week @ 2024-09-06 296/week @ 2024-09-13 269/week @ 2024-09-20 160/week @ 2024-09-27

820 downloads per month

MIT license

29KB
532 lines

benri

Windows macOS Linux crates.io docs.rs

Convenient macros wrapping the standard library.

This library provides convenient macros!() around std functionality.

Feature flags

Flag Purpose
log Enable log usage in certain places

Example 1 - Flip a bool:

let mut a = false;
flip!(a);
assert!(a == true);
flip!(a);
assert!(a == false);

Example 2 - Get the current Instant:

let now = now!();

std::thread::sleep(std::time::Duration::from_secs(1));

assert!(now.elapsed().as_secs() >= 1);

Example 3 - Get elapsed Instant time:

let now = now!();

std::thread::sleep(std::time::Duration::from_secs(1));
assert!(secs!(now)     >= 1);
assert!(secs_f64!(now) >= 1.0);
assert!(millis!(now)   >= 1000);
assert!(micros!(now)   >= 10000);
assert!(nanos!(now)    >= 100000);

Example 4 - Sleep a thread:

let now = now!();

// This sleeps the current thread for 1 second.
sleep!(1000);

assert!(secs!(now) >= 1);

Example 5 - Exit all threads:

std::thread::spawn(|| {
    mass_panic!();
}).join().unwrap();

// The program will has already exited.
// The below statement will never be reached.
unsafe { /* do bad things */ }

Example 6 - Send/receive a channel message or mass_panic!():

This works with any channel (like crossbeam_channel) that have the same method names as the std channels since the inner macro is calling .send() and .recv().

let (tx, rx) = std::sync::mpsc::channel::<u8>();

std::thread::spawn(move || {
    send!(tx, 255);
}).join().unwrap();

assert!(recv!(rx) == 255);

Dependencies

~22KB