4141
4242#![ stable( feature = "rust1" , since = "1.0.0" ) ]
4343
44+ use fmt;
45+
4446/// An identity function.
4547///
4648/// Two things are important to note about this function:
@@ -367,22 +369,26 @@ pub trait From<T>: Sized {
367369///
368370/// [`TryFrom`]: trait.TryFrom.html
369371/// [`Into`]: trait.Into.html
370- #[ unstable ( feature = "try_from" , issue = "33417 " ) ]
372+ #[ stable ( feature = "try_from" , since = "1.34.0 " ) ]
371373pub trait TryInto < T > : Sized {
372374 /// The type returned in the event of a conversion error.
375+ #[ stable( feature = "try_from" , since = "1.34.0" ) ]
373376 type Error ;
374377
375378 /// Performs the conversion.
379+ #[ stable( feature = "try_from" , since = "1.34.0" ) ]
376380 fn try_into ( self ) -> Result < T , Self :: Error > ;
377381}
378382
379383/// Attempt to construct `Self` via a conversion.
380- #[ unstable ( feature = "try_from" , issue = "33417 " ) ]
384+ #[ stable ( feature = "try_from" , since = "1.34.0 " ) ]
381385pub trait TryFrom < T > : Sized {
382386 /// The type returned in the event of a conversion error.
387+ #[ stable( feature = "try_from" , since = "1.34.0" ) ]
383388 type Error ;
384389
385390 /// Performs the conversion.
391+ #[ stable( feature = "try_from" , since = "1.34.0" ) ]
386392 fn try_from ( value : T ) -> Result < Self , Self :: Error > ;
387393}
388394
@@ -450,7 +456,7 @@ impl<T> From<T> for T {
450456
451457
452458// TryFrom implies TryInto
453- #[ unstable ( feature = "try_from" , issue = "33417 " ) ]
459+ #[ stable ( feature = "try_from" , since = "1.34.0 " ) ]
454460impl < T , U > TryInto < U > for T where U : TryFrom < T >
455461{
456462 type Error = U :: Error ;
@@ -462,9 +468,9 @@ impl<T, U> TryInto<U> for T where U: TryFrom<T>
462468
463469// Infallible conversions are semantically equivalent to fallible conversions
464470// with an uninhabited error type.
465- #[ unstable ( feature = "try_from" , issue = "33417 " ) ]
471+ #[ stable ( feature = "try_from" , since = "1.34.0 " ) ]
466472impl < T , U > TryFrom < U > for T where U : Into < T > {
467- type Error = ! ;
473+ type Error = Infallible ;
468474
469475 fn try_from ( value : U ) -> Result < Self , Self :: Error > {
470476 Ok ( U :: into ( value) )
@@ -496,3 +502,115 @@ impl AsRef<str> for str {
496502 self
497503 }
498504}
505+
506+ ////////////////////////////////////////////////////////////////////////////////
507+ // THE NO-ERROR ERROR TYPE
508+ ////////////////////////////////////////////////////////////////////////////////
509+
510+ /// The error type for errors that can never happen.
511+ ///
512+ /// Since this enum has no variant, a value of this type can never actually exist.
513+ /// This can be useful for generic APIs that use [`Result`] and parameterize the error type,
514+ /// to indicate that the result is always [`Ok`].
515+ ///
516+ /// For example, the [`TryFrom`] trait (conversion that returns a [`Result`])
517+ /// has a blanket implementation for all types where a reverse [`Into`] implementation exists.
518+ ///
519+ /// ```ignore (illustrates std code, duplicating the impl in a doctest would be an error)
520+ /// impl<T, U> TryFrom<U> for T where U: Into<T> {
521+ /// type Error = Infallible;
522+ ///
523+ /// fn try_from(value: U) -> Result<Self, Infallible> {
524+ /// Ok(U::into(value)) // Never returns `Err`
525+ /// }
526+ /// }
527+ /// ```
528+ ///
529+ /// # Future compatibility
530+ ///
531+ /// This enum has the same role as [the `!` “never” type][never],
532+ /// which is unstable in this version of Rust.
533+ /// When `!` is stabilized, we plan to make `Infallible` a type alias to it:
534+ ///
535+ /// ```ignore (illustrates future std change)
536+ /// pub type Infallible = !;
537+ /// ```
538+ ///
539+ /// … and eventually deprecate `Infallible`.
540+ ///
541+ ///
542+ /// However there is one case where `!` syntax can be used
543+ /// before `!` is stabilized as a full-fleged type: in the position of a function’s return type.
544+ /// Specifically, it is possible implementations for two different function pointer types:
545+ ///
546+ /// ```
547+ /// trait MyTrait {}
548+ /// impl MyTrait for fn() -> ! {}
549+ /// impl MyTrait for fn() -> std::convert::Infallible {}
550+ /// ```
551+ ///
552+ /// With `Infallible` being an enum, this code is valid.
553+ /// However when `Infallible` becomes an alias for the never type,
554+ /// the two `impl`s will start to overlap
555+ /// and therefore will be disallowed by the language’s trait coherence rules.
556+ ///
557+ /// [`Ok`]: ../result/enum.Result.html#variant.Ok
558+ /// [`Result`]: ../result/enum.Result.html
559+ /// [`TryFrom`]: trait.TryFrom.html
560+ /// [`Into`]: trait.Into.html
561+ /// [never]: ../../std/primitive.never.html
562+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
563+ #[ derive( Copy ) ]
564+ pub enum Infallible { }
565+
566+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
567+ impl Clone for Infallible {
568+ fn clone ( & self ) -> Infallible {
569+ match * self { }
570+ }
571+ }
572+
573+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
574+ impl fmt:: Debug for Infallible {
575+ fn fmt ( & self , _: & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
576+ match * self { }
577+ }
578+ }
579+
580+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
581+ impl fmt:: Display for Infallible {
582+ fn fmt ( & self , _: & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
583+ match * self { }
584+ }
585+ }
586+
587+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
588+ impl PartialEq for Infallible {
589+ fn eq ( & self , _: & Infallible ) -> bool {
590+ match * self { }
591+ }
592+ }
593+
594+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
595+ impl Eq for Infallible { }
596+
597+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
598+ impl PartialOrd for Infallible {
599+ fn partial_cmp ( & self , _other : & Self ) -> Option < crate :: cmp:: Ordering > {
600+ match * self { }
601+ }
602+ }
603+
604+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
605+ impl Ord for Infallible {
606+ fn cmp ( & self , _other : & Self ) -> crate :: cmp:: Ordering {
607+ match * self { }
608+ }
609+ }
610+
611+ #[ stable( feature = "convert_infallible" , since = "1.34.0" ) ]
612+ impl From < !> for Infallible {
613+ fn from ( x : !) -> Self {
614+ x
615+ }
616+ }
0 commit comments