|
40 | 40 |
|
41 | 41 | #![stable(feature = "rust1", since = "1.0.0")]
|
42 | 42 |
|
43 |
| -use crate::fmt; |
44 |
| - |
45 | 43 | /// An identity function.
|
46 | 44 | ///
|
47 | 45 | /// Two things are important to note about this function:
|
@@ -426,9 +424,7 @@ pub trait TryInto<T>: Sized {
|
426 | 424 | /// - `TryFrom<T> for U` implies [`TryInto`]`<U> for T`
|
427 | 425 | /// - [`try_from`] is reflexive, which means that `TryFrom<T> for T`
|
428 | 426 | /// is implemented and cannot fail -- the associated `Error` type for
|
429 |
| -/// calling `T::try_from()` on a value of type `T` is [`Infallible`]. |
430 |
| -/// When the [`!`] type is stabilized [`Infallible`] and [`!`] will be |
431 |
| -/// equivalent. |
| 427 | +/// calling `T::try_from()` on a value of type `T` is [`!`]. |
432 | 428 | ///
|
433 | 429 | /// `TryFrom<T>` can be implemented as follows:
|
434 | 430 | ///
|
@@ -477,7 +473,6 @@ pub trait TryInto<T>: Sized {
|
477 | 473 | /// [`TryInto`]: trait.TryInto.html
|
478 | 474 | /// [`i32::MAX`]: ../../std/i32/constant.MAX.html
|
479 | 475 | /// [`!`]: ../../std/primitive.never.html
|
480 |
| -/// [`Infallible`]: enum.Infallible.html |
481 | 476 | #[stable(feature = "try_from", since = "1.34.0")]
|
482 | 477 | pub trait TryFrom<T>: Sized {
|
483 | 478 | /// The type returned in the event of a conversion error.
|
@@ -551,6 +546,17 @@ impl<T> From<T> for T {
|
551 | 546 | fn from(t: T) -> T { t }
|
552 | 547 | }
|
553 | 548 |
|
| 549 | +#[stable(feature = "convert_infallible", since = "1.34.0")] |
| 550 | +#[cfg(not(bootstrap))] |
| 551 | +#[rustc_reservation_impl="a future version of Rust might implement `From<!>` for \ |
| 552 | + all types. \ |
| 553 | + However, it is OK to implement `From<!>` for types you own - \ |
| 554 | + when the blanket impl will be added, coherence will be changed \ |
| 555 | + to make these impls not be an error." |
| 556 | +] |
| 557 | +impl<T> From<!> for T { |
| 558 | + fn from(t: !) -> T { t } |
| 559 | +} |
554 | 560 |
|
555 | 561 | // TryFrom implies TryInto
|
556 | 562 | #[stable(feature = "try_from", since = "1.34.0")]
|
@@ -604,110 +610,6 @@ impl AsRef<str> for str {
|
604 | 610 | // THE NO-ERROR ERROR TYPE
|
605 | 611 | ////////////////////////////////////////////////////////////////////////////////
|
606 | 612 |
|
607 |
| -/// The error type for errors that can never happen. |
608 |
| -/// |
609 |
| -/// Since this enum has no variant, a value of this type can never actually exist. |
610 |
| -/// This can be useful for generic APIs that use [`Result`] and parameterize the error type, |
611 |
| -/// to indicate that the result is always [`Ok`]. |
612 |
| -/// |
613 |
| -/// For example, the [`TryFrom`] trait (conversion that returns a [`Result`]) |
614 |
| -/// has a blanket implementation for all types where a reverse [`Into`] implementation exists. |
615 |
| -/// |
616 |
| -/// ```ignore (illustrates std code, duplicating the impl in a doctest would be an error) |
617 |
| -/// impl<T, U> TryFrom<U> for T where U: Into<T> { |
618 |
| -/// type Error = Infallible; |
619 |
| -/// |
620 |
| -/// fn try_from(value: U) -> Result<Self, Infallible> { |
621 |
| -/// Ok(U::into(value)) // Never returns `Err` |
622 |
| -/// } |
623 |
| -/// } |
624 |
| -/// ``` |
625 |
| -/// |
626 |
| -/// # Future compatibility |
627 |
| -/// |
628 |
| -/// This enum has the same role as [the `!` “never” type][never], |
629 |
| -/// which is unstable in this version of Rust. |
630 |
| -/// When `!` is stabilized, we plan to make `Infallible` a type alias to it: |
631 |
| -/// |
632 |
| -/// ```ignore (illustrates future std change) |
633 |
| -/// pub type Infallible = !; |
634 |
| -/// ``` |
635 |
| -/// |
636 |
| -/// … and eventually deprecate `Infallible`. |
637 |
| -/// |
638 |
| -/// |
639 |
| -/// However there is one case where `!` syntax can be used |
640 |
| -/// before `!` is stabilized as a full-fleged type: in the position of a function’s return type. |
641 |
| -/// Specifically, it is possible implementations for two different function pointer types: |
642 |
| -/// |
643 |
| -/// ``` |
644 |
| -/// trait MyTrait {} |
645 |
| -/// impl MyTrait for fn() -> ! {} |
646 |
| -/// impl MyTrait for fn() -> std::convert::Infallible {} |
647 |
| -/// ``` |
648 |
| -/// |
649 |
| -/// With `Infallible` being an enum, this code is valid. |
650 |
| -/// However when `Infallible` becomes an alias for the never type, |
651 |
| -/// the two `impl`s will start to overlap |
652 |
| -/// and therefore will be disallowed by the language’s trait coherence rules. |
653 |
| -/// |
654 |
| -/// [`Ok`]: ../result/enum.Result.html#variant.Ok |
655 |
| -/// [`Result`]: ../result/enum.Result.html |
656 |
| -/// [`TryFrom`]: trait.TryFrom.html |
657 |
| -/// [`Into`]: trait.Into.html |
658 |
| -/// [never]: ../../std/primitive.never.html |
659 |
| -#[stable(feature = "convert_infallible", since = "1.34.0")] |
660 |
| -#[derive(Copy)] |
661 |
| -pub enum Infallible {} |
662 |
| - |
663 |
| -#[stable(feature = "convert_infallible", since = "1.34.0")] |
664 |
| -impl Clone for Infallible { |
665 |
| - fn clone(&self) -> Infallible { |
666 |
| - match *self {} |
667 |
| - } |
668 |
| -} |
669 |
| - |
670 |
| -#[stable(feature = "convert_infallible", since = "1.34.0")] |
671 |
| -impl fmt::Debug for Infallible { |
672 |
| - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { |
673 |
| - match *self {} |
674 |
| - } |
675 |
| -} |
676 |
| - |
| 613 | +/// Blah Blah Blah |
677 | 614 | #[stable(feature = "convert_infallible", since = "1.34.0")]
|
678 |
| -impl fmt::Display for Infallible { |
679 |
| - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { |
680 |
| - match *self {} |
681 |
| - } |
682 |
| -} |
683 |
| - |
684 |
| -#[stable(feature = "convert_infallible", since = "1.34.0")] |
685 |
| -impl PartialEq for Infallible { |
686 |
| - fn eq(&self, _: &Infallible) -> bool { |
687 |
| - match *self {} |
688 |
| - } |
689 |
| -} |
690 |
| - |
691 |
| -#[stable(feature = "convert_infallible", since = "1.34.0")] |
692 |
| -impl Eq for Infallible {} |
693 |
| - |
694 |
| -#[stable(feature = "convert_infallible", since = "1.34.0")] |
695 |
| -impl PartialOrd for Infallible { |
696 |
| - fn partial_cmp(&self, _other: &Self) -> Option<crate::cmp::Ordering> { |
697 |
| - match *self {} |
698 |
| - } |
699 |
| -} |
700 |
| - |
701 |
| -#[stable(feature = "convert_infallible", since = "1.34.0")] |
702 |
| -impl Ord for Infallible { |
703 |
| - fn cmp(&self, _other: &Self) -> crate::cmp::Ordering { |
704 |
| - match *self {} |
705 |
| - } |
706 |
| -} |
707 |
| - |
708 |
| -#[stable(feature = "convert_infallible", since = "1.34.0")] |
709 |
| -impl From<!> for Infallible { |
710 |
| - fn from(x: !) -> Self { |
711 |
| - x |
712 |
| - } |
713 |
| -} |
| 615 | +pub type Infallible = !; |
0 commit comments