btree_map_from!() { /* proc-macro */ }
Expand description

An initializer for BTreeMap that works the same as btree_map! except that values can be of any type that can be converted into the collection’s item type via an Into implementation.

The type of the item must be known at compile time, and usually this means an explicit type annotation is required.

Usage

use velcro::btree_map_from;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Foo(u64);

impl From<u64> for Foo {
    fn from(other: u64) -> Self {
        Foo(other)
    }
}

let mut map1 = BTreeMap::new();
map1.insert('a', Foo(0));
map1.insert('b', Foo(1));
map1.insert('c', Foo(1));
map1.insert('d', Foo(1));
map1.insert('e', Foo(1));
map1.insert('f', Foo(2));

let map2: BTreeMap<char, Foo> = btree_map_from! {
    'a': 0,
    ..('b'..='e'): 1,
    'f': 2
};

assert_eq!(map1, map2);