@@ -499,3 +499,96 @@ impl AsRef<str> for str {
499
499
self
500
500
}
501
501
}
502
+
503
+ ////////////////////////////////////////////////////////////////////////////////
504
+ // THE NO-ERROR ERROR TYPE
505
+ ////////////////////////////////////////////////////////////////////////////////
506
+
507
+ /// The error type for errors that can never happen.
508
+ ///
509
+ /// Since this enum has no variant, a value of this type can never actually exist.
510
+ /// This can be useful for generic APIs that use [`Result`] and parameterize the error type,
511
+ /// to indicate that the result is always [`Ok`].
512
+ ///
513
+ /// For example, the [`TryFrom`] trait (conversion that returns a [`Result`])
514
+ /// has a blanket implementation for all types where a reverse [`Into`] implementation exists.
515
+ ///
516
+ /// ```ignore (illustrates std code, duplicating the impl in a doctest would be an error)
517
+ /// impl<T, U> TryFrom<U> for T where U: Into<T> {
518
+ /// type Error = Infallible;
519
+ ///
520
+ /// fn try_from(value: U) -> Result<Self, Infallible> {
521
+ /// Ok(U::into(value)) // Never returns `Err`
522
+ /// }
523
+ /// }
524
+ /// ```
525
+ ///
526
+ /// # Future compatibility
527
+ ///
528
+ /// This enum has the same role as [the `!` “never” type][never],
529
+ /// which is unstable in this version of Rust.
530
+ /// When `!` is stabilized, we plan to make `Infallible` a type alias to it:
531
+ ///
532
+ /// ```ignore (illustrates future std change)
533
+ /// pub type Infallible = !;
534
+ /// ```
535
+ ///
536
+ /// … and eventually deprecate `Infallible`.
537
+ ///
538
+ ///
539
+ /// However there is one case where `!` syntax can be used
540
+ /// before `!` is stabilized as a full-fleged type: in the position of a function’s return type.
541
+ /// Specifically, it is possible implementations for two different function pointer types:
542
+ ///
543
+ /// ```
544
+ /// trait MyTrait {}
545
+ /// impl MyTrait for fn() -> ! {}
546
+ /// impl MyTrait for fn() -> std::convert::Infallible {}
547
+ /// ```
548
+ ///
549
+ /// With `Infallible` being an enum, this code is valid.
550
+ /// However when `Infallible` becomes an alias for the never type,
551
+ /// the two `impl`s will start to overlap
552
+ /// and therefore will be disallowed by the language’s trait coherence rules.
553
+ ///
554
+ /// [`Ok`]: ../result/enum.Result.html#variant.Ok
555
+ /// [`Result`]: ../result/enum.Result.html
556
+ /// [`TryFrom`]: trait.TryFrom.html
557
+ /// [`Into`]: trait.Into.html
558
+ /// [never]: ../../std/primitive.never.html
559
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
560
+ #[ derive( Copy ) ]
561
+ pub enum Infallible { }
562
+
563
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
564
+ impl Clone for Infallible {
565
+ fn clone ( & self ) -> Infallible {
566
+ match * self { }
567
+ }
568
+ }
569
+
570
+ use fmt;
571
+
572
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
573
+ impl fmt:: Debug for Infallible {
574
+ fn fmt ( & self , _: & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
575
+ match * self { }
576
+ }
577
+ }
578
+
579
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
580
+ impl fmt:: Display for Infallible {
581
+ fn fmt ( & self , _: & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
582
+ match * self { }
583
+ }
584
+ }
585
+
586
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
587
+ impl PartialEq for Infallible {
588
+ fn eq ( & self , _: & Infallible ) -> bool {
589
+ match * self { }
590
+ }
591
+ }
592
+
593
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
594
+ impl Eq for Infallible { }
0 commit comments