33// instead of the target type itself.
44// You can read more about it at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
55use std:: convert:: { TryFrom , TryInto } ;
6+ use std:: error;
67
78#[ derive( Debug , PartialEq ) ]
89struct Color {
@@ -24,19 +25,19 @@ struct Color {
2425
2526// Tuple implementation
2627impl TryFrom < ( i16 , i16 , i16 ) > for Color {
27- type Error = String ;
28+ type Error = Box < dyn error :: Error > ;
2829 fn try_from ( tuple : ( i16 , i16 , i16 ) ) -> Result < Self , Self :: Error > { }
2930}
3031
3132// Array implementation
3233impl TryFrom < [ i16 ; 3 ] > for Color {
33- type Error = String ;
34+ type Error = Box < dyn error :: Error > ;
3435 fn try_from ( arr : [ i16 ; 3 ] ) -> Result < Self , Self :: Error > { }
3536}
3637
3738// Slice implementation
3839impl TryFrom < & [ i16 ] > for Color {
39- type Error = String ;
40+ type Error = Box < dyn error :: Error > ;
4041 fn try_from ( slice : & [ i16 ] ) -> Result < Self , Self :: Error > { }
4142}
4243
@@ -76,41 +77,43 @@ mod tests {
7677 }
7778 #[ test]
7879 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( ) ) ;
8082 assert_eq ! (
81- c,
82- Ok ( Color {
83+ c. unwrap ( ) ,
84+ Color {
8385 red: 183 ,
8486 green: 65 ,
8587 blue: 14
86- } )
88+ }
8789 ) ;
8890 }
8991 #[ test]
9092 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 ( ) ;
9294 assert ! ( c. is_err( ) ) ;
9395 }
9496 #[ test]
9597 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 ( ) ;
9799 assert ! ( c. is_err( ) ) ;
98100 }
99101 #[ test]
100102 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 ( ) ;
102104 assert ! ( c. is_err( ) ) ;
103105 }
104106 #[ test]
105107 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( ) ) ;
107110 assert_eq ! (
108- c,
109- Ok ( Color {
111+ c. unwrap ( ) ,
112+ Color {
110113 red: 183 ,
111114 green: 65 ,
112115 blue: 14
113- } )
116+ }
114117 ) ;
115118 }
116119 #[ test]
@@ -131,14 +134,15 @@ mod tests {
131134 #[ test]
132135 fn test_slice_correct ( ) {
133136 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( ) ) ;
135139 assert_eq ! (
136- c,
137- Ok ( Color {
140+ c. unwrap ( ) ,
141+ Color {
138142 red: 183 ,
139143 green: 65 ,
140144 blue: 14
141- } )
145+ }
142146 ) ;
143147 }
144148 #[ test]
0 commit comments