@@ -69,6 +69,8 @@ impl fmt::Debug for Error {
69
69
enum Repr {
70
70
Os ( i32 ) ,
71
71
Simple ( ErrorKind ) ,
72
+ // &str is a fat pointer, but &&str is a thin pointer.
73
+ SimpleMessage ( ErrorKind , & ' static & ' static str ) ,
72
74
Custom ( Box < Custom > ) ,
73
75
}
74
76
@@ -259,6 +261,18 @@ impl Error {
259
261
Error { repr : Repr :: Custom ( Box :: new ( Custom { kind, error } ) ) }
260
262
}
261
263
264
+ /// Creates a new I/O error from a known kind of error as well as a
265
+ /// constant message.
266
+ ///
267
+ /// This function does not allocate.
268
+ ///
269
+ /// This function should maybe change to
270
+ /// `new_const<const MSG: &'static str>(kind: ErrorKind)`
271
+ /// in the future, when const generics allow that.
272
+ pub ( crate ) const fn new_const ( kind : ErrorKind , message : & ' static & ' static str ) -> Error {
273
+ Self { repr : Repr :: SimpleMessage ( kind, message) }
274
+ }
275
+
262
276
/// Returns an error representing the last OS error which occurred.
263
277
///
264
278
/// This function reads the value of `errno` for the target platform (e.g.
@@ -342,6 +356,7 @@ impl Error {
342
356
Repr :: Os ( i) => Some ( i) ,
343
357
Repr :: Custom ( ..) => None ,
344
358
Repr :: Simple ( ..) => None ,
359
+ Repr :: SimpleMessage ( ..) => None ,
345
360
}
346
361
}
347
362
@@ -377,6 +392,7 @@ impl Error {
377
392
match self . repr {
378
393
Repr :: Os ( ..) => None ,
379
394
Repr :: Simple ( ..) => None ,
395
+ Repr :: SimpleMessage ( ..) => None ,
380
396
Repr :: Custom ( ref c) => Some ( & * c. error ) ,
381
397
}
382
398
}
@@ -448,6 +464,7 @@ impl Error {
448
464
match self . repr {
449
465
Repr :: Os ( ..) => None ,
450
466
Repr :: Simple ( ..) => None ,
467
+ Repr :: SimpleMessage ( ..) => None ,
451
468
Repr :: Custom ( ref mut c) => Some ( & mut * c. error ) ,
452
469
}
453
470
}
@@ -484,6 +501,7 @@ impl Error {
484
501
match self . repr {
485
502
Repr :: Os ( ..) => None ,
486
503
Repr :: Simple ( ..) => None ,
504
+ Repr :: SimpleMessage ( ..) => None ,
487
505
Repr :: Custom ( c) => Some ( c. error ) ,
488
506
}
489
507
}
@@ -512,6 +530,7 @@ impl Error {
512
530
Repr :: Os ( code) => sys:: decode_error_kind ( code) ,
513
531
Repr :: Custom ( ref c) => c. kind ,
514
532
Repr :: Simple ( kind) => kind,
533
+ Repr :: SimpleMessage ( kind, _) => kind,
515
534
}
516
535
}
517
536
}
@@ -527,6 +546,9 @@ impl fmt::Debug for Repr {
527
546
. finish ( ) ,
528
547
Repr :: Custom ( ref c) => fmt:: Debug :: fmt ( & c, fmt) ,
529
548
Repr :: Simple ( kind) => fmt. debug_tuple ( "Kind" ) . field ( & kind) . finish ( ) ,
549
+ Repr :: SimpleMessage ( kind, & message) => {
550
+ fmt. debug_struct ( "Error" ) . field ( "kind" , & kind) . field ( "message" , & message) . finish ( )
551
+ }
530
552
}
531
553
}
532
554
}
@@ -541,6 +563,7 @@ impl fmt::Display for Error {
541
563
}
542
564
Repr :: Custom ( ref c) => c. error . fmt ( fmt) ,
543
565
Repr :: Simple ( kind) => write ! ( fmt, "{}" , kind. as_str( ) ) ,
566
+ Repr :: SimpleMessage ( _, & msg) => msg. fmt ( fmt) ,
544
567
}
545
568
}
546
569
}
@@ -551,6 +574,7 @@ impl error::Error for Error {
551
574
fn description ( & self ) -> & str {
552
575
match self . repr {
553
576
Repr :: Os ( ..) | Repr :: Simple ( ..) => self . kind ( ) . as_str ( ) ,
577
+ Repr :: SimpleMessage ( _, & msg) => msg,
554
578
Repr :: Custom ( ref c) => c. error . description ( ) ,
555
579
}
556
580
}
@@ -560,6 +584,7 @@ impl error::Error for Error {
560
584
match self . repr {
561
585
Repr :: Os ( ..) => None ,
562
586
Repr :: Simple ( ..) => None ,
587
+ Repr :: SimpleMessage ( ..) => None ,
563
588
Repr :: Custom ( ref c) => c. error . cause ( ) ,
564
589
}
565
590
}
@@ -568,6 +593,7 @@ impl error::Error for Error {
568
593
match self . repr {
569
594
Repr :: Os ( ..) => None ,
570
595
Repr :: Simple ( ..) => None ,
596
+ Repr :: SimpleMessage ( ..) => None ,
571
597
Repr :: Custom ( ref c) => c. error . source ( ) ,
572
598
}
573
599
}
0 commit comments