Enum std::convert::Infallible
永远不会发生的错误的错误类型。
pub enum Infallible {}由于此枚举没有变体,因此这种类型的值实际上永远不会存在。 这对于使用 Result 并参数化错误类型的泛型 API 很有用,以指示结果始终为 Ok。
例如,对于存在反向 Into 实现的所有类型,TryFrom trait (返回 Result 的转换) 都具有通用实现。
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)) // 永不返回 `Err`
}
}Future 兼容性
该枚举与 never 类型 (!) 具有相同的作用,在此版本的 Rust 中是不稳定的。 当 ! 稳定后,我们计划将 Infallible 用作它的类型别名:
pub type Infallible = !;… 并最终弃用 Infallible。
但是,在一种情况下,可以在将 ! 稳定为完整类型之前使用 ! 语法:在函数的返回类型位置。 具体来说,可以实现两种不同的函数指针类型:
trait MyTrait {}
impl MyTrait for fn() -> ! {}
impl MyTrait for fn() -> std::convert::Infallible {}Infallible 是一个枚举,这个代码是有效的。 但是,当 Infallible 成为 never type 的别名时,两个 impl 将开始重叠,因此将被语言的 trait 一致性规则所禁止。
Trait Implementations
impl Clone for Infallible
clone
返回值的副本。
fn clone(&self) -> Infallibleclone_from
从source执行复制分配。
fn clone_from(&mut self, source: &Self)impl Debug for Infallible
fmt
使用给定的格式化程序格式化该值。
fn fmt(&self, _: &mut Formatter<'_>) -> Result<(), Error>impl Display for Infallible
fmt
使用给定的格式化程序格式化该值。
fn fmt(&self, _: &mut Formatter<'_>) -> Result<(), Error>impl Error for Infallible
source
此错误的下级来源 (如果有)。
fn source(&self) -> Option<&(dyn Error + 'static)>provide
nightly-only
提供对用于错误报告的上下文的基于类型的访问。
fn provide<'a>(&'a self, demand: &mut Demand<'a>)impl From<!> for Infallible
from
从输入类型转换为此类型。
fn from(x: !) -> Infallibleimpl From<Infallible> for TryFromIntError
from
从输入类型转换为此类型。
fn from(x: Infallible) -> TryFromIntErrorimpl From<Infallible> for TryFromSliceError
from
从输入类型转换为此类型。
fn from(x: Infallible) -> TryFromSliceErrorimpl Hash for Infallible
hash
将该值输入给定的 Hasher。
fn hash<H>(&self, _: &mut H)
where
H: Hasher,hash_slice
将这种类型的切片送入给定的 Hasher 中。
fn hash_slice<H>(data: &[Self], state: &mut H)
where
H: Hasher,
Self: Sized,impl Ord for Infallible
cmp
此方法返回 self 和 other 之间的 Ordering。
fn cmp(&self, _other: &Infallible) -> Orderingmax
比较并返回两个值中的最大值。
fn max(self, other: Self) -> Self
where
Self: Sized,min
比较并返回两个值中的最小值。
fn min(self, other: Self) -> Self
where
Self: Sized,clamp
将值限制在某个时间间隔内。
fn clamp(self, min: Self, max: Self) -> Self
where
Self: Sized + PartialOrd<Self>,impl PartialEq<Infallible> for Infallible
eq
此方法测试 self 和 other 值是否相等,并由 == 使用。
fn eq(&self, _: &Infallible) -> boolne
此方法测试 !=。 默认实现几乎总是足够的,并且不应在没有充分理由的情况下被覆盖。
fn ne(&self, other: &Rhs) -> boolimpl PartialOrd<Infallible> for Infallible
partial_cmp
如果存在,则此方法返回 self 和 other 值之间的顺序。
fn partial_cmp(&self, _other: &Infallible) -> Option<Ordering>lt
此方法测试的内容少于 (对于 self 和 other),并且由 < 操作员使用。
fn lt(&self, other: &Rhs) -> boolle
此方法测试小于或等于 (对于 self 和 other),并且由 <= 运算符使用。
fn le(&self, other: &Rhs) -> boolgt
此方法测试大于 (对于 self 和 other),并且由 > 操作员使用。
fn gt(&self, other: &Rhs) -> boolge
此方法测试是否大于或等于 (对于 self 和 other),并且由 >= 运算符使用。
fn ge(&self, other: &Rhs) -> boolimpl Termination for Infallible
report
被调用以获取值的表示形式作为状态码。 此状态代码返回到操作系统。
fn report(self) -> ExitCodeimpl Copy for Infallible
impl Eq for Infallible
Auto Trait Implementations
impl RefUnwindSafe for Infallible
impl Send for Infallible
impl Sync for Infallible
impl Unpin for Infallible
impl UnwindSafe for Infallible
Blanket Implementations
impl<T> Any for T
impl<T> Any for T
where
T: 'static + ?Sized,impl<T> Borrow<T> for T
impl<T> Borrow<T> for T
where
T: ?Sized,impl<T> BorrowMut<T> for T
impl<T> BorrowMut<T> for T
where
T: ?Sized,impl<T> From<!> for T
impl<T> From<T> for T
impl<T, U> Into<U> for T
impl<T, U> Into<U> for T
where
U: From<T>,impl<E> Provider for E
impl<E> Provider for E
where
E: Error + ?Sized,impl<T> ToOwned for T
impl<T> ToOwned for T
where
T: Clone,impl<T> ToString for T
impl<T> ToString for T
where
T: Display + ?Sized,impl<T, U> TryFrom<U> for T
impl<T, U> TryFrom<U> for T
where
U: Into<T>,impl<T, U> TryInto<U> for T
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,