Crate collection_macro

Crate collection_macro 

Source
Expand description

crates.io docs.rs license msrv github

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 with seq![] 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> 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:

§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 with map! {} 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:

Macros§

map
General-purpose macro for creating a map with keys pointing to values
seq
General-purpose macro for creating a sequence of items

Traits§

Map0
Map that can be empty. Unlocks: map! {}
Map1Plus
Map that can have 1 or more elements. Unlocks: map! { a => b }
Seq0
Sequence that can be empty. Unlocks: seq![]
Seq1Plus
Sequence that can have 1 or more elements. Unlocks: seq![1]