Expand description
This 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.
§Usage
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:
- If your type implements
Seq0<T>, then it can be used withseq![]syntax - If your type implements
Seq1Plus<T>, then it can be used with 1+ argument to:seq![1, 2] - If your type implements both
Seq0<T>andSeq1Plus<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:
- For a sequence of 0 or more elements can such as
Vec<T>, implement bothSeq0andSeq1Plus - If your sequence is non-empty like
NonEmpty<Vec<T>>, implement justSeq1Plus- thenseq![]will be a compile error
§map! {}
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:
- If your type implements
Map0<K, V>, then it can be used withmap! {}syntax - If your type implements
Map1Plus<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:
- For a map of 0 or more
key => valuepairs can such asHashMap<K, V>, implement bothMap0andMap1Plus - If your map is non-empty like
NonEmpty<BTreeMap<K, V>>, implement justMap1Plus- thenmap! {}will be a compile error
Macros§
- map
- General-purpose macro for creating a map with keys pointing to values
- seq
- General-purpose macro for creating a sequence of items