collection_literals-1.0.2/.cargo_vcs_info.json 0000644 00000000136 00000000001 0015066 0 ustar {
"git": {
"sha1": "5c5e88a5cfbf5603123187039c4382366d46edd1"
},
"path_in_vcs": ""
} collection_literals-1.0.2/Cargo.lock 0000644 00000000243 00000000001 0013040 0 ustar # This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "collection_literals"
version = "1.0.2"
collection_literals-1.0.2/Cargo.toml 0000644 00000002644 00000000001 0013072 0 ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
name = "collection_literals"
version = "1.0.2"
build = false
exclude = [
"!/src",
"!/tests",
"!/README.md",
"/.github/",
"/.idea/",
"/.vscode/",
"/nix",
"/flake.nix",
"/flake.lock",
".envrc",
".gitignore",
]
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Easy-to-use macros for initializing any collection"
readme = "README.md"
keywords = [
"literal",
"macro",
"hashmap",
"collection",
"maplit",
]
categories = ["rust-patterns"]
license = "MIT"
repository = "https://github.com/staedoix/collection_literals"
[badges.maintenance]
status = "passively-maintained"
[lib]
name = "collection_literals"
path = "src/lib.rs"
[[test]]
name = "btree"
path = "tests/btree.rs"
[[test]]
name = "collection"
path = "tests/collection.rs"
[[test]]
name = "hash"
path = "tests/hash.rs"
[dependencies]
[lints.rust]
unsafe_code = "forbid"
collection_literals-1.0.2/Cargo.toml.orig 0000644 0000000 0000000 00000001403 10461020230 0016543 0 ustar 0000000 0000000 [package]
name = "collection_literals"
version = "1.0.2"
edition = "2021"
description = "Easy-to-use macros for initializing any collection"
readme = "README.md"
repository = "https://github.com/staedoix/collection_literals"
license = "MIT"
keywords = ["literal", "macro", "hashmap", "collection", "maplit"]
categories = ["rust-patterns"]
exclude = [
"!/src",
"!/tests",
"!/README.md",
"/.github/",
"/.idea/",
"/.vscode/",
"/nix",
"/flake.nix",
"/flake.lock",
".envrc",
".gitignore",
]
[badges]
maintenance.status = "passively-maintained"
[lints.rust]
unsafe_code = "forbid"
[[test]]
name = "collection"
path = "tests/collection.rs"
[[test]]
name = "hash"
path = "tests/hash.rs"
[[test]]
name = "btree"
path = "tests/btree.rs"
[dependencies]
collection_literals-1.0.2/README.md 0000644 0000000 0000000 00000010204 10461020230 0015132 0 ustar 0000000 0000000 # Collection Literals
Simple library for convenient collection initialization.
## `collection!` macro
Macro for initializing collections of any type.
```rust
use collection_literals::collection;
fn main() {
let empty_collection: HashMap = collection! {};
let empty_collection = collection! { HashMap:: };
let hash_set: HashSet = collection! { 1, 5, 9, 12 };
let hash_set = collection! { HashSet::; 1, 5, 9, 12 };
let hash_map: HashMap = collection! {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
};
let hash_map = collection! { HashMap::;
0 => '0',
1 => '1',
2 => '2',
3 => '3',
};
let btree_set: BTreeSet = collection! { 1, 5, 9, 12 };
let btree_set = collection! { BTreeSet::; 1, 5, 9, 12 };
let btree_map: BTreeMap = collection! {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
};
let btree_map = collection! { BTreeMap::;
0 => '0',
1 => '1',
2 => '2',
3 => '3',
};
}
```
## `hash!` macro
Macro for initializing both HashMaps and HashSets. It can infer both type of collection and types of entries, but you
can provide explicit type annotations.
```rust
use collection_literals::hash;
fn main() {
let empty_hash_map: HashMap = hash! {};
let empty_hash_map = hash! { map of String => bool };
let empty_hash_map = hash! { map of String => bool {} };
let empty_hash_map = hash! { of String => bool };
let empty_hash_map = hash! { of String => bool {} };
let empty_hash_set: HashSet = hash! {};
let empty_hash_set = hash! { set of u8 };
let empty_hash_set = hash! { set of u8 {} };
let empty_hash_set = hash! { of u8 };
let empty_hash_set = hash! { of u8 {} };
let hash_map: HashMap = hash! {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
};
let hash_map = hash! { map of u64 => char {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
}};
let hash_map = hash! { of u64 => char {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
}};
let hash_map = hash! {
0_u64 => '0',
1_u64 => '1',
2_u64 => '2',
3_u64 => '3',
};
let hash_set: HashSet = hash! { 1, 2, 3 };
let hash_set = hash! { set of u8 { 1, 2, 3 } };
let hash_set = hash! { of u8 { 1, 2, 3 } };
let hash_set = hash! { 1_u8, 2_u8, 3_u8 };
}
```
## `btree!` macro:
Macro for initializing both BTreeMaps and BTreeSets. It can infer both type of collection and types of entries, but you
can provide explicit type annotations.
```rust
use collection_literals::btree;
fn main() {
let empty_btree_map: BTreeMap = btree! {};
let empty_btree_map = btree! { map of String => bool };
let empty_btree_map = btree! { map of String => bool {} };
let empty_btree_map = btree! { of String => bool };
let empty_btree_map = btree! { of String => bool {} };
let empty_btree_set: BTreeSet = hash! {};
let empty_btree_set = btree! { set of u8 };
let empty_btree_set = btree! { set of u8 {} };
let empty_btree_set = btree! { of u8 };
let empty_btree_set = btree! { of u8 {} };
let btree_map: BTreeMap = btree! {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
};
let btree_map = btree! { map of u64 => char {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
}};
let btree_map = btree! { of u64 => char {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
}};
let btree_map = btree! {
0_u64 => '0',
1_u64 => '1',
2_u64 => '2',
3_u64 => '3',
};
let btree_set: BTreeSet = btree! { 1, 2, 3 };
let btree_set = btree! { set of u8 { 1, 2, 3 } };
let btree_set = btree! { of u8 { 1, 2, 3 } };
let btree_set = btree! { 1_u8, 2_u8, 3_u8 };
}
```
collection_literals-1.0.2/src/lib.rs 0000644 0000000 0000000 00000010737 10461020230 0015571 0 ustar 0000000 0000000 /// Macro for initializing collections of any type.
///
/// The collection must implement [From] array of values or key-value pairs.
/// [From] [IntoIterator] will also work.
/// You must specify type of collection.
/// ```rust
/// use std::collections::LinkedList;
/// use collection_literals::collection;
///
/// let linked_list: LinkedList = collection! { "Hello".to_string(), "Hallo".to_string() };
/// assert_eq!(linked_list, LinkedList::from(["Hello".to_string(), "Hallo".to_string()]));
///
/// let linked_list = collection! { LinkedList::<&str>; "Bonjour", "Здравствуй" };
/// assert_eq!(linked_list, LinkedList::from(["Bonjour", "Здравствуй"]));
/// ```
#[macro_export]
macro_rules! collection {
($collection_type:ty) => {{
<$collection_type>::new()
}};
// map-like
($collection_type:ty; $($key:expr => $value:expr),* $(,)?) => {{
let temp: $collection_type = core::convert::From::from([$(($key, $value),)*]);
temp
}};
($($key:expr => $value:expr),* $(,)?) => {{
core::convert::From::from([$(($key, $value),)*])
}};
// set-like
($collection_type:ty; $($value:expr),* $(,)?) => {{
let temp: $collection_type = core::convert::From::from([$($value,)*]);
temp
}};
($($value:expr),* $(,)?) => {{
core::convert::From::from([$($value,)*])
}};
}
/// Macro for initializing both HashMaps and HashSets.
/// It can infer both type of collection and types of entries but you can provide explicit type annotations.
/// ```rust
/// use std::collections::{HashMap, HashSet};
/// use collection_literals::hash;
///
/// let set = hash! { set of &str { "Hi", "Hoi" } };
/// let map = hash! { map of u8 => char {
/// 0 => '0',
/// 1 => '1',
/// 2 => '2',
/// }};
///
/// assert_eq!(set, HashSet::from(["Hi", "Hoi"]));
/// assert_eq!(map, HashMap::from([(0, '0'), (1, '1'), (2, '2')]));
///
///
/// assert_eq!(hash! { 88, 99 }, hash! { set of i32 { 88, 99 } });
/// assert_eq!(hash! { 88 => 99 }, hash! { map of i32 => i32 { 88 => 99 } });
///```
#[macro_export]
macro_rules! hash {
() => {{ collection!{} }};
// map-like
($(map)? of $key_type:ty => $value_type:ty) => {{
std::collections::HashMap::<$key_type, $value_type>::new()
}};
($($key:expr => $value:expr),* $(,)?) => {{
std::collections::HashMap::from([$(($key, $value),)*])
}};
($(map)? of $key_type:ty => $value_type:ty { $($key:expr => $value:expr),* $(,)? }) => {{
std::collections::HashMap::<$key_type, $value_type>::from([$(($key, $value),)*])
}};
// set-like
($(set)? of $value_type:ty) => {{
std::collections::HashSet::<$value_type>::new()
}};
($($value:expr),* $(,)?) => {{
std::collections::HashSet::from([$($value,)*])
}};
($(set)? of $value_type:ty { $($value:expr),* $(,)? }) => {{
std::collections::HashSet::<$value_type>::from([$($value,)*])
}};
}
/// Macro for initializing both BTreeMaps and BTreeSets.
/// It can infer both type of collection and types of entries but you can provide explicit type annotations.
/// ```rust
/// use std::collections::{BTreeMap, BTreeSet};
/// use collection_literals::btree;
///
/// let set = btree! { set of &str { "Hi", "Hoi" } };
/// let map = btree! { map of u8 => char {
/// 0 => '0',
/// 1 => '1',
/// 2 => '2',
/// }};
///
/// assert_eq!(set, BTreeSet::from(["Hi", "Hoi"]));
/// assert_eq!(map, BTreeMap::from([(0, '0'), (1, '1'), (2, '2')]));
///
///
/// assert_eq!(btree! { 88, 99 }, btree! { set of i32 { 88, 99 } });
/// assert_eq!(btree! { 88 => 99 }, btree! { map of i32 => i32 { 88 => 99 } });
///```
#[macro_export]
macro_rules! btree {
() => {{ collection!{} }};
// map-like
($(map)? of $key_type:ty => $value_type:ty) => {{
std::collections::BTreeMap::<$key_type, $value_type>::new()
}};
($($key:expr => $value:expr),* $(,)?) => {{
std::collections::BTreeMap::from([$(($key, $value),)*])
}};
($(map)? of $key_type:ty => $value_type:ty { $($key:expr => $value:expr),* $(,)? }) => {{
std::collections::BTreeMap::<$key_type, $value_type>::from([$(($key, $value),)*])
}};
// set-like
($(set)? of $value_type:ty) => {{
std::collections::BTreeSet::<$value_type>::new()
}};
($($value:expr),* $(,)?) => {{
std::collections::BTreeSet::from([$($value,)*])
}};
($(set)? of $value_type:ty { $($value:expr),* $(,)? }) => {{
std::collections::BTreeSet::<$value_type>::from([$($value,)*])
}};
}
collection_literals-1.0.2/tests/btree.rs 0000644 0000000 0000000 00000005020 10461020230 0016464 0 ustar 0000000 0000000 use std::collections::{BTreeMap, BTreeSet};
use collection_literals::{btree, collection};
#[test]
fn it_should_create_defaults() {
let tested_set: BTreeSet = btree! {};
let desired_set: BTreeSet = collection! {};
assert_eq!(tested_set, desired_set);
let tested_set: BTreeMap = btree! {};
let desired_set: BTreeMap = collection! {};
assert_eq!(tested_set, desired_set);
}
#[test]
fn it_should_properly_create_sets() {
let tested_set = btree! {1, 2, 3, 4, 5, 6, 7, 8, 9};
let desired_set: BTreeSet = collection! {1, 2, 3, 4, 5, 6, 7, 8, 9};
assert_eq!(tested_set, desired_set);
let tested_set = btree! {1, 1, 1, 8, 8, 8};
let desired_set: BTreeSet = collection! {1, 8};
assert_eq!(tested_set, desired_set);
}
fn is_prime>(number: T) -> bool {
let number = number.into();
let float = number as f64;
let s = float.sqrt().trunc() as i64;
for d in 2..=s {
if number % d == 0 {
return false;
}
}
true
}
#[test]
fn it_should_properly_create_maps() {
let tested_map = btree! {
1 => is_prime(1),
2 => is_prime(2),
3 => is_prime(3),
4 => is_prime(4),
5 => is_prime(5),
6 => is_prime(6),
7 => is_prime(7),
8 => is_prime(8),
9 => is_prime(9),
};
let desired_map: BTreeMap = collection! {
1 => is_prime(1),
2 => is_prime(2),
3 => is_prime(3),
4 => is_prime(4),
5 => is_prime(5),
6 => is_prime(6),
7 => is_prime(7),
8 => is_prime(8),
9 => is_prime(9),
};
assert_eq!(tested_map, desired_map);
let tested_map = btree! {
1 => true,
1 => true,
3 => true,
3 => false,
5 => true,
5 => false,
7 => true,
7 => false,
9 => false,
};
let desired_map: BTreeMap = collection! {
1 => true,
3 => false,
5 => false,
7 => false,
9 => false,
};
assert_eq!(tested_map, desired_map);
}
#[test]
fn it_should_create_the_same_sets_with_various_syntax() {
let set1 = btree! { set of u8 };
let set2 = btree!(set of u8);
let set3 = btree![set of u8];
let set4 = btree! { of u8 };
let set5 = btree!(of u8);
let set6 = btree![of u8];
let sets = vec![set1, set2, set3, set4, set5, set6];
for i in 0..sets.len() {
for j in i..sets.len() {
assert_eq!(sets[i], sets[j])
}
}
}
collection_literals-1.0.2/tests/collection.rs 0000644 0000000 0000000 00000007612 10461020230 0017527 0 ustar 0000000 0000000 use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use collection_literals::collection;
#[test]
fn it_should_create_defaults() {
let vector: Vec = collection![];
assert_eq!(vector, Vec::::default());
let hash_set: HashSet = collection! {};
assert_eq!(hash_set, HashSet::::default());
let btree_set: BTreeSet = collection! {};
assert_eq!(btree_set, BTreeSet::::default());
let hash_map: HashMap = collection! {};
assert_eq!(hash_map, HashMap::::default());
let btree_map: BTreeMap = collection! {};
assert_eq!(btree_map, BTreeMap::::default());
let vector = collection!(Vec);
assert_eq!(vector, Vec::::default());
let hash_set = collection!(HashSet);
assert_eq!(hash_set, HashSet::::default());
let btree_set = collection!(BTreeSet);
assert_eq!(btree_set, BTreeSet::::default());
let hash_map = collection!(HashMap);
assert_eq!(hash_map, HashMap::::default());
let btree_map = collection!(BTreeMap);
assert_eq!(btree_map, BTreeMap::::default());
}
#[test]
fn it_should_properly_create_hash_sets() {
let tested_set: HashSet = collection! {1, 2, 3, 4, 5, 6, 7, 8, 9};
let desired_set = HashSet::::from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
assert_eq!(tested_set, desired_set);
let tested_set: HashSet = collection! {1, 1, 1, 4, 4, 4, 8, 8, 8};
let desired_set = HashSet::::from([1, 4, 8]);
assert_eq!(tested_set, desired_set);
}
#[test]
fn it_should_properly_create_btree_sets() {
let tested_set: BTreeSet = collection! {1, 2, 3, 4, 5, 6, 7, 8, 9};
let desired_set = BTreeSet::::from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
assert_eq!(tested_set, desired_set);
let tested_set: BTreeSet = collection! {1, 1, 1, 4, 4, 4, 8, 8, 8};
let desired_set = BTreeSet::::from([1, 4, 8]);
assert_eq!(tested_set, desired_set);
}
fn is_prime>(number: T) -> bool {
let number = number.into();
let float = number as f64;
let s = float.sqrt().trunc() as i64;
for d in 2..=s {
if number % d == 0 {
return false;
}
}
true
}
#[test]
fn it_should_properly_create_hash_maps() {
let tested_map: HashMap = collection! {
1 => true,
2 => true,
3 => true,
4 => false,
5 => true,
6 => false,
7 => true,
8 => false,
9 => false,
};
let mut desired_map = HashMap::::new();
for i in 1..=9 {
desired_map.insert(i, is_prime(i));
}
assert_eq!(tested_map, desired_map);
let tested_map: HashMap = collection! {
1 => true,
1 => true,
3 => true,
3 => false,
5 => true,
5 => false,
7 => true,
7 => false,
9 => false,
};
let desired_map =
HashMap::::from([(1, true), (3, false), (5, false), (7, false), (9, false)]);
assert_eq!(tested_map, desired_map);
}
#[test]
fn it_should_properly_create_btree_maps() {
let tested_map: BTreeMap = collection! {
1 => true,
2 => true,
3 => true,
4 => false,
5 => true,
6 => false,
7 => true,
8 => false,
9 => false,
};
let mut desired_map = BTreeMap::::new();
for i in 1..=9 {
desired_map.insert(i, is_prime(i));
}
assert_eq!(tested_map, desired_map);
let tested_map: BTreeMap = collection! {
1 => true,
1 => true,
3 => true,
3 => false,
5 => true,
5 => false,
7 => true,
7 => false,
9 => false,
};
let desired_map =
BTreeMap::::from([(1, true), (3, false), (5, false), (7, false), (9, false)]);
assert_eq!(tested_map, desired_map);
}
collection_literals-1.0.2/tests/hash.rs 0000644 0000000 0000000 00000010552 10461020230 0016314 0 ustar 0000000 0000000 use std::collections::{HashMap, HashSet};
use collection_literals::{collection, hash};
#[test]
fn it_should_create_defaults() {
let set0: HashSet = hash! {};
let set1 = hash! { set of u8 };
let set2 = hash! { set of u8 {} };
let set3 = hash! { of u8 };
let set4 = hash! { of u8 {} };
let desired_set: HashSet = collection! {};
let sets = vec![set0, set1, set2, set3, set4, desired_set];
for i in 0..sets.len() {
for j in i..sets.len() {
assert_eq!(sets[i], sets[j])
}
}
let map0: HashMap = hash! {};
let map1 = hash! { map of u8 => bool };
let map2 = hash! { map of u8 => bool {} };
let map3 = hash! { of u8 => bool };
let map4 = hash! { of u8 => bool {} };
let desired_map: HashMap = collection! {};
let maps = vec![map0, map1, map2, map3, map4, desired_map];
for i in 0..maps.len() {
for j in i..maps.len() {
assert_eq!(maps[i], maps[j])
}
}
}
#[test]
fn it_should_properly_create_sets() {
let set0 = hash! {1, 2, 3, 4, 5, 6, 7, 8, 9};
let set1 = hash! { set of i32 {1, 2, 3, 4, 5, 6, 7, 8, 9} };
let set2 = hash! { of i32 {1, 2, 3, 4, 5, 6, 7, 8, 9} };
let desired_set: HashSet = collection! {1, 2, 3, 4, 5, 6, 7, 8, 9};
let sets = vec![set0, set1, set2, desired_set];
for i in 0..sets.len() {
for j in i..sets.len() {
assert_eq!(sets[i], sets[j])
}
}
let set0 = hash! {1, 1, 1, 8, 8, 8};
let set1 = hash! { set of i32 {1, 1, 1, 8, 8, 8} };
let set2 = hash! { of i32 {1, 1, 1, 8, 8, 8} };
let desired_set: HashSet = collection! {1, 8};
let sets = vec![set0, set1, set2, desired_set];
for i in 0..sets.len() {
for j in i..sets.len() {
assert_eq!(sets[i], sets[j])
}
}
}
fn is_prime>(number: T) -> bool {
let number = number.into();
let float = number as f64;
let s = float.sqrt().trunc() as i64;
for d in 2..=s {
if number % d == 0 {
return false;
}
}
true
}
#[test]
fn it_should_properly_create_maps() {
let map0 = hash! {
1 => is_prime(1),
2 => is_prime(2),
3 => is_prime(3),
4 => is_prime(4),
5 => is_prime(5),
6 => is_prime(6),
7 => is_prime(7),
8 => is_prime(8),
9 => is_prime(9),
};
let map1 = hash! { map of i32 => bool {
1 => is_prime(1),
2 => is_prime(2),
3 => is_prime(3),
4 => is_prime(4),
5 => is_prime(5),
6 => is_prime(6),
7 => is_prime(7),
8 => is_prime(8),
9 => is_prime(9),
}};
let map2 = hash! { of i32 => bool {
1 => is_prime(1),
2 => is_prime(2),
3 => is_prime(3),
4 => is_prime(4),
5 => is_prime(5),
6 => is_prime(6),
7 => is_prime(7),
8 => is_prime(8),
9 => is_prime(9),
}};
let desired_map: HashMap = collection! {
1 => is_prime(1),
2 => is_prime(2),
3 => is_prime(3),
4 => is_prime(4),
5 => is_prime(5),
6 => is_prime(6),
7 => is_prime(7),
8 => is_prime(8),
9 => is_prime(9),
};
let maps = vec![map0, map1, map2, desired_map];
for i in 0..maps.len() {
for j in i..maps.len() {
assert_eq!(maps[i], maps[j])
}
}
let map0 = hash! {
1 => true,
1 => true,
3 => true,
3 => false,
5 => true,
5 => false,
7 => true,
7 => false,
9 => false,
};
let map1 = hash! { map of i32 => bool {
1 => true,
1 => true,
3 => true,
3 => false,
5 => true,
5 => false,
7 => true,
7 => false,
9 => false,
}};
let map2 = hash! { of i32 => bool {
1 => true,
1 => true,
3 => true,
3 => false,
5 => true,
5 => false,
7 => true,
7 => false,
9 => false,
}};
let desired_map: HashMap = collection! {
1 => true,
3 => false,
5 => false,
7 => false,
9 => false,
};
let maps = vec![map0, map1, map2, desired_map];
for i in 0..maps.len() {
for j in i..maps.len() {
assert_eq!(maps[i], maps[j])
}
}
}