/
lib.rs
67 lines (56 loc) 路 1.53 KB
/
lib.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
67
use std::panic;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
#[test]
fn next_chunk_fewer() {
let iter = [1, 2, 3].into_iter();
let res: Result<[i32; 4], _> = arrays::from_iter(iter);
let mut rem = res.unwrap_err();
assert_eq!(rem.next(), Some(1));
assert_eq!(rem.next(), Some(2));
assert_eq!(rem.next(), Some(3));
}
#[test]
fn next_chunk_panic() {
static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug)]
struct Foo;
impl Drop for Foo {
fn drop(&mut self) {
DROP_COUNT.fetch_add(1, Ordering::SeqCst);
}
}
let iter = [Foo, Foo, Foo].into_iter().enumerate().map(|(i, f)| {
if i == 2 {
panic!("馃槺");
}
f
});
let res = panic::catch_unwind(|| {
let _: [Foo; 3] = arrays::from_iter(iter).unwrap();
});
assert!(res.is_err());
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3);
}
#[test]
fn next_chunk_unchecked_panic() {
static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug)]
struct Foo;
impl Drop for Foo {
fn drop(&mut self) {
DROP_COUNT.fetch_add(1, Ordering::SeqCst);
}
}
let iter = [Foo, Foo, Foo].into_iter().enumerate().map(|(i, f)| {
if i == 2 {
panic!("馃槺");
}
f
});
let res = panic::catch_unwind(|| {
let _: [Foo; 3] = unsafe { arrays::from_iter_unchecked(iter) };
});
assert!(res.is_err());
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3);
}