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§
- log
- Logging functionality using
log - mem
std::mem::*- ops
- Operators
- panic
- Panic macros
- sync
std::sync::*- thread
std::thread::*- time
std::time::*
Macros§
- arc
- Calls an
Arc::new - arc_mut
- Calls
Arc::newandMutex::new - arc_rw
- Calls
Arc::newandRwLock::new - atomic_
add fetch_add()astd::sync::atomictype withOrdering::SeqCst- atomic_
flip - Flip an
AtomicBoolin place withOrdering::SeqCst. - atomic_
load loadastd::sync::atomictype withOrdering::SeqCst- atomic_
store store()astd::sync::atomictype withOrdering::SeqCst- atomic_
sub fetch_sub()astd::sync::atomictype withOrdering::SeqCst- debug_
panic panic!(), but only in debug mode.- drop
- Moves all inputs in a scope
{}and immediately exits it. - fail
- Forward input to
log::error!, prefixed with red[FAIL] - flip
- Flip a
boolin place - forget
- Calls
std::mem::forget() - half_
second - Expands to [
std::time::Duration::from_millis(500)] - half_
threads - Get
50%the available threads as ausize. - lock
.lock()aMutexand.unwrap()- lockr
.read()aRwLockand.unwrap()- lockw
.write()to aRwLockand .unwrap()- mass_
panic - Terminate all threads.
- micros
- Calls
std::time::Instant::elapsedandstd::time::Duration::as_micros() - millis
- Calls
std::time::Instant::elapsedandstd::time::Duration::as_millis() - minute
- Expands to [
std::time::Duration::from_secs(60)] - most_
threads - Gets
80%of the available threads as ausize. - nanos
- Calls
std::time::Instant::elapsedandstd::time::Duration::as_nanos() - now
- Expands to
std::time::Instant::now() - ok
- Forward input to
log::info, prefixed with green[ OK ] - ok_
debug - Forward input to
log::debug, prefixed with green[ OK ] - ok_
trace - Forward input to
log::trace, prefixed with green[ OK ] - park
- Calls
std::thread::park - quarter_
second - Expands to [
std::time::Duration::from_millis(250)] - quarter_
threads - Get
25%of the available threads as ausize. - recv
.recv()a channel message and.unwrap()- recv_
or_ mass .recv()a channel message,mass_panic!on failure- replace
- Calls
std::mem::swap(). This automatically appends&muttodest. - second
- Expands to [
std::time::Duration::from_secs(1)] - secs
- Calls
std::time::Instant::elapsedandstd::time::Duration::as_secs - secs_
f32 - Calls
std::time::Instant::elapsedandstd::time::Duration::as_secs_f32() - secs_
f64 - Calls
std::time::Instant::elapsedandstd::time::Duration::as_secs_f64() - send
.send()a channel message and.unwrap()- send_
or_ mass .send()a channel message,mass_panic!on failure- size
- Calls
std::mem::size_of()on the type given - skip
- Forward input to
log::info, prefixed with white[SKIP] - skip_
debug - Forward input to
log::debug, prefixed with white[SKIP] - skip_
trace - Forward input to
log::trace, prefixed with white[SKIP] - skip_
warn - Forward input to
log::warn, prefixed with white[SKIP] - sleep
- Sleep the current thread for
xseconds - sleep_
micros - Sleep the current thread for
xmicroseconds - sleep_
millis - Sleep the current thread for
xmilliseconds - sleep_
nanos - Sleep the current thread for
xnanoseconds - swap
- Calls
std::mem::swap(). This automatically appends&mutto both inputs. - take
- Calls
std::mem::take(). This automatically appends&muton the input. - third_
second - Expands to [
std::time::Duration::from_millis(333)] - threads
- Get the total available amount of threads as a
usize - unix
- Get the seconds elapsed
std::time::UNIX_EPOCH - unwrap_
or_ mass matchaResult,mass_panic!onResult::Err