File: collections_generics.rs

package info (click to toggle)
rust-buildstructor 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 532 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,205 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::collections::{HashMap, HashSet};
use std::hash::Hash;

use buildstructor::buildstructor;

pub struct Collections {
    map: HashMap<String, String>,
    set: HashSet<String>,
}

#[buildstructor]
impl Collections {
    #[builder]
    fn new<K: Into<String> + Eq + Hash, V: Into<String>>(
        map: HashMap<K, V>,
        set: HashSet<K>,
    ) -> Collections {
        Self {
            map: map.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
            set: set.into_iter().map(|v| v.into()).collect(),
        }
    }
}

fn main() {
    let collections = Collections::builder()
        .map_entry("Appa", "1")
        .map_entry("Momo", "2")
        .set_entry("Aang")
        .set_entry("Katara")
        .set_entry("Sokka")
        .set_entry("Toph")
        .build();
    assert_eq!(
        collections.map,
        HashMap::from([
            ("Appa".to_string(), "1".to_string()),
            ("Momo".to_string(), "2".to_string())
        ])
    );
    assert_eq!(
        collections.set,
        HashSet::from([
            ("Aang".to_string()),
            ("Katara".to_string()),
            ("Sokka".to_string()),
            ("Toph".to_string()),
        ])
    );
}