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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
//@ run-pass
//! Test deref patterns using string and bytestring literals.
#![feature(deref_patterns)]
#![allow(incomplete_features)]
fn main() {
for (test_in, test_expect) in [("zero", 0), ("one", 1), ("two", 2)] {
// Test string literal patterns having type `str`.
let test_actual = match *test_in {
"zero" => 0,
"one" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
// Test matching on `&mut str`.
let test_actual = match &mut *test_in.to_string() {
"zero" => 0,
"one" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
// Test string literals in deref patterns.
let test_actual = match test_in.to_string() {
deref!("zero") => 0,
"one" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
// Test peeling references in addition to smart pointers.
let test_actual = match &test_in.to_string() {
deref!("zero") => 0,
"one" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
}
// Test that we can still mutate in the match arm after using a literal to test equality:
let mut test = "test".to_string();
if let deref!(s @ "test") = &mut test {
s.make_ascii_uppercase();
}
assert_eq!(test, "TEST");
for (test_in, test_expect) in [(b"0", 0), (b"1", 1), (b"2", 2)] {
// Test byte string literal patterns having type `[u8; N]`
let test_actual = match *test_in {
b"0" => 0,
b"1" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
// Test byte string literal patterns having type `[u8]`
let test_actual = match *(test_in as &[u8]) {
b"0" => 0,
b"1" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
// Test matching on `&mut [u8; N]`.
let test_actual = match &mut test_in.clone() {
b"0" => 0,
b"1" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
// Test matching on `&mut [u8]`.
let test_actual = match &mut test_in.clone()[..] {
b"0" => 0,
b"1" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
// Test byte string literals used as arrays in deref patterns.
let test_actual = match Box::new(*test_in) {
deref!(b"0") => 0,
b"1" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
// Test byte string literals used as slices in deref patterns.
let test_actual = match test_in.to_vec() {
deref!(b"0") => 0,
b"1" => 1,
_ => 2,
};
assert_eq!(test_actual, test_expect);
}
}
|