#sugar #macro #sugars

sugars

A collection of macros in Rust to make some tasks easier to use or less verbose

7 releases (2 stable)

✓ Uses Rust 2018 edition

1.1.0 Mar 25, 2020
1.0.0 Sep 27, 2019
0.5.0 Aug 11, 2019
0.4.0 Jul 19, 2019
0.1.0 Jun 28, 2019

#56 in Rust patterns

Download history 33/week @ 2019-12-14 85/week @ 2019-12-21 143/week @ 2019-12-28 188/week @ 2020-01-04 74/week @ 2020-01-11 64/week @ 2020-01-18 4/week @ 2020-01-25 325/week @ 2020-02-01 242/week @ 2020-02-08 44/week @ 2020-02-15 284/week @ 2020-02-22 108/week @ 2020-02-29 99/week @ 2020-03-07 328/week @ 2020-03-14 170/week @ 2020-03-21 361/week @ 2020-03-28

664 downloads per month

Custom license

60KB
1.5K SLoC

Sugars - Nice Rust macros for better writing

Documentation Github Actions Build Status License Hits-of-Code

This crate provides a collection of macros to make some tasks easier to use on Rust ecosystem.

What it has to offer?

  • arc: A simple macro to make a new Arc value.³
  • boxed: A simple macro to make a new Box value.³
  • btmap: Create a BTreeMap from a list of key-value pairs
  • btset: Create a BTreeSet from a list of elements
  • cbtmap: Macro to BTreeMap collection comprehensions¹
  • cbtset: Macro to BTreeSet collection comprehensions¹
  • cell: A simple macro to make a new Cell value.³
  • cmap: Macro to HashMap collection comprehensions¹
  • cow: A simple macro to make a new Cow::Owned value.
  • cset: Macro to HashSet collection comprehensions¹
  • cvec: Macro to Vec collection comprehensions¹
  • dur: Creates a Duration object following a time pattern²
  • flkl: Create a LinkedList from a list of elements, adding each element to the start of the list.
  • hmap: Create a HashMap from a list of key-value pairs
  • hset: Create a HashSet from a list of elements
  • lkl: Create a LinkedList from a list of elements, adding each element to the end of the list.
  • mutex: A simple macro to make a new Mutex value.³
  • refcell: A simple macro to make a new RefCell value.³
  • rc: A simple macro to make a new Rc value.³
  • sleep: Makes a thread sleep a amount following a time pattern²
  • time: Print out the time it took to execute a given expression in seconds
  1. The comprehension macros supports a haskell-like as well as python-like writing syntax and have the limitation of not supporting nesting
  2. A time pattern can be: mim, sec, nano, micro, milli
  3. Also can return a tuple if is given more than one parameter

Examples

arc, boxed, cell, cow, mutex and refcell: Usage are mostly the same, just change de Types and macros

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

hmap and btmap: Usage are the same, just change HashMap to BTreeMap and hmap! to btmap!

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

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

assert_eq!(map, map2);

hset and btset: Usage are the same, just change HashSet to BTreeSet and hset! to btset!

let mut set = HashSet::new();
map.insert(1);
map.insert(2);

let set2 = hset!{1, 2};

assert_eq!(set, set2);

dur and sleep

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

assert_eq!(d1, d2);

// Sleeps uses the same syntax, but makes the thread sleep for the given time
sleep!(10 sec)

cvec: Notice that cvec can be nested up to 3 times max

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

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

cset and cbtset: Notice that cset/cbtset cannot be nested. Usage are the same, just change HashSet to BTreeSet and cset! to cbtset!

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

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

cmap and cbtmap: Notice that cmap/cbtmap cannot be nested. Usage are the same, just change HashMap to BTreeMap and cmap! to 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

// 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.38.

LICENSE

This software is licensed under the MIT Public License.

No runtime deps