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