Crate sugars

Crate sugars 

Source
Expand description

Documentation License

§Sugars - Nice macros for better writing

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

§What it has to offer?

  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:

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

Usage of hmap, same as btmap:

use std::collections::HashMap;
use sugars::hmap;

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

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

assert_eq!(map, map2);

Usage of hset, same as btset:

use std::collections::HashSet;
use sugars::hset;

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:

use sugars::deque;
use std::collections::VecDeque;
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.

use std::collections::HashSet;
use sugars::c;

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:

use sugars::cvec;

// 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:

use sugars::cset;

// 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:

use sugars::cmap;

// 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:

use sugars::{dur, 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:

use sugars::{time, sleep};

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

Macros§

arc
Create a new Arc.
bheap
Create a BinaryHeap from a list of elements.
boxed
Create a new Box.
btmap
Create a BTreeMap from a list of key-value pairs.
btset
Create a BTreeSet from a list of elements.
c
Generic lazy iterator comprehensions.
cbheap
Build BinaryHeap from collection iterator comprehensions.
cbtmap
Build BTreeMap from collection iterator comprehensions.
cbtset
Build BTreeSet from collection iterator comprehensions.
cdeque
Build VecDeque from collection iterator comprehensions.
cell
Create a new Cell.
clkl
Build LinkedList from collection iterator comprehensions.
cmap
Build HashMap from collection iterator comprehensions.
cow
Create new Cow type.
cset
Build HashSet from collection iterator comprehensions.
cvec
Build Vec from collection iterator comprehensions.
deque
Create a VecDeque from a list of elements.
dur
Creates a Duration object following a time pattern.
hash
Macro that return the hash of what is passed and also can receive a hasher to use that intead of default HashMap Hasher.
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.
mutex
Create a new Mutex.
rc
Create a new Rc.
refcell
Create a new RefCell.
rlkl
Create a reversed LinkedList from a list of elements.
rwlock
Create a new RwLock.
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.