| Crates.io | collection_macro |
| lib.rs | collection_macro |
| version | 0.1.4 |
| created_at | 2025-10-01 16:02:19.308907+00 |
| updated_at | 2025-10-13 21:15:44.168522+00 |
| description | General-purpose `seq![]` and `map! {}` collection macros |
| homepage | |
| repository | https://github.com/nik-rev/seq-map |
| max_upload_size | |
| id | 1862803 |
| size | 42,367 |
collection_macroThis crate provides the general-purpose seq![] and map! {} macros.
[dependencies]
collection_macro = "0.1"
We also show off how to bypass the Orphan Rule to create incredibly versatile macros.
These macros rely on type inference to determine the collection that they create.
The real power of these macros lies in the fact that work with absolutely any collection type, even collections from other crates.
seq![]Takes a list of expressions, and creates a sequence like Vec<T> or HashSet<T>:
let seq: Vec<_> = seq![1, 2, 3];
You can use the array syntax seq![expr; amount]:
let seq: Vec<_> = seq![0; 10];
assert_eq!(seq, vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
You can create non-empty sequences, like ones from the mitsein crate:
extern crate mitsein;
use collection_macro::{seq, Seq1Plus};
use mitsein::NonEmpty;
struct BypassOrphanRule;
// we usually can't implement external trait `Seq1Plus`
// for external struct `NonEmpty`,
// but because `BypassOrphanRule` is a local type, and it is
// inferred in the `seq!` macro, this works!
impl<T> Seq1Plus<BypassOrphanRule, T> for NonEmpty<Vec<T>> {
fn from_1(first: T, capacity: usize) -> Self {
NonEmpty::<Vec<T>>::from_one_with_capacity(first, capacity)
}
fn insert(&mut self, value: T) {
self.push(value);
}
}
// it just works!!
let seq: NonEmpty<Vec<_>> = seq![1, 2, 3];
assert_eq!(seq, NonEmpty::<Vec<_>>::from_head_and_tail(1, [2, 3]));
Non-empty sequences fail to compile if no arguments are provided:
let seq: NonEmpty<Vec<_>> = seq![];
Traits:
Seq0<T>, then it can be used with seq![] syntaxSeq1Plus<T>, then it can be used with 1+ argument to: seq![1, 2]Seq0<T> and Seq1Plus<T> then you can use the array syntax: seq![0; 10]seq! can be used with these standard library types by default:
But you can use it with any struct, even the ones from external crates by implementing the traits Seq0 and Seq1Plus.
Tips:
Vec<T>, implement both Seq0 and Seq1PlusNonEmpty<Vec<T>>, implement just Seq1Plus - then seq![] will be a compile errormap! {}Takes a list of key => value pairs, and creates a map like HashMap<K, V> or BTreeMap<K, V>:
let seq: HashMap<_, _> = map! {
'A' => 0x41,
'b' => 0x62,
'!' => 0x21
};
assert_eq!(seq, HashMap::from([('A', 0x41), ('b', 0x62), ('!', 0x21)]));
Traits:
Map0<K, V>, then it can be used with map! {} syntaxMap1Plus<K, V>, then it can be used with 1+ argument to: map! { 'A' => 0x41, 'b' => 0x62 }map! can be used with these standard library types by default:
But you can use it with any struct, even the ones from external crates by implementing the traits Map0 and Map1Plus.
Tips:
key => value pairs can such as HashMap<K, V>, implement both Map0 and Map1PlusNonEmpty<BTreeMap<K, V>>, implement just Map1Plus - then map! {} will be a compile error