3
3
// instead of the target type itself.
4
4
// You can read more about it at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
5
5
use std:: convert:: { TryFrom , TryInto } ;
6
+ use std:: error;
6
7
7
8
#[ derive( Debug , PartialEq ) ]
8
9
struct Color {
@@ -24,19 +25,19 @@ struct Color {
24
25
25
26
// Tuple implementation
26
27
impl TryFrom < ( i16 , i16 , i16 ) > for Color {
27
- type Error = String ;
28
+ type Error = Box < dyn error :: Error > ;
28
29
fn try_from ( tuple : ( i16 , i16 , i16 ) ) -> Result < Self , Self :: Error > { }
29
30
}
30
31
31
32
// Array implementation
32
33
impl TryFrom < [ i16 ; 3 ] > for Color {
33
- type Error = String ;
34
+ type Error = Box < dyn error :: Error > ;
34
35
fn try_from ( arr : [ i16 ; 3 ] ) -> Result < Self , Self :: Error > { }
35
36
}
36
37
37
38
// Slice implementation
38
39
impl TryFrom < & [ i16 ] > for Color {
39
- type Error = String ;
40
+ type Error = Box < dyn error :: Error > ;
40
41
fn try_from ( slice : & [ i16 ] ) -> Result < Self , Self :: Error > { }
41
42
}
42
43
@@ -76,41 +77,43 @@ mod tests {
76
77
}
77
78
#[ test]
78
79
fn test_tuple_correct ( ) {
79
- let c: Result < Color , String > = ( 183 , 65 , 14 ) . try_into ( ) ;
80
+ let c: Result < Color , _ > = ( 183 , 65 , 14 ) . try_into ( ) ;
81
+ assert ! ( c. is_ok( ) ) ;
80
82
assert_eq ! (
81
- c,
82
- Ok ( Color {
83
+ c. unwrap ( ) ,
84
+ Color {
83
85
red: 183 ,
84
86
green: 65 ,
85
87
blue: 14
86
- } )
88
+ }
87
89
) ;
88
90
}
89
91
#[ test]
90
92
fn test_array_out_of_range_positive ( ) {
91
- let c: Result < Color , String > = [ 1000 , 10000 , 256 ] . try_into ( ) ;
93
+ let c: Result < Color , _ > = [ 1000 , 10000 , 256 ] . try_into ( ) ;
92
94
assert ! ( c. is_err( ) ) ;
93
95
}
94
96
#[ test]
95
97
fn test_array_out_of_range_negative ( ) {
96
- let c: Result < Color , String > = [ -10 , -256 , -1 ] . try_into ( ) ;
98
+ let c: Result < Color , _ > = [ -10 , -256 , -1 ] . try_into ( ) ;
97
99
assert ! ( c. is_err( ) ) ;
98
100
}
99
101
#[ test]
100
102
fn test_array_sum ( ) {
101
- let c: Result < Color , String > = [ -1 , 255 , 255 ] . try_into ( ) ;
103
+ let c: Result < Color , _ > = [ -1 , 255 , 255 ] . try_into ( ) ;
102
104
assert ! ( c. is_err( ) ) ;
103
105
}
104
106
#[ test]
105
107
fn test_array_correct ( ) {
106
- let c: Result < Color , String > = [ 183 , 65 , 14 ] . try_into ( ) ;
108
+ let c: Result < Color , _ > = [ 183 , 65 , 14 ] . try_into ( ) ;
109
+ assert ! ( c. is_ok( ) ) ;
107
110
assert_eq ! (
108
- c,
109
- Ok ( Color {
111
+ c. unwrap ( ) ,
112
+ Color {
110
113
red: 183 ,
111
114
green: 65 ,
112
115
blue: 14
113
- } )
116
+ }
114
117
) ;
115
118
}
116
119
#[ test]
@@ -131,14 +134,15 @@ mod tests {
131
134
#[ test]
132
135
fn test_slice_correct ( ) {
133
136
let v = vec ! [ 183 , 65 , 14 ] ;
134
- let c: Result < Color , String > = Color :: try_from ( & v[ ..] ) ;
137
+ let c: Result < Color , _ > = Color :: try_from ( & v[ ..] ) ;
138
+ assert ! ( c. is_ok( ) ) ;
135
139
assert_eq ! (
136
- c,
137
- Ok ( Color {
140
+ c. unwrap ( ) ,
141
+ Color {
138
142
red: 183 ,
139
143
green: 65 ,
140
144
blue: 14
141
- } )
145
+ }
142
146
) ;
143
147
}
144
148
#[ test]
0 commit comments