11 releases (6 stable)

3.0.1 Feb 4, 2022
3.0.0 Dec 22, 2020
2.0.0 Sep 6, 2020
1.2.0 May 1, 2020
0.4.0 Jul 19, 2019

#263 in Rust patterns

Download history 478/week @ 2024-07-25 242/week @ 2024-08-01 498/week @ 2024-08-08 416/week @ 2024-08-15 730/week @ 2024-08-22 271/week @ 2024-08-29 329/week @ 2024-09-05 606/week @ 2024-09-12 468/week @ 2024-09-19 628/week @ 2024-09-26 629/week @ 2024-10-03 679/week @ 2024-10-10 788/week @ 2024-10-17 1105/week @ 2024-10-24 3512/week @ 2024-10-31 3978/week @ 2024-11-07

9,542 downloads per month
Used in 8 crates (7 directly)

Custom license

57KB
984 lines

Sugars - Nice Rust macros for better writing.

Crates.io Documentation License Github Actions Build Status Hits-of-Code

This crate provides a collection of useful macros to make tasks easier.

What it has to offer?

  • Macros for std::collections:
    • deque: Create VecDeque from list of elements.
    • hset: Create HashSet “ .
    • btset: Create BTreeSet “ .
    • bheap: Create BinaryHeap “ .
    • hmap: Create HashMap from key-value pairs.
    • btmap: Create BTreeMap “ .
    • lkl: Create LinkedList from list of elements.
    • rlkl: Create LinkedList, but reversed.
  • Macros for .collect() comprehensions:
    • c: Macro to make lazy Iterator collection comprehensions, others below are based on this one.
    • cbheap: Build BinaryHeap with collection comprehensions.
    • cbtmap: Build BTreeMap with “ .
    • cbtset: Build BTreeSet with “ .
    • cdeque: Build VecDeque with “ .
    • cmap: Build HashMap with “ .
    • cset: Build HashSet with “ .
    • cvec: Build Vec with “ .
  • Smart Pointers:
    • arc: Create new Arc.¹
    • boxed: Create new Box.¹
    • cell: Create new Cell.¹
    • mutex: Create new Mutex.¹
    • refcell: Create new RefCell.¹
    • rc: Create new Rc.¹
    • rwlock: Create new RwLock.¹
    • cow: Create new Cow.
  • Time/Duration:
    • dur: Creates a Duration object following a time pattern.²
    • sleep: Makes current thread sleep an custom time amount.²
    • time: Print out the time it took to execute a given expression in seconds.
  1. Returns a tuple if multiple parameters are given.
  2. Accepted time patterns are: min, sec, nano, micro and milli.

Examples

std::collections

Usage of boxed, same as arc, cell, cow, mutex and refcell:

assert_eq!(Box::new(10), boxed!(10));

Usage of hmap, same as btmap:

let mut map = HashMap::new();
map.insert("1", 1);
map.insert("2", 2);
map.insert("3", 3);

let map2 = hmap! {"1" => 1, "2" => 2, "3" => 3};

assert_eq!(map, map2);

Usage of hset, same as btset:

let set = hset! {1, 2, 3};

let mut set2 = HashSet::new();
set2.insert(1);
set2.insert(2);
set2.insert(3);

assert_eq!(set, set2);

Usage of deque, same as bheap, lkl and rlkl:

let deque = deque![1, 2, 3];

let mut deque2 = VecDeque::new();
deque2.push_back(1);
deque2.push_back(2);
deque2.push_back(3);

assert_eq!(deque2, deque);

Comprenhensions

Usage of c!: It follows the syntax: c![<expr>; <<pattern> in <iterator>, >...[, if <condition>]].

Note that it generates a lazy Iterator that needs to be dealt with.

let vec = c![x; x in 0..10].collect::<Vec<_>>();
let set = c![i*2; &i in vec.iter()].collect::<HashSet<_>>();
// A more complex one
let vec = c![i+j; i in vec.into_iter(), j in set.iter(), if i%2 == 0 && j%2 != 0].collect::<Vec<_>>();

// Or using type hints
let vec: Vec<_> = c![x; x in 0..10].collect();
let set: HashSet<_> = c![i*2; &i in vec.iter()].collect();
let vec: Vec<_> = c![i+j; i in vec.into_iter(), j in set.iter(), if i%2 == 0 && j%2 != 0].collect();

Usage of cvec!, same as cdeque!, clkl! and cbheap!:

// Normal comprehension
cvec![x; x in 0..10];

// You can filter as well
cvec![x; x in 0..10, if x % 2 == 0];

Usage of cset, same as cbtset:

// Normal comprehension
cset! {x; x in 0..10};

// You can filter as well
cset! {x; x in 0..10, if x % 2 == 0};

Usage of cmap, same as cbtmap:

// Normal comprehension
cmap! {x => x*2; x in 1..10};

// You can filter as well
cmap! {x => x*2; x in 1..10, if x % 2 == 0};

Time/Duration:

Usage of dur and sleep:

let d1 = dur!(10 sec);
let d2 = std::time::Duration::from_secs(10);

assert_eq!(d1, d2);

// Same syntax, but make the thread sleep for the given time
sleep!(10 sec)

Usage of time:

// Should print to stderr ≈ 2.0000 seconds
time!( sleep!(2 sec) );

// It also return the evaluated expression, like dbg! macro
let x = time!( 100 + 20 );

Minimal Viable Rust Version

This software requires Rust version equal or above 1.39.0.

LICENSE

This software is licensed under the MIT Public License.

No runtime deps