sugars/lib.rs
1//! [](https://docs.rs/sugars)
2//! [](https://github.com/GrayJack/sugars/blob/master/LICENSE)
3//!
4//! # Sugars - Nice macros for better writing
5//!
6//! This crate provides a collection of useful macros to make tasks easier.
7//!
8//! ## What it has to offer?
9//! * **Macros for [`std::collections`]:**
10//! * [**deque**]: Create [`VecDeque`] from list of elements.
11//! * [**hset**]: Create [`HashSet`] “ .
12//! * [**btset**]: Create [`BTreeSet`] “ .
13//! * [**bheap**]: Create [`BinaryHeap`] “ .
14//! * [**hmap**]: Create [`HashMap`] from key-value pairs.
15//! * [**btmap**]: Create [`BTreeMap`] “ .
16//! * [**lkl**]: Create [`LinkedList`] from list of elements.
17//! * [**rlkl**]: Create [`LinkedList`], but reversed.
18//! * **Macros for `.collect()` comprehensions:**
19//! * [**c**]: Macro to make lazy Iterator collection comprehensions, others below are
20//! based on this one.
21//! * [**cbheap**]: Macro to [`BinaryHeap`] collection comprehensions.
22//! * [**cbtmap**]: Macro to [`BTreeMap`] “ .
23//! * [**cbtset**]: Macro to [`BTreeSet`] “ .
24//! * [**cdeque**]: Macro to [`VecDeque`] “ .
25//! * [**cmap**]: Macro to [`HashMap`] “ .
26//! * [**cset**]: Macro to [`HashSet`] “ .
27//! * [**cvec**]: Macro to [`Vec`] “ .
28//! * **Smart Pointers:**
29//! * [**arc**]: Create new [`Arc`].**¹**
30//! * [**boxed**]: Create new [`Box`].**¹**
31//! * [**cell**]: Create new [`Cell`].**¹**
32//! * [**mutex**]: Create new [`Mutex`].**¹**
33//! * [**refcell**]: Create new [`RefCell`].**¹**
34//! * [**rc**]: Create new [`Rc`].**¹**
35//! * [**rwlock**]: Create new [`RwLock`].**¹**
36//! * [**cow**]: Create new [`Cow`].
37//! * **Time/Duration:**
38//! * [**dur**]: Creates a [`Duration`] object following a time pattern.**²**
39//! * [**sleep**]: Makes current thread sleep an amount following a time pattern.**²**
40//! * [**time**]: Print out the time it took to execute a given expression in seconds.
41//!
42//! 1. Returns a tuple if multiple parameters are given.
43//! 2. Accepted time patterns are: `min`, `sec`, `nano`, `micro` and `milli`.
44//!
45//! ## Examples
46//! ### `std::collections`
47//! Usage of **`boxed`**, same as **`arc`**, **`cell`**, **`cow`**, **`mutex`** and **`refcell`**:
48//! ```rust
49//! use sugars::boxed;
50//! assert_eq!(Box::new(10), boxed!(10));
51//! ```
52//!
53//! Usage of **`hmap`**, same as **`btmap`**:
54//! ```rust
55//! use std::collections::HashMap;
56//! use sugars::hmap;
57//!
58//! let map = hmap! {"1" => 1, "2" => 2, "3" => 3};
59//!
60//! let mut map2 = HashMap::new();
61//! map2.insert("1", 1);
62//! map2.insert("2", 2);
63//! map2.insert("3", 3);
64//!
65//! assert_eq!(map, map2);
66//! ```
67//!
68//! Usage of **`hset`**, same as **``btset``**:
69//! ```rust
70//! use std::collections::HashSet;
71//! use sugars::hset;
72//!
73//! let set = hset! {1, 2, 3};
74//!
75//! let mut set2 = HashSet::new();
76//! set2.insert(1);
77//! set2.insert(2);
78//! set2.insert(3);
79//!
80//! assert_eq!(set, set2);
81//! ```
82//!
83//! Usage of **`deque`**, same as **`bheap`**, **`lkl`** and **`rlkl`**:
84//! ```rust
85//! use sugars::deque;
86//! use std::collections::VecDeque;
87//! let deque = deque![1, 2, 3];
88//!
89//! let mut deque2 = VecDeque::new();
90//! deque2.push_back(1);
91//! deque2.push_back(2);
92//! deque2.push_back(3);
93//!
94//! assert_eq!(deque2, deque);
95//! ```
96//!
97//! ### Comprenhensions
98//! Usage of **`c!`**: It follows the syntax: `c![<expr>; <<pattern> in <iterator>, >...[, if <condition>]]`.
99//!
100//! Note that it generates a lazy _Iterator_ that needs to be dealt with.
101//! ```rust
102//! use std::collections::HashSet;
103//! use sugars::c;
104//!
105//! let vec = c![x; x in 0..10].collect::<Vec<_>>();
106//! let set = c![i*2; &i in vec.iter()].collect::<HashSet<_>>();
107//! // A more complex one
108//! let vec = c![i+j; i in vec.into_iter(), j in set.iter(), if i%2 == 0 && j%2 != 0].collect::<Vec<_>>();
109//!
110//! // Or using type hints
111//! let vec: Vec<_> = c![x; x in 0..10].collect();
112//! let set: HashSet<_> = c![i*2; &i in vec.iter()].collect();
113//! let vec: Vec<_> = c![i+j; i in vec.into_iter(), j in set.iter(), if i%2 == 0 && j%2 != 0].collect();
114//! ```
115//!
116//! Usage of **`cvec`**, same as **`cdeque`**, **`clkl`** and **`cbheap`**:
117//! ```rust
118//! use sugars::cvec;
119//!
120//! // Normal comprehension
121//! cvec![x; x in 0..10];
122//!
123//! // You can filter as well
124//! cvec![x; x in 0..10, if x % 2 == 0];
125//! ```
126//!
127//! Usage of **`cset`**, same as **`cbtset`**:
128//! ```rust
129//! use sugars::cset;
130//!
131//! // Normal comprehension
132//! cset! {x; x in 0..10};
133//!
134//! // You can filter as well
135//! cset! {x; x in 0..10, if x % 2 == 0};
136//! ```
137//!
138//! Usage of **`cmap`**, same as **`cbtmap`**:
139//! ```rust
140//! use sugars::cmap;
141//!
142//! // Normal comprehension
143//! cmap! {x => x*2; x in 1..10};
144//!
145//! // You can filter as well
146//! cmap! {x => x*2; x in 1..10, if x % 2 == 0};
147//! ```
148//!
149//! ### Time/Duration:
150//! Usage of **`dur`** and **`sleep`**:
151//! ```rust
152//! use sugars::{dur, sleep};
153//!
154//! let d1 = dur!(10 sec);
155//! let d2 = std::time::Duration::from_secs(10);
156//!
157//! assert_eq!(d1, d2);
158//!
159//! // Same syntax, but make the thread sleep for the given time
160//! sleep!(10 sec)
161//! ```
162//!
163//! Usage of **`time`**:
164//! ```rust
165//! use sugars::{time, sleep};
166//!
167//! // Should print to stderr ≈ 2.0000 seconds
168//! time!( sleep!(2 sec) );
169//!
170//! // It also return the evaluated expression, like dbg! macro
171//! let x = time!( 100 + 20 );
172//! ```
173//!
174//! ## Minimal Viable Rust Version
175//! This software requires Rust version equal or above 1.39.0.
176//!
177//! ## LICENSE
178//! This software is licensed under the [MIT Public License](./LICENSE).
179//!
180//! [**deque**]: deque
181//! [**hset**]: hset
182//! [**btset**]: btset
183//! [**bheap**]: bheap
184//! [**hmap**]: hmap
185//! [**btmap**]: btmap
186//! [**lkl**]: lkl
187//! [**rlkl**]: rlkl
188//! [**c**]: c
189//! [**cbheap**]: cbheap
190//! [**cbtmap**]: cbtmap
191//! [**cbtset**]: cbtset
192//! [**cdeque**]: cdeque
193//! [**cmap**]: cmap
194//! [**cset**]: cset
195//! [**cvec**]: cvec
196//! [**arc**]: arc
197//! [**boxed**]: boxed
198//! [**cell**]: cell
199//! [**mutex**]: mutex
200//! [**refcell**]: refcell
201//! [**rc**]: rc
202//! [**rwlock**]: rwlock
203//! [**cow**]: cow
204//! [**dur**]: dur
205//! [**sleep**]: sleep
206//! [**time**]: time
207//!
208//! [`BinaryHeap`]: ::std::collections::BinaryHeap
209//! [`BTreeMap`]: ::std::collections::BTreeMap
210//! [`BTreeSet`]: ::std::collections::BTreeSet
211//! [`HashMap`]: ::std::collections::HashMap
212//! [`HashSet`]: ::std::collections::HashSet
213//! [`VecDeque`]: ::std::collections::VecDeque
214//! [`LinkedList`]: ::std::collections::LinkedList
215//! [`Arc`]: ::std::sync::Arc
216//! [`Cell`]: ::std::cell::Cell
217//! [`Mutex`]: ::std::sync::Mutex
218//! [`Rc`]: ::std::rc::Rc
219//! [`RefCell`]: ::std::cell::RefCell
220//! [`RwLock`]: ::std::sync::RwLock
221//! [`Duration`]: ::std::time::Duration
222//! [`Cow`]: ::std::borrow::Cow
223
224mod collections;
225mod comprehension;
226mod hash;
227mod pointer;
228mod times;