Expand description
benri
Convenient macros wrapping the standard library.
This library provides convenient macros around std functionality.
Modules
Due to how Rust macros are currently exported, all macros
will show up in the global namespace: benri::*.
To use specific types of macros, you can import the module instead:
use benri::time::*;
let now = now!();Feature flags
log- Enablelogusage 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);Modules
- Logging functionality using
log std::mem::*- Operators
- Panic macros
std::sync::*std::thread::*std::time::*
Macros
- Calls an
Arc::new - Calls
Arc::newandMutex::new - Calls
Arc::newandRwLock::new - Flip an
AtomicBoolin place withOrdering::SeqCst. - Moves all inputs in a scope
{}and immediately exits it. - Forward input to
log::error!, appended with red... FAIL - Flip a
boolin place - Calls
std::mem::forget() - Expands to [
std::time::Duration::from_millis(500)] - Get
50%the available threads as ausize. - Terminate all threads.
- Expands to [
std::time::Duration::from_secs(60)] - Gets
80%of the available threads as ausize. - Expands to
std::time::Instant::now() - Forward input to
log::info, appended with green... OK - Forward input to
log::debug, appended with green... OK - Forward input to
log::trace, appended with green... OK - Calls
std::thread::park - Expands to [
std::time::Duration::from_millis(250)] - Get
25%of the available threads as ausize. recv.()a channel message,mass_panic!on failure- Expands to [
std::time::Duration::from_secs(1)] .send()a channel message,mass_panic!on failure.send()a channel message,panic!current thread on failure- Calls
std::mem::size_of()on the type given - Forward input to
log::info, appended with white... SKIP - Forward input to
log::debug, appended with white... SKIP - Forward input to
log::trace, appended with white... SKIP - Forward input to
log::warn, appended with white... SKIP - Sleep the current thread for
xseconds - Sleep the current thread for
xmicroseconds - Sleep the current thread for
xmilliseconds - Sleep the current thread for
xnanoseconds - Calls
std::mem::swap(). This automatically appends&mutto both inputs. - Calls
std::mem::take(). This automatically appends&muton the input. - Expands to [
std::time::Duration::from_millis(333)] - Get the total available amount of threads as a
usize - Get the seconds elapsed
std::time::UNIX_EPOCH - Get the seconds elapsed since
std::time::UNIX_EPOCH