Enum Infallible

enum Infallible

The error type for errors that can never happen.

Since this enum has no variant, a value of this type can never actually exist. This can be useful for generic APIs that use Result and parameterize the error type, to indicate that the result is always Ok.

For example, the TryFrom trait (conversion that returns a Result) has a blanket implementation for all types where a reverse Into implementation exists.

impl<T, U> TryFrom<U> for T where U: Into<T> {
    type Error = Infallible;

    fn try_from(value: U) -> Result<Self, Infallible> {
        Ok(U::into(value))  // Never returns `Err`
    }
}

Future compatibility

This enum has the same role as [the ! “never” type][never], which is unstable in this version of Rust. When ! is stabilized, we plan to make Infallible a type alias to it:

pub type Infallible = !;

… and eventually deprecate Infallible.

However there is one case where ! syntax can be used before ! is stabilized as a full-fledged type: in the position of a function’s return type. Specifically, it is possible to have implementations for two different function pointer types:

trait MyTrait {}
impl MyTrait for fn() -> ! {}
impl MyTrait for fn() -> std::convert::Infallible {}

With Infallible being an enum, this code is valid. However when Infallible becomes an alias for the never type, the two impls will start to overlap and therefore will be disallowed by the language’s trait coherence rules.

Implementations

impl Clone for Infallible

fn clone(self: &Self) -> Infallible

impl Copy for Infallible

impl Debug for Infallible

fn fmt(self: &Self, _: &mut fmt::Formatter<'_>) -> fmt::Result

impl Display for Infallible

fn fmt(self: &Self, _: &mut fmt::Formatter<'_>) -> fmt::Result

impl Eq for Infallible

impl Error for Infallible

impl Freeze for Infallible

impl From for Infallible

fn from(x: never) -> Self

impl Hash for Infallible

fn hash<H: Hasher>(self: &Self, _: &mut H)

impl Ord for Infallible

fn cmp(self: &Self, _other: &Self) -> crate::cmp::Ordering

impl PartialEq for Infallible

fn eq(self: &Self, _: &Infallible) -> bool

impl PartialOrd for Infallible

fn partial_cmp(self: &Self, _other: &Self) -> Option<crate::cmp::Ordering>

impl RefUnwindSafe for Infallible

impl Send for Infallible

impl Sync for Infallible

impl Unpin for Infallible

impl UnwindSafe for Infallible

impl<T> Any for Infallible

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Infallible

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Infallible

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Infallible

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Infallible

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> From for Infallible

fn from(t: never) -> T

impl<T, U> Into for Infallible

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Infallible

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Infallible

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>