forked from yegor256/micromap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom.rs
66 lines (55 loc) · 1.53 KB
/
from.rs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-FileCopyrightText: Copyright (c) 2023-2025 Yegor Bugayenko
// SPDX-License-Identifier: MIT
use crate::Map;
impl<K: PartialEq, V, const N: usize> FromIterator<(K, V)> for Map<K, V, N> {
#[inline]
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let mut m: Self = Self::new();
for (k, v) in iter {
m.insert(k, v);
}
m
}
}
impl<K: PartialEq, V, const N: usize> From<[(K, V); N]> for Map<K, V, N> {
#[inline]
fn from(arr: [(K, V); N]) -> Self {
Self::from_iter(arr)
}
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_ARRAY: [(i32, &str); 5] =
[(1, "sun"), (2, "mon"), (3, "tue"), (4, "wed"), (5, "thu")];
#[test]
fn from_iter() {
let vec = Vec::from(TEST_ARRAY);
let m: Map<i32, &str, 10> = Map::from_iter(vec);
assert_eq!(m.len(), 5);
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn from_larger_iter() {
let vec = Vec::from(TEST_ARRAY);
let _m: Map<i32, &str, 1> = Map::from_iter(vec);
}
#[test]
fn from_array() {
let m = Map::from(TEST_ARRAY);
assert_eq!(m.len(), 5);
}
#[test]
fn array_into_map() {
let m: Map<i32, &str, 5> = TEST_ARRAY.into();
assert_eq!(m.len(), 5);
}
#[test]
fn from_with_duplicates() {
let arr = [(1, "sun"), (2, "mon"), (3, "tue"), (1, "wed"), (2, "thu")];
let m = Map::from(arr);
assert_eq!(m.len(), 3);
assert_eq!(m[&2], "thu");
}
}