{"id":18024214,"url":"https://github.com/grayjack/sugars","last_synced_at":"2025-04-10T00:18:36.565Z","repository":{"id":34996176,"uuid":"194045145","full_name":"GrayJack/sugars","owner":"GrayJack","description":"Syntatic sugar Rust macros","archived":false,"fork":false,"pushed_at":"2022-06-10T02:28:48.000Z","size":80,"stargazers_count":82,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"dev","last_synced_at":"2025-04-10T00:18:32.906Z","etag":null,"topics":["macros","rust","rust-crate","rust-lang"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GrayJack.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"GrayJack"}},"created_at":"2019-06-27T07:28:42.000Z","updated_at":"2025-03-27T11:01:05.000Z","dependencies_parsed_at":"2022-08-08T03:15:59.627Z","dependency_job_id":null,"html_url":"https://github.com/GrayJack/sugars","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GrayJack%2Fsugars","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GrayJack%2Fsugars/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GrayJack%2Fsugars/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GrayJack%2Fsugars/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GrayJack","download_url":"https://codeload.github.com/GrayJack/sugars/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131318,"owners_count":21052820,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["macros","rust","rust-crate","rust-lang"],"created_at":"2024-10-30T07:12:10.365Z","updated_at":"2025-04-10T00:18:36.540Z","avatar_url":"https://github.com/GrayJack.png","language":"Rust","funding_links":["https://github.com/sponsors/GrayJack"],"categories":[],"sub_categories":[],"readme":"# Sugars - Nice Rust macros for better writing.\n[![Crates.io](https://img.shields.io/crates/v/sugars.svg)](https://crates.io/crates/sugars)\n[![Documentation](https://docs.rs/sugars/badge.svg)](https://docs.rs/sugars)\n[![License](https://img.shields.io/github/license/GrayJack/sugars.svg)](./LICENSE)\n[![Github Actions](https://github.com/GrayJack/sugars/workflows/Build/badge.svg)](https://github.com/GrayJack/sugars/actions)\n[![Build Status](https://travis-ci.com/GrayJack/sugars.svg?branch=master)](https://travis-ci.com/GrayJack/sugars)\n[![Hits-of-Code](https://hitsofcode.com/github/GrayJack/sugars)](https://hitsofcode.com/view/github/GrayJack/sugars)\n\nThis crate provides a collection of useful macros to make tasks easier.\n\n## What it has to offer?\n * **Macros for `std::collections`:**\n    * [**deque**]: Create **`VecDeque`** from list of elements.\n    * [**hset**]: Create **`HashSet`** “ .\n    * [**btset**]: Create **`BTreeSet`** “ .\n    * [**bheap**]: Create **`BinaryHeap`** “ .\n    * [**hmap**]: Create **`HashMap`** from key-value pairs.\n    * [**btmap**]: Create **`BTreeMap`** “ .\n    * [**lkl**]: Create **`LinkedList`** from list of elements.\n    * [**rlkl**]: Create **`LinkedList`**, but reversed.\n * **Macros for `.collect()` comprehensions:**\n    * [**c**]: Macro to make lazy `Iterator` collection comprehensions, others below are\n      based on this one.\n    * [**cbheap**]: Build **`BinaryHeap`** with collection comprehensions.\n    * [**cbtmap**]: Build **`BTreeMap`** with “ .\n    * [**cbtset**]: Build **`BTreeSet`** with “ .\n    * [**cdeque**]: Build **`VecDeque`** with “ .\n    * [**cmap**]: Build **`HashMap`** with “ .\n    * [**cset**]: Build **`HashSet`** with “ .\n    * [**cvec**]: Build **`Vec`** with “ .\n * **Smart Pointers:**\n    * [**arc**]: Create new **`Arc`**.**¹**\n    * [**boxed**]: Create new **`Box`**.**¹**\n    * [**cell**]: Create new **`Cell`**.**¹**\n    * [**mutex**]: Create new **`Mutex`**.**¹**\n    * [**refcell**]: Create new **`RefCell`**.**¹**\n    * [**rc**]: Create new **`Rc`**.**¹**\n    * [**rwlock**]: Create new **`RwLock`**.**¹**\n    * [**cow**]: Create new **`Cow`**.\n * **Time/Duration:**\n    * [**dur**]: Creates a **`Duration`** object following a time pattern.**²**\n    * [**sleep**]: Makes current thread sleep an custom time amount.**²**\n    * [**time**]: Print out the time it took to execute a given expression in seconds.\n\n 1. Returns a tuple if multiple parameters are given.\n 2. Accepted time patterns are: `min`, `sec`, `nano`, `micro` and `milli`.\n\n## Examples\n### `std::collections`\nUsage of **`boxed`**, same as **`arc`**, **`cell`**, **`cow`**, **`mutex`** and **`refcell`**:\n```rust\nassert_eq!(Box::new(10), boxed!(10));\n```\n\nUsage of **`hmap`**, same as **`btmap`**:\n```rust\nlet mut map = HashMap::new();\nmap.insert(\"1\", 1);\nmap.insert(\"2\", 2);\nmap.insert(\"3\", 3);\n\nlet map2 = hmap! {\"1\" =\u003e 1, \"2\" =\u003e 2, \"3\" =\u003e 3};\n\nassert_eq!(map, map2);\n```\n\nUsage of **`hset`**, same as **``btset``**:\n```rust\nlet set = hset! {1, 2, 3};\n\nlet mut set2 = HashSet::new();\nset2.insert(1);\nset2.insert(2);\nset2.insert(3);\n\nassert_eq!(set, set2);\n```\n\nUsage of **`deque`**, same as **`bheap`**, **`lkl`** and **`rlkl`**:\n```rust\nlet deque = deque![1, 2, 3];\n\nlet mut deque2 = VecDeque::new();\ndeque2.push_back(1);\ndeque2.push_back(2);\ndeque2.push_back(3);\n\nassert_eq!(deque2, deque);\n```\n\n### Comprenhensions\nUsage of **`c!`**: It follows the syntax: `c![\u003cexpr\u003e; \u003c\u003cpattern\u003e in \u003citerator\u003e, \u003e...[, if \u003ccondition\u003e]]`.\n\nNote that it generates a lazy _Iterator_ that needs to be dealt with.\n```rust\nlet vec = c![x; x in 0..10].collect::\u003cVec\u003c_\u003e\u003e();\nlet set = c![i*2; \u0026i in vec.iter()].collect::\u003cHashSet\u003c_\u003e\u003e();\n// A more complex one\nlet vec = c![i+j; i in vec.into_iter(), j in set.iter(), if i%2 == 0 \u0026\u0026 j%2 != 0].collect::\u003cVec\u003c_\u003e\u003e();\n\n// Or using type hints\nlet vec: Vec\u003c_\u003e = c![x; x in 0..10].collect();\nlet set: HashSet\u003c_\u003e = c![i*2; \u0026i in vec.iter()].collect();\nlet vec: Vec\u003c_\u003e = c![i+j; i in vec.into_iter(), j in set.iter(), if i%2 == 0 \u0026\u0026 j%2 != 0].collect();\n```\n\nUsage of **`cvec!`**, same as **`cdeque!`**, **`clkl!`** and **`cbheap!`**:\n```rust\n// Normal comprehension\ncvec![x; x in 0..10];\n\n// You can filter as well\ncvec![x; x in 0..10, if x % 2 == 0];\n```\n\nUsage of **`cset`**, same as **`cbtset`**:\n```rust\n// Normal comprehension\ncset! {x; x in 0..10};\n\n// You can filter as well\ncset! {x; x in 0..10, if x % 2 == 0};\n```\n\nUsage of **`cmap`**, same as **`cbtmap`**:\n```rust\n// Normal comprehension\ncmap! {x =\u003e x*2; x in 1..10};\n\n// You can filter as well\ncmap! {x =\u003e x*2; x in 1..10, if x % 2 == 0};\n```\n\n### Time/Duration:\nUsage of **`dur`** and **`sleep`**:\n```rust\nlet d1 = dur!(10 sec);\nlet d2 = std::time::Duration::from_secs(10);\n\nassert_eq!(d1, d2);\n\n// Same syntax, but make the thread sleep for the given time\nsleep!(10 sec)\n```\n\nUsage of **`time`**:\n```rust\n// Should print to stderr ≈ 2.0000 seconds\ntime!( sleep!(2 sec) );\n\n// It also return the evaluated expression, like dbg! macro\nlet x = time!( 100 + 20 );\n```\n\n## Minimal Viable Rust Version\nThis software requires Rust version equal or above 1.39.0.\n\n## LICENSE\nThis software is licensed under the [MIT Public License](./LICENSE).\n\n[**deque**]: https://docs.rs/sugars/latest/sugars/macro.deque.html\n[**hset**]: https://docs.rs/sugars/latest/sugars/macro.hset.html\n[**btset**]: https://docs.rs/sugars/latest/sugars/macro.btset.html\n[**bheap**]: https://docs.rs/sugars/latest/sugars/macro.bheap.html\n[**hmap**]: https://docs.rs/sugars/latest/sugars/macro.hmap.html\n[**btmap**]: https://docs.rs/sugars/latest/sugars/macro.btmap.html\n[**lkl**]: https://docs.rs/sugars/latest/sugars/macro.lkl.html\n[**rlkl**]: https://docs.rs/sugars/latest/sugars/macro.rlkl.html\n[**c**]: https://docs.rs/sugars/latest/sugars/macro.c.html\n[**cbheap**]: https://docs.rs/sugars/latest/sugars/macro.cbheap.html\n[**cbtmap**]: https://docs.rs/sugars/latest/sugars/macro.cbtmap.html\n[**cbtset**]: https://docs.rs/sugars/latest/sugars/macro.cbtset.html\n[**cdeque**]: https://docs.rs/sugars/latest/sugars/macro.cdeque.html\n[**cmap**]: https://docs.rs/sugars/latest/sugars/macro.cmap.html\n[**cset**]: https://docs.rs/sugars/latest/sugars/macro.cset.html\n[**cvec**]: https://docs.rs/sugars/latest/sugars/macro.cvec.html\n[**arc**]: https://docs.rs/sugars/latest/sugars/macro.arc.html\n[**boxed**]: https://docs.rs/sugars/latest/sugars/macro.boxed.html\n[**cell**]: https://docs.rs/sugars/latest/sugars/macro.cell.html\n[**mutex**]: https://docs.rs/sugars/latest/sugars/macro.mutex.html\n[**refcell**]: https://docs.rs/sugars/latest/sugars/macro.refcell.html\n[**rc**]: https://docs.rs/sugars/latest/sugars/macro.rc.html\n[**rwlock**]: https://docs.rs/sugars/latest/sugars/macro.rwlock.html\n[**cow**]: https://docs.rs/sugars/latest/sugars/macro.cow.html\n[**dur**]: https://docs.rs/sugars/latest/sugars/macro.dur.html\n[**sleep**]: https://docs.rs/sugars/latest/sugars/macro.sleep.html\n[**time**]: https://docs.rs/sugars/latest/sugars/macro.time.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrayjack%2Fsugars","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrayjack%2Fsugars","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrayjack%2Fsugars/lists"}