1- use super :: { Custom , Error , ErrorData , ErrorKind , Repr , SimpleMessage , const_error} ;
1+ use crate :: io :: { Error , ErrorKind , const_error} ;
22use crate :: sys:: io:: { decode_error_kind, error_string} ;
3- use crate :: { assert_matches, error, fmt} ;
3+ use crate :: { error, fmt} ;
4+
5+ // Make sure that all these are used even when running tests
6+ #[ cfg( test) ]
7+ const _: ( ) = {
8+ let _ = ( crate :: sys:: io:: errno, crate :: sys:: io:: is_interrupted) ;
9+ } ;
410
511#[ test]
612fn test_size ( ) {
@@ -12,12 +18,9 @@ fn test_debug_error() {
1218 let code = 6 ;
1319 let msg = error_string ( code) ;
1420 let kind = decode_error_kind ( code) ;
15- let err = Error {
16- repr : Repr :: new_custom ( Box :: new ( Custom {
17- kind : ErrorKind :: InvalidInput ,
18- error : Box :: new ( Error { repr : super :: Repr :: new_os ( code) } ) ,
19- } ) ) ,
20- } ;
21+
22+ let err = Error :: new ( ErrorKind :: InvalidInput , Error :: from_raw_os_error ( code) ) ;
23+
2124 let expected = format ! (
2225 "Custom {{ \
2326 kind: InvalidInput, \
@@ -30,6 +33,15 @@ fn test_debug_error() {
3033 code, kind, msg
3134 ) ;
3235 assert_eq ! ( format!( "{err:?}" ) , expected) ;
36+
37+ let err = Error :: from ( ErrorKind :: AddrInUse ) ;
38+ assert_eq ! ( format!( "{err:?}" ) , "Kind(AddrInUse)" ) ;
39+
40+ let err = Error :: READ_EXACT_EOF ;
41+ assert_eq ! (
42+ format!( "{err:?}" ) ,
43+ "Error { kind: UnexpectedEof, message: \" failed to fill whole buffer\" }"
44+ ) ;
3345}
3446
3547#[ test]
@@ -70,10 +82,6 @@ fn test_os_packing() {
7082 for code in -20 ..20 {
7183 let e = Error :: from_raw_os_error ( code) ;
7284 assert_eq ! ( e. raw_os_error( ) , Some ( code) ) ;
73- assert_matches ! (
74- e. repr. data( ) ,
75- ErrorData :: Os ( c) if c == code,
76- ) ;
7785 }
7886}
7987
@@ -82,28 +90,18 @@ fn test_errorkind_packing() {
8290 assert_eq ! ( Error :: from( ErrorKind :: NotFound ) . kind( ) , ErrorKind :: NotFound ) ;
8391 assert_eq ! ( Error :: from( ErrorKind :: PermissionDenied ) . kind( ) , ErrorKind :: PermissionDenied ) ;
8492 assert_eq ! ( Error :: from( ErrorKind :: Uncategorized ) . kind( ) , ErrorKind :: Uncategorized ) ;
85- // Check that the innards look like what we want.
86- assert_matches ! (
87- Error :: from( ErrorKind :: OutOfMemory ) . repr. data( ) ,
88- ErrorData :: Simple ( ErrorKind :: OutOfMemory ) ,
89- ) ;
9093}
9194
9295#[ test]
9396fn test_simple_message_packing ( ) {
94- use super :: ErrorKind :: * ;
95- use super :: SimpleMessage ;
97+ use crate :: io:: ErrorKind :: * ;
9698 macro_rules! check_simple_msg {
9799 ( $err: expr, $kind: ident, $msg: literal) => { {
98100 let e = & $err;
99101 // Check that the public api is right.
100102 assert_eq!( e. kind( ) , $kind) ;
101103 assert!( format!( "{e:?}" ) . contains( $msg) ) ;
102- // and we got what we expected
103- assert_matches!(
104- e. repr. data( ) ,
105- ErrorData :: SimpleMessage ( SimpleMessage { kind: $kind, message: $msg } )
106- ) ;
104+ assert!( format!( "{e}" ) . contains( $msg) ) ;
107105 } } ;
108106 }
109107
@@ -126,19 +124,6 @@ impl fmt::Display for Bojji {
126124 }
127125}
128126
129- #[ test]
130- fn test_custom_error_packing ( ) {
131- use super :: Custom ;
132- let test = Error :: new ( ErrorKind :: Uncategorized , Bojji ( true ) ) ;
133- assert_matches ! (
134- test. repr. data( ) ,
135- ErrorData :: Custom ( Custom {
136- kind: ErrorKind :: Uncategorized ,
137- error,
138- } ) if error. downcast_ref:: <Bojji >( ) . as_deref( ) == Some ( & Bojji ( true ) ) ,
139- ) ;
140- }
141-
142127#[ derive( Debug ) ]
143128struct E ;
144129
@@ -181,11 +166,9 @@ fn test_std_io_error_downcast() {
181166 assert_eq ! ( kind, io_error. kind( ) ) ;
182167
183168 // Case 5: simple message
184- const SIMPLE_MESSAGE : SimpleMessage =
185- SimpleMessage { kind : ErrorKind :: Other , message : "simple message error test" } ;
186- let io_error = Error :: from_static_message ( & SIMPLE_MESSAGE ) ;
169+ let io_error = const_error ! ( ErrorKind :: Other , "simple message error test" ) ;
187170 let io_error = io_error. downcast :: < E > ( ) . unwrap_err ( ) ;
188171
189- assert_eq ! ( SIMPLE_MESSAGE . kind , io_error. kind( ) ) ;
190- assert_eq ! ( SIMPLE_MESSAGE . message , format!( "{io_error}" ) ) ;
172+ assert_eq ! ( io_error. kind( ) , ErrorKind :: Other ) ;
173+ assert_eq ! ( format!( "{io_error}" ) , "simple message error test" ) ;
191174}
0 commit comments