40
40
41
41
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
42
42
43
+ use crate :: fmt;
44
+
43
45
mod num;
44
46
45
47
#[ unstable( feature = "convert_float_to_int" , issue = "67057" ) ]
@@ -429,7 +431,9 @@ pub trait TryInto<T>: Sized {
429
431
/// - `TryFrom<T> for U` implies [`TryInto`]`<U> for T`
430
432
/// - [`try_from`] is reflexive, which means that `TryFrom<T> for T`
431
433
/// is implemented and cannot fail -- the associated `Error` type for
432
- /// calling `T::try_from()` on a value of type `T` is [`!`].
434
+ /// calling `T::try_from()` on a value of type `T` is [`Infallible`].
435
+ /// When the [`!`] type is stabilized [`Infallible`] and [`!`] will be
436
+ /// equivalent.
433
437
///
434
438
/// `TryFrom<T>` can be implemented as follows:
435
439
///
@@ -478,6 +482,7 @@ pub trait TryInto<T>: Sized {
478
482
/// [`TryInto`]: trait.TryInto.html
479
483
/// [`i32::MAX`]: ../../std/i32/constant.MAX.html
480
484
/// [`!`]: ../../std/primitive.never.html
485
+ /// [`Infallible`]: enum.Infallible.html
481
486
#[ stable( feature = "try_from" , since = "1.34.0" ) ]
482
487
pub trait TryFrom < T > : Sized {
483
488
/// The type returned in the event of a conversion error.
@@ -633,9 +638,9 @@ impl AsRef<str> for str {
633
638
// THE NO-ERROR ERROR TYPE
634
639
////////////////////////////////////////////////////////////////////////////////
635
640
636
- /// A type alias for [the `!` “never” type][ never] .
641
+ /// The error type for errors that can never happen .
637
642
///
638
- /// `Infallible` represents types of errors that can never happen since `!` has no valid values .
643
+ /// Since this enum has no variant, a value of this type can never actually exist .
639
644
/// This can be useful for generic APIs that use [`Result`] and parameterize the error type,
640
645
/// to indicate that the result is always [`Ok`].
641
646
///
@@ -652,15 +657,91 @@ impl AsRef<str> for str {
652
657
/// }
653
658
/// ```
654
659
///
655
- /// # Eventual deprecation
660
+ /// # Future compatibility
661
+ ///
662
+ /// This enum has the same role as [the `!` “never” type][never],
663
+ /// which is unstable in this version of Rust.
664
+ /// When `!` is stabilized, we plan to make `Infallible` a type alias to it:
665
+ ///
666
+ /// ```ignore (illustrates future std change)
667
+ /// pub type Infallible = !;
668
+ /// ```
669
+ ///
670
+ /// … and eventually deprecate `Infallible`.
671
+ ///
672
+ ///
673
+ /// However there is one case where `!` syntax can be used
674
+ /// before `!` is stabilized as a full-fleged type: in the position of a function’s return type.
675
+ /// Specifically, it is possible implementations for two different function pointer types:
676
+ ///
677
+ /// ```
678
+ /// trait MyTrait {}
679
+ /// impl MyTrait for fn() -> ! {}
680
+ /// impl MyTrait for fn() -> std::convert::Infallible {}
681
+ /// ```
656
682
///
657
- /// Previously, `Infallible` was defined as `enum Infallible {}`.
658
- /// Now that it is merely a type alias to `!`, we will eventually deprecate `Infallible`.
683
+ /// With `Infallible` being an enum, this code is valid.
684
+ /// However when `Infallible` becomes an alias for the never type,
685
+ /// the two `impl`s will start to overlap
686
+ /// and therefore will be disallowed by the language’s trait coherence rules.
659
687
///
660
688
/// [`Ok`]: ../result/enum.Result.html#variant.Ok
661
689
/// [`Result`]: ../result/enum.Result.html
662
690
/// [`TryFrom`]: trait.TryFrom.html
663
691
/// [`Into`]: trait.Into.html
664
692
/// [never]: ../../std/primitive.never.html
665
693
#[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
666
- pub type Infallible = !;
694
+ #[ derive( Copy ) ]
695
+ pub enum Infallible { }
696
+
697
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
698
+ impl Clone for Infallible {
699
+ fn clone ( & self ) -> Infallible {
700
+ match * self { }
701
+ }
702
+ }
703
+
704
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
705
+ impl fmt:: Debug for Infallible {
706
+ fn fmt ( & self , _: & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
707
+ match * self { }
708
+ }
709
+ }
710
+
711
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
712
+ impl fmt:: Display for Infallible {
713
+ fn fmt ( & self , _: & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
714
+ match * self { }
715
+ }
716
+ }
717
+
718
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
719
+ impl PartialEq for Infallible {
720
+ fn eq ( & self , _: & Infallible ) -> bool {
721
+ match * self { }
722
+ }
723
+ }
724
+
725
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
726
+ impl Eq for Infallible { }
727
+
728
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
729
+ impl PartialOrd for Infallible {
730
+ fn partial_cmp ( & self , _other : & Self ) -> Option < crate :: cmp:: Ordering > {
731
+ match * self { }
732
+ }
733
+ }
734
+
735
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
736
+ impl Ord for Infallible {
737
+ fn cmp ( & self , _other : & Self ) -> crate :: cmp:: Ordering {
738
+ match * self { }
739
+ }
740
+ }
741
+
742
+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
743
+ impl From < !> for Infallible {
744
+ fn from ( x : !) -> Self {
745
+ x
746
+ }
747
+ }
0 commit comments