Skip to content

Commit 85f13f0

Browse files
committed
Add a convert::Infallible empty enum, make string::ParseError an alias
1 parent e544947 commit 85f13f0

File tree

3 files changed

+97
-36
lines changed

3 files changed

+97
-36
lines changed

src/liballoc/string.rs

+3-34
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ impl String {
486486
/// [`str::from_utf8`]: ../../std/str/fn.from_utf8.html
487487
/// [`as_bytes`]: struct.String.html#method.as_bytes
488488
/// [`FromUtf8Error`]: struct.FromUtf8Error.html
489-
/// [`Err`]: ../../stdresult/enum.Result.html#variant.Err
489+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
490490
#[inline]
491491
#[stable(feature = "rust1", since = "1.0.0")]
492492
pub fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error> {
@@ -2073,48 +2073,17 @@ impl ops::DerefMut for String {
20732073
/// [`String`]: struct.String.html
20742074
/// [`from_str`]: ../../std/str/trait.FromStr.html#tymethod.from_str
20752075
#[stable(feature = "str_parse_error", since = "1.5.0")]
2076-
#[derive(Copy)]
2077-
pub enum ParseError {}
2076+
pub type ParseError = core::convert::Infallible;
20782077

20792078
#[stable(feature = "rust1", since = "1.0.0")]
20802079
impl FromStr for String {
2081-
type Err = ParseError;
2080+
type Err = core::convert::Infallible;
20822081
#[inline]
20832082
fn from_str(s: &str) -> Result<String, ParseError> {
20842083
Ok(String::from(s))
20852084
}
20862085
}
20872086

2088-
#[stable(feature = "str_parse_error", since = "1.5.0")]
2089-
impl Clone for ParseError {
2090-
fn clone(&self) -> ParseError {
2091-
match *self {}
2092-
}
2093-
}
2094-
2095-
#[stable(feature = "str_parse_error", since = "1.5.0")]
2096-
impl fmt::Debug for ParseError {
2097-
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
2098-
match *self {}
2099-
}
2100-
}
2101-
2102-
#[stable(feature = "str_parse_error2", since = "1.8.0")]
2103-
impl fmt::Display for ParseError {
2104-
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
2105-
match *self {}
2106-
}
2107-
}
2108-
2109-
#[stable(feature = "str_parse_error", since = "1.5.0")]
2110-
impl PartialEq for ParseError {
2111-
fn eq(&self, _: &ParseError) -> bool {
2112-
match *self {}
2113-
}
2114-
}
2115-
2116-
#[stable(feature = "str_parse_error", since = "1.5.0")]
2117-
impl Eq for ParseError {}
21182087

21192088
/// A trait for converting a value to a `String`.
21202089
///

src/libcore/convert.rs

+93
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,96 @@ impl AsRef<str> for str {
499499
self
500500
}
501501
}
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 {}

src/libstd/path.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ use iter::{self, FusedIterator};
7878
use ops::{self, Deref};
7979
use rc::Rc;
8080
use str::FromStr;
81-
use string::ParseError;
8281
use sync::Arc;
8382

8483
use ffi::{OsStr, OsString};
@@ -1453,7 +1452,7 @@ impl From<String> for PathBuf {
14531452

14541453
#[stable(feature = "path_from_str", since = "1.32.0")]
14551454
impl FromStr for PathBuf {
1456-
type Err = ParseError;
1455+
type Err = core::convert::Infallible;
14571456

14581457
fn from_str(s: &str) -> Result<Self, Self::Err> {
14591458
Ok(PathBuf::from(s))

0 commit comments

Comments
 (0)