Debian Codegraph @

debian/missing-sources/derive_more/tests/try_unwrap.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![cfg_attr(nightly, feature(never_type))]
3 #![allow(dead_code)] // some code is tested for type checking only
4
5 #[cfg(not(feature = "std"))]
6 extern crate alloc;
7
8 #[cfg(not(feature = "std"))]
9 use alloc::string::ToString as _;
10
11 use derive_more::TryUnwrap;
12
13 #[derive(TryUnwrap)]
14 enum Either<TLeft, TRight> {
15 Left(TLeft),
16 Right(TRight),
17 }
18
19 #[derive(TryUnwrap)]
20 #[derive(Debug, PartialEq)]
21 #[try_unwrap(ref, ref_mut)]
22 enum Maybe<T> {
23 Nothing,
24 Just(T),
25 }
26
27 #[derive(TryUnwrap)]
28 enum Color {
29 Rgb(u8, u8, u8),
30 Cmyk(u8, u8, u8, u8),
31 }
32
33 /// With lifetime
34 #[derive(TryUnwrap)]
35 enum Nonsense<'a, T> {
36 Ref(&'a T),
37 NoRef,
38 #[try_unwrap(ignore)]
39 NoRefIgnored,
40 }
41
42 #[derive(TryUnwrap)]
43 enum WithConstraints<T>
44 where
45 T: Copy,
46 {
47 One(T),
48 Two,
49 }
50
51 #[derive(TryUnwrap)]
52 enum KitchenSink<'a, 'b, T1: Copy, T2: Clone>
53 where
54 T2: Into<T1> + 'b,
55 {
56 Left(&'a T1),
57 Right(&'b T2),
58 OwnBoth(T1, T2),
59 Empty,
60 NeverMind(),
61 NothingToSeeHere(),
62 }
63
64 /// Single variant enum
65 #[derive(TryUnwrap)]
66 enum Single {
67 Value(i32),
68 }
69
70 #[derive(TryUnwrap)]
71 #[derive(Debug, PartialEq)]
72 #[try_unwrap(ref, ref_mut)]
73 enum Tuple<T> {
74 None,
75 Single(T),
76 Double(T, T),
77 Triple(T, T, T),
78 }
79
80 #[test]
81 pub fn test_try_unwrap() {
82 assert_eq!(Maybe::<()>::Nothing.try_unwrap_nothing().ok(), Some(()));
83 assert_eq!(Maybe::Just(1).try_unwrap_just_ref().ok(), Some(&1));
84 assert_eq!(Maybe::Just(42).try_unwrap_just_mut().ok(), Some(&mut 42));
85
86 assert_eq!(
87 Maybe::<()>::Nothing.try_unwrap_just().map_err(|e| e.input),
88 Err(Maybe::<()>::Nothing),
89 );
90 assert_eq!(
91 Maybe::Just(1).try_unwrap_nothing_ref().map_err(|e| e.input),
92 Err(&Maybe::Just(1)),
93 );
94 assert_eq!(
95 Maybe::Just(42)
96 .try_unwrap_nothing_mut()
97 .map_err(|e| e.to_string()),
98 Err(
99 "Attempt to call `Maybe::try_unwrap_nothing_mut()` on a `Maybe::Just` value"
100 .to_string()
101 ),
102 );
103 }
104
105 #[test]
106 pub fn test_try_unwrap_mut_1() {
107 let mut value = Tuple::Double(1, 12);
108
109 if let Ok((a, b)) = value.try_unwrap_double_mut() {
110 *a = 9;
111 *b = 10;
112 }
113
114 assert_eq!(value, Tuple::Double(9, 10));
115 }
116
117 #[test]
118 pub fn test_try_unwrap_mut_2() {
119 let mut value = Tuple::Single(128);
120
121 if let Ok(x) = value.try_unwrap_single_mut() {
122 *x *= 2;
123 }
124
125 if let Err(e) = value.try_unwrap_none_mut() {
126 let x = *e.input.try_unwrap_single_ref().unwrap_or(&0);
127 *e.input = Tuple::Double(x - 1, x);
128 }
129
130 assert_eq!(value, Tuple::Double(255, 256));
131 }
132
133 #[cfg(nightly)]
134 mod never {
135 use super::*;
136
137 #[derive(TryUnwrap)]
138 enum Enum {
139 Tuple(!),
140 TupleMulti(i32, !),
141 }
142 }
143
144 mod deprecated {
145 use super::*;
146
147 #[derive(TryUnwrap)]
148 #[deprecated(note = "enum")]
149 enum Enum {
150 #[deprecated(note = "variant")]
151 Tuple(#[deprecated(note = "field")] i32),
152 #[deprecated(note = "variant")]
153 TupleMulti(i32, #[deprecated(note = "field")] i32),
154 }
155 }