2019-03-25 22:21:05 +01:00
|
|
|
|
//! Traits for conversions between types.
|
2015-03-18 09:14:54 -07:00
|
|
|
|
//!
|
2019-03-20 23:15:41 +01:00
|
|
|
|
//! The traits in this module provide a way to convert from one type to another type.
|
|
|
|
|
//! Each trait serves a different purpose:
|
2015-04-06 14:25:39 -04:00
|
|
|
|
//!
|
2019-03-20 23:15:41 +01:00
|
|
|
|
//! - Implement the [`AsRef`] trait for cheap reference-to-reference conversions
|
|
|
|
|
//! - Implement the [`AsMut`] trait for cheap mutable-to-mutable conversions
|
|
|
|
|
//! - Implement the [`From`] trait for consuming value-to-value conversions
|
2019-03-21 15:06:16 +01:00
|
|
|
|
//! - Implement the [`Into`] trait for consuming value-to-value conversions to types
|
2019-03-21 15:26:07 +01:00
|
|
|
|
//! outside the current crate
|
2019-03-21 15:06:16 +01:00
|
|
|
|
//! - The [`TryFrom`] and [`TryInto`] traits behave like [`From`] and [`Into`],
|
2019-03-21 15:26:07 +01:00
|
|
|
|
//! but should be implemented when the conversion can fail.
|
2016-01-14 16:06:53 +01:00
|
|
|
|
//!
|
2019-03-20 23:15:41 +01:00
|
|
|
|
//! The traits in this module are often used as trait bounds for generic functions such that to
|
|
|
|
|
//! arguments of multiple types are supported. See the documentation of each trait for examples.
|
|
|
|
|
//!
|
|
|
|
|
//! As a library author, you should always prefer implementing [`From<T>`][`From`] or
|
2017-01-29 23:33:12 +01:00
|
|
|
|
//! [`TryFrom<T>`][`TryFrom`] rather than [`Into<U>`][`Into`] or [`TryInto<U>`][`TryInto`],
|
|
|
|
|
//! as [`From`] and [`TryFrom`] provide greater flexibility and offer
|
2017-04-03 07:57:20 +10:00
|
|
|
|
//! equivalent [`Into`] or [`TryInto`] implementations for free, thanks to a
|
2019-03-20 23:15:41 +01:00
|
|
|
|
//! blanket implementation in the standard library. Only implement [`Into`] or [`TryInto`]
|
|
|
|
|
//! when a conversion to a type outside the current crate is required.
|
2016-01-14 16:06:53 +01:00
|
|
|
|
//!
|
2017-04-01 15:35:03 +11:00
|
|
|
|
//! # Generic Implementations
|
2016-01-14 16:06:53 +01:00
|
|
|
|
//!
|
2017-01-29 23:33:12 +01:00
|
|
|
|
//! - [`AsRef`] and [`AsMut`] auto-dereference if the inner type is a reference
|
|
|
|
|
//! - [`From`]`<U> for T` implies [`Into`]`<T> for U`
|
|
|
|
|
//! - [`TryFrom`]`<U> for T` implies [`TryInto`]`<T> for U`
|
2017-04-03 07:57:20 +10:00
|
|
|
|
//! - [`From`] and [`Into`] are reflexive, which means that all types can
|
2017-04-18 08:29:05 +10:00
|
|
|
|
//! `into` themselves and `from` themselves
|
2016-01-14 16:06:53 +01:00
|
|
|
|
//!
|
2015-04-06 14:25:39 -04:00
|
|
|
|
//! See each trait for usage examples.
|
2017-01-29 23:33:12 +01:00
|
|
|
|
//!
|
|
|
|
|
//! [`Into`]: trait.Into.html
|
|
|
|
|
//! [`From`]: trait.From.html
|
|
|
|
|
//! [`TryFrom`]: trait.TryFrom.html
|
|
|
|
|
//! [`TryInto`]: trait.TryInto.html
|
|
|
|
|
//! [`AsRef`]: trait.AsRef.html
|
|
|
|
|
//! [`AsMut`]: trait.AsMut.html
|
2015-03-18 09:14:54 -07:00
|
|
|
|
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
|
2019-04-15 11:23:21 +09:00
|
|
|
|
use crate::fmt;
|
2019-02-17 21:30:00 +01:00
|
|
|
|
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// An identity function.
|
|
|
|
|
///
|
|
|
|
|
/// Two things are important to note about this function:
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
|
|
|
|
/// - It is not always equivalent to a closure like `|x| x` since the
|
|
|
|
|
/// closure may coerce `x` into a different type.
|
|
|
|
|
///
|
|
|
|
|
/// - It moves the input `x` passed to the function.
|
|
|
|
|
///
|
|
|
|
|
/// While it might seem strange to have a function that just returns back the
|
|
|
|
|
/// input, there are some interesting uses.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// Using `identity` to do nothing among other interesting functions:
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// use std::convert::identity;
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
|
|
|
|
/// fn manipulation(x: u32) -> u32 {
|
|
|
|
|
/// // Let's assume that this function does something interesting.
|
|
|
|
|
/// x + 1
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// let _arr = &[identity, manipulation];
|
2018-01-19 01:27:59 +01:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// Using `identity` to get a function that changes nothing in a conditional:
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// use std::convert::identity;
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
2018-01-19 03:11:57 +01:00
|
|
|
|
/// # let condition = true;
|
|
|
|
|
///
|
|
|
|
|
/// # fn manipulation(x: u32) -> u32 { x + 1 }
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// let do_stuff = if condition { manipulation } else { identity };
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
|
|
|
|
/// // do more interesting stuff..
|
2018-01-19 03:11:57 +01:00
|
|
|
|
///
|
2018-01-19 01:27:59 +01:00
|
|
|
|
/// let _results = do_stuff(42);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// Using `identity` to keep the `Some` variants of an iterator of `Option<T>`:
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// use std::convert::identity;
|
2018-01-19 01:27:59 +01:00
|
|
|
|
///
|
|
|
|
|
/// let iter = vec![Some(1), None, Some(3)].into_iter();
|
2018-08-19 18:34:46 +02:00
|
|
|
|
/// let filtered = iter.filter_map(identity).collect::<Vec<_>>();
|
2018-01-19 01:27:59 +01:00
|
|
|
|
/// assert_eq!(vec![1, 3], filtered);
|
|
|
|
|
/// ```
|
2019-01-04 01:42:31 +01:00
|
|
|
|
#[stable(feature = "convert_id", since = "1.33.0")]
|
2018-01-19 01:27:59 +01:00
|
|
|
|
#[inline]
|
2018-08-19 18:49:31 +02:00
|
|
|
|
pub const fn identity<T>(x: T) -> T { x }
|
2018-01-19 01:27:59 +01:00
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// Used to do a cheap reference-to-reference conversion.
|
2019-03-25 22:21:05 +01:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// This trait is similar to [`AsMut`] which is used for converting between mutable references.
|
|
|
|
|
/// If you need to do a costly conversion it is better to implement [`From`] with type
|
2019-03-21 18:49:12 +01:00
|
|
|
|
/// `&T` or write a custom function.
|
2017-04-03 07:57:20 +10:00
|
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
2019-04-03 11:19:16 +03:00
|
|
|
|
/// `AsRef` has the same signature as [`Borrow`], but `Borrow` is different in few aspects:
|
2019-03-25 22:21:05 +01:00
|
|
|
|
///
|
2019-04-03 11:19:16 +03:00
|
|
|
|
/// - Unlike `AsRef`, `Borrow` has a blanket impl for any `T`, and can be used to accept either
|
|
|
|
|
/// a reference or a value.
|
|
|
|
|
/// - `Borrow` also requires that `Hash`, `Eq` and `Ord` for borrowed value are
|
|
|
|
|
/// equivalent to those of the owned value. For this reason, if you want to
|
|
|
|
|
/// borrow only a single field of a struct you can implement `AsRef`, but not `Borrow`.
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
2016-08-29 00:07:03 +02:00
|
|
|
|
/// [`Borrow`]: ../../std/borrow/trait.Borrow.html
|
2015-04-30 14:40:38 -04:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// **Note: This trait must not fail**. If the conversion can fail, use a
|
2017-04-03 07:57:20 +10:00
|
|
|
|
/// dedicated method which returns an [`Option<T>`] or a [`Result<T, E>`].
|
2016-08-29 00:07:03 +02:00
|
|
|
|
///
|
|
|
|
|
/// [`Option<T>`]: ../../std/option/enum.Option.html
|
|
|
|
|
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// # Generic Implementations
|
|
|
|
|
///
|
|
|
|
|
/// - `AsRef` auto-dereferences if the inner type is a reference or a mutable
|
2017-04-18 08:29:05 +10:00
|
|
|
|
/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type
|
|
|
|
|
/// `&mut Foo` or `&&mut Foo`)
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
2015-04-06 14:25:39 -04:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// By using trait bounds we can accept arguments of different types as long as they can be
|
2019-03-21 18:49:12 +01:00
|
|
|
|
/// converted a the specified type `T`.
|
2019-03-25 22:21:05 +01:00
|
|
|
|
///
|
2019-03-21 18:49:12 +01:00
|
|
|
|
/// For example: By creating a generic function that takes an `AsRef<str>` we express that we
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// want to accept all references that can be converted to &str as an argument.
|
|
|
|
|
/// Since both [`String`] and `&str` implement `AsRef<str>` we can accept both as input argument.
|
2016-08-29 00:07:03 +02:00
|
|
|
|
///
|
|
|
|
|
/// [`String`]: ../../std/string/struct.String.html
|
2015-04-06 14:25:39 -04:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// fn is_hello<T: AsRef<str>>(s: T) {
|
|
|
|
|
/// assert_eq!("hello", s.as_ref());
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let s = "hello";
|
|
|
|
|
/// is_hello(s);
|
|
|
|
|
///
|
|
|
|
|
/// let s = "hello".to_string();
|
|
|
|
|
/// is_hello(s);
|
|
|
|
|
/// ```
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
pub trait AsRef<T: ?Sized> {
|
2015-04-04 10:26:14 -04:00
|
|
|
|
/// Performs the conversion.
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_ref(&self) -> &T;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// Used to do a cheap mutable-to-mutable reference conversion.
|
2019-03-25 22:21:05 +01:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// This trait is similar to [`AsRef`] but used for converting between mutable
|
|
|
|
|
/// references. If you need to do a costly conversion it is better to
|
2019-03-21 18:49:12 +01:00
|
|
|
|
/// implement [`From`] with type `&mut T` or write a custom function.
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// **Note: This trait must not fail**. If the conversion can fail, use a
|
2017-04-03 07:57:20 +10:00
|
|
|
|
/// dedicated method which returns an [`Option<T>`] or a [`Result<T, E>`].
|
2016-08-29 00:07:03 +02:00
|
|
|
|
///
|
|
|
|
|
/// [`Option<T>`]: ../../std/option/enum.Option.html
|
|
|
|
|
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// # Generic Implementations
|
|
|
|
|
///
|
2018-02-04 11:57:36 -08:00
|
|
|
|
/// - `AsMut` auto-dereferences if the inner type is a mutable reference
|
|
|
|
|
/// (e.g.: `foo.as_mut()` will work the same if `foo` has type `&mut Foo`
|
|
|
|
|
/// or `&mut &mut Foo`)
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
2016-09-15 19:47:04 -06:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2019-03-21 18:49:12 +01:00
|
|
|
|
/// Using `AsMut` as trait bound for a generic function we can accept all mutable references
|
|
|
|
|
/// that can be converted to type `&mut T`. Because [`Box<T>`] implements `AsMut<T>` we can
|
|
|
|
|
/// write a function `add_one`that takes all arguments that can be converted to `&mut u64`.
|
|
|
|
|
/// Because [`Box<T>`] implements `AsMut<T>` `add_one` accepts arguments of type
|
|
|
|
|
/// `&mut Box<u64>` as well:
|
2016-09-15 19:47:04 -06:00
|
|
|
|
/// ```
|
|
|
|
|
/// fn add_one<T: AsMut<u64>>(num: &mut T) {
|
|
|
|
|
/// *num.as_mut() += 1;
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let mut boxed_num = Box::new(0);
|
|
|
|
|
/// add_one(&mut boxed_num);
|
|
|
|
|
/// assert_eq!(*boxed_num, 1);
|
|
|
|
|
/// ```
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// [`Box<T>`]: ../../std/boxed/struct.Box.html
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
pub trait AsMut<T: ?Sized> {
|
2015-04-04 10:26:14 -04:00
|
|
|
|
/// Performs the conversion.
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_mut(&mut self) -> &mut T;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// A value-to-value conversion that consumes the input value. The
|
|
|
|
|
/// opposite of [`From`].
|
2015-04-06 14:25:39 -04:00
|
|
|
|
///
|
2019-03-21 15:06:16 +01:00
|
|
|
|
/// One should only implement [`Into`] if a conversion to a type outside the current crate is
|
|
|
|
|
/// required. Otherwise one should always prefer implementing [`From`] over [`Into`] because
|
2019-03-25 22:21:05 +01:00
|
|
|
|
/// implementing [`From`] automatically provides one with a implementation of [`Into`] thanks to
|
|
|
|
|
/// the blanket implementation in the standard library. [`From`] cannot do these type of
|
|
|
|
|
/// conversions because of Rust's orphaning rules.
|
2016-01-15 01:24:33 +01:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// **Note: This trait must not fail**. If the conversion can fail, use [`TryInto`].
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// # Generic Implementations
|
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// - [`From<T>`]` for U` implies `Into<U> for T`
|
|
|
|
|
/// - [`Into`]` is reflexive, which means that `Into<T> for T` is implemented
|
2017-05-20 15:24:43 -04:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// # Implementing `Into` for conversions to external types
|
2019-03-25 22:21:05 +01:00
|
|
|
|
///
|
2019-03-21 15:06:16 +01:00
|
|
|
|
/// If the destination type is not part of the current crate
|
|
|
|
|
/// then you can't implement [`From`] directly.
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// For example, take this code:
|
2017-05-20 15:24:43 -04:00
|
|
|
|
///
|
|
|
|
|
/// ```compile_fail
|
|
|
|
|
/// struct Wrapper<T>(Vec<T>);
|
|
|
|
|
/// impl<T> From<Wrapper<T>> for Vec<T> {
|
|
|
|
|
/// fn from(w: Wrapper<T>) -> Vec<T> {
|
|
|
|
|
/// w.0
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// This will fail to compile because we cannot implement a trait for a type
|
|
|
|
|
/// if both the trait and the type are not defined by the current crate.
|
|
|
|
|
/// This is due to Rust's orphaning rules. To bypass this, you can implement `Into` directly:
|
2017-05-20 15:24:43 -04:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// struct Wrapper<T>(Vec<T>);
|
|
|
|
|
/// impl<T> Into<Vec<T>> for Wrapper<T> {
|
|
|
|
|
/// fn into(self) -> Vec<T> {
|
|
|
|
|
/// self.0
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2019-03-21 18:49:12 +01:00
|
|
|
|
/// It is important to understand that `Into` does not provide a [`From`] implementation
|
|
|
|
|
/// (as [`From`] does with `Into`). Therefore, you should always try to implement [`From`]
|
2019-03-21 15:06:16 +01:00
|
|
|
|
/// and then fall back to `Into` if [`From`] can't be implemented.
|
2019-03-25 22:21:05 +01:00
|
|
|
|
///
|
2019-03-21 18:49:12 +01:00
|
|
|
|
/// Prefer using `Into` over [`From`] when specifying trait bounds on a generic function
|
|
|
|
|
/// to ensure that types that only implement `Into` can be used as well.
|
2017-05-20 15:24:43 -04:00
|
|
|
|
///
|
2015-04-06 14:25:39 -04:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2016-08-29 00:07:03 +02:00
|
|
|
|
/// [`String`] implements `Into<Vec<u8>>`:
|
2015-04-06 14:25:39 -04:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// In order to express that we want a generic function to take all arguments that can be
|
2019-03-21 18:49:12 +01:00
|
|
|
|
/// converted to a specified type `T`, we can use a trait bound of `Into<T>`.
|
|
|
|
|
/// For example: The function `is_hello` takes all arguments that can be converted into a
|
|
|
|
|
/// `Vec<u8>`.
|
2019-03-20 23:15:41 +01:00
|
|
|
|
///
|
2015-04-06 14:25:39 -04:00
|
|
|
|
/// ```
|
|
|
|
|
/// fn is_hello<T: Into<Vec<u8>>>(s: T) {
|
|
|
|
|
/// let bytes = b"hello".to_vec();
|
|
|
|
|
/// assert_eq!(bytes, s.into());
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let s = "hello".to_string();
|
|
|
|
|
/// is_hello(s);
|
|
|
|
|
/// ```
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2016-08-29 00:07:03 +02:00
|
|
|
|
/// [`TryInto`]: trait.TryInto.html
|
|
|
|
|
/// [`Option<T>`]: ../../std/option/enum.Option.html
|
|
|
|
|
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
|
|
|
|
|
/// [`String`]: ../../std/string/struct.String.html
|
|
|
|
|
/// [From]: trait.From.html
|
2017-03-12 14:04:52 -04:00
|
|
|
|
/// [`into`]: trait.Into.html#tymethod.into
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
pub trait Into<T>: Sized {
|
2015-04-04 10:26:14 -04:00
|
|
|
|
/// Performs the conversion.
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn into(self) -> T;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// Used to do value-to-value conversions while consuming the input value. It is the reciprocal of
|
|
|
|
|
/// [`Into`].
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
2019-03-21 15:06:16 +01:00
|
|
|
|
/// One should always prefer implementing [`From`] over [`Into`]
|
|
|
|
|
/// because implementing [`From`] automatically provides one with a implementation of [`Into`]
|
|
|
|
|
/// thanks to the blanket implementation in the standard library.
|
2019-03-25 22:21:05 +01:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// Only implement [`Into`] if a conversion to a type outside the current crate is required.
|
|
|
|
|
/// [`From`] cannot do these type of conversions because of Rust's orphaning rules.
|
|
|
|
|
/// See [`Into`] for more details.
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// Prefer using [`Into`] over using [`From`] when specifying trait bounds on a generic function.
|
|
|
|
|
/// This way, types that directly implement [`Into`] can be used as arguments as well.
|
2017-04-18 08:29:05 +10:00
|
|
|
|
///
|
2019-03-25 22:21:05 +01:00
|
|
|
|
/// The [`From`] is also very useful when performing error handling. When constructing a function
|
|
|
|
|
/// that is capable of failing, the return type will generally be of the form `Result<T, E>`.
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// The `From` trait simplifies error handling by allowing a function to return a single error type
|
2019-03-25 22:21:05 +01:00
|
|
|
|
/// that encapsulate multiple error types. See the "Examples" section and [the book][book] for more
|
|
|
|
|
/// details.
|
2015-04-06 14:25:39 -04:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// **Note: This trait must not fail**. If the conversion can fail, use [`TryFrom`].
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// # Generic Implementations
|
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// - [`From<T>`]` for U` implies [`Into<U>`]` for T`
|
|
|
|
|
/// - [`From`] is reflexive, which means that `From<T> for T` is implemented
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
2015-04-06 14:25:39 -04:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2016-08-29 00:07:03 +02:00
|
|
|
|
/// [`String`] implements `From<&str>`:
|
2015-04-06 14:25:39 -04:00
|
|
|
|
///
|
2019-03-20 23:15:41 +01:00
|
|
|
|
/// An explicit conversion from a &str to a String is done as follows:
|
2015-04-06 14:25:39 -04:00
|
|
|
|
/// ```
|
|
|
|
|
/// let string = "hello".to_string();
|
2015-04-26 23:18:19 -04:00
|
|
|
|
/// let other_string = String::from("hello");
|
2015-04-06 14:25:39 -04:00
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(string, other_string);
|
|
|
|
|
/// ```
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2019-03-25 22:21:05 +01:00
|
|
|
|
/// While performing error handling it is often useful to implement `From` for your own error type.
|
|
|
|
|
/// By converting underlying error types to our own custom error type that encapsulates the
|
|
|
|
|
/// underlying error type, we can return a single error type without losing information on the
|
|
|
|
|
/// underlying cause. The '?' operator automatically converts the underlying error type to our
|
|
|
|
|
/// custom error type by calling `Into<CliError>::into` which is automatically provided when
|
|
|
|
|
/// implementing `From`. The compiler then infers which implementation of `Into` should be used.
|
2017-04-01 15:35:03 +11:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
2018-11-16 16:22:06 -05:00
|
|
|
|
/// use std::fs;
|
|
|
|
|
/// use std::io;
|
2017-04-04 08:27:20 +10:00
|
|
|
|
/// use std::num;
|
|
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// enum CliError {
|
|
|
|
|
/// IoError(io::Error),
|
|
|
|
|
/// ParseError(num::ParseIntError),
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2017-04-04 08:27:20 +10:00
|
|
|
|
/// impl From<io::Error> for CliError {
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// fn from(error: io::Error) -> Self {
|
|
|
|
|
/// CliError::IoError(error)
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2017-04-04 08:27:20 +10:00
|
|
|
|
/// impl From<num::ParseIntError> for CliError {
|
|
|
|
|
/// fn from(error: num::ParseIntError) -> Self {
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// CliError::ParseError(error)
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2017-04-07 18:26:36 +10:00
|
|
|
|
/// fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
|
2018-11-16 16:22:06 -05:00
|
|
|
|
/// let mut contents = fs::read_to_string(&file_name)?;
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// let num: i32 = contents.trim().parse()?;
|
2017-04-07 18:26:36 +10:00
|
|
|
|
/// Ok(num)
|
2017-04-01 15:35:03 +11:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-01-14 16:06:53 +01:00
|
|
|
|
///
|
2016-08-29 00:07:03 +02:00
|
|
|
|
/// [`TryFrom`]: trait.TryFrom.html
|
|
|
|
|
/// [`Option<T>`]: ../../std/option/enum.Option.html
|
|
|
|
|
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
|
|
|
|
|
/// [`String`]: ../../std/string/struct.String.html
|
2016-10-21 00:49:47 +01:00
|
|
|
|
/// [`Into<U>`]: trait.Into.html
|
2017-03-12 14:04:52 -04:00
|
|
|
|
/// [`from`]: trait.From.html#tymethod.from
|
2019-03-21 18:42:15 +01:00
|
|
|
|
/// [book]: ../../book/ch09-00-error-handling.html
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-03-17 22:42:51 -07:00
|
|
|
|
#[rustc_on_unimplemented(
|
|
|
|
|
on(
|
|
|
|
|
all(_Self="&str", T="std::string::String"),
|
2019-03-22 20:38:09 -07:00
|
|
|
|
note="to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
|
2019-03-17 22:42:51 -07:00
|
|
|
|
)
|
|
|
|
|
)]
|
2015-10-23 21:51:38 -04:00
|
|
|
|
pub trait From<T>: Sized {
|
2015-04-04 10:26:14 -04:00
|
|
|
|
/// Performs the conversion.
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-05-02 06:35:36 +02:00
|
|
|
|
fn from(_: T) -> Self;
|
2015-03-18 09:14:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 07:57:20 +10:00
|
|
|
|
/// An attempted conversion that consumes `self`, which may or may not be
|
|
|
|
|
/// expensive.
|
2016-05-04 22:42:14 -07:00
|
|
|
|
///
|
2019-02-13 12:52:35 -05:00
|
|
|
|
/// Library authors should usually not directly implement this trait,
|
|
|
|
|
/// but should prefer implementing the [`TryFrom`] trait, which offers
|
|
|
|
|
/// greater flexibility and provides an equivalent `TryInto`
|
|
|
|
|
/// implementation for free, thanks to a blanket implementation in the
|
|
|
|
|
/// standard library. For more information on this, see the
|
|
|
|
|
/// documentation for [`Into`].
|
2019-01-31 21:47:18 -05:00
|
|
|
|
///
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// # Implementing `TryInto`
|
2019-01-31 21:47:18 -05:00
|
|
|
|
///
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// This suffers the same restrictions and reasoning as implementing
|
|
|
|
|
/// [`Into`], see there for details.
|
2016-08-29 00:07:03 +02:00
|
|
|
|
///
|
|
|
|
|
/// [`TryFrom`]: trait.TryFrom.html
|
2017-05-20 15:24:43 -04:00
|
|
|
|
/// [`Into`]: trait.Into.html
|
2019-02-08 15:00:47 +01:00
|
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2016-05-04 22:42:14 -07:00
|
|
|
|
pub trait TryInto<T>: Sized {
|
|
|
|
|
/// The type returned in the event of a conversion error.
|
2019-02-08 15:00:47 +01:00
|
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-03-05 13:00:32 -08:00
|
|
|
|
type Error;
|
2016-05-04 22:42:14 -07:00
|
|
|
|
|
|
|
|
|
/// Performs the conversion.
|
2019-02-08 15:00:47 +01:00
|
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-03-05 13:00:32 -08:00
|
|
|
|
fn try_into(self) -> Result<T, Self::Error>;
|
2016-05-04 22:42:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// Simple and safe type conversions that may fail in a controlled
|
|
|
|
|
/// way under some circumstances. It is the reciprocal of [`TryInto`].
|
2019-01-31 21:47:18 -05:00
|
|
|
|
///
|
|
|
|
|
/// This is useful when you are doing a type conversion that may
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// trivially succeed but may also need special handling.
|
|
|
|
|
/// For example, there is no way to convert an `i64` into an `i32`
|
|
|
|
|
/// using the [`From`] trait, because an `i64` may contain a value
|
|
|
|
|
/// that an `i32` cannot represent and so the conversion would lose data.
|
|
|
|
|
/// This might be handled by truncating the `i64` to an `i32` (essentially
|
2019-01-31 21:47:18 -05:00
|
|
|
|
/// giving the `i64`'s value modulo `i32::MAX`) or by simply returning
|
|
|
|
|
/// `i32::MAX`, or by some other method. The `From` trait is intended
|
2019-02-13 12:52:35 -05:00
|
|
|
|
/// for perfect conversions, so the `TryFrom` trait informs the
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// programmer when a type conversion could go bad and lets them
|
|
|
|
|
/// decide how to handle it.
|
2019-01-31 21:47:18 -05:00
|
|
|
|
///
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// # Generic Implementations
|
|
|
|
|
///
|
|
|
|
|
/// - `TryFrom<T> for U` implies [`TryInto<U>`]` for T`
|
2019-01-31 21:47:18 -05:00
|
|
|
|
/// - [`try_from`] is reflexive, which means that `TryFrom<T> for T`
|
2019-02-13 12:52:35 -05:00
|
|
|
|
/// is implemented and cannot fail -- the associated `Error` type for
|
2019-02-26 23:47:55 -05:00
|
|
|
|
/// calling `T::try_from()` on a value of type `T` is `Infallible`.
|
|
|
|
|
/// When the `!` type is stablized `Infallible` and `!` will be
|
|
|
|
|
/// equivalent.
|
2019-01-31 21:47:18 -05:00
|
|
|
|
///
|
2019-03-29 23:22:11 +01:00
|
|
|
|
/// `TryFrom<T>` can be implemented as follows:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::convert::TryFrom;
|
|
|
|
|
///
|
|
|
|
|
/// struct SuperiorThanZero(i32);
|
|
|
|
|
///
|
|
|
|
|
/// impl TryFrom<i32> for SuperiorThanZero {
|
|
|
|
|
/// type Error = &'static str;
|
|
|
|
|
///
|
|
|
|
|
/// fn try_from(value: i32) -> Result<Self, Self::Error> {
|
|
|
|
|
/// if value < 0 {
|
|
|
|
|
/// Err("SuperiorThanZero only accepts value superior than zero!")
|
|
|
|
|
/// } else {
|
|
|
|
|
/// Ok(SuperiorThanZero(value))
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// As described, [`i32`] implements `TryFrom<i64>`:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2019-02-14 09:19:03 -05:00
|
|
|
|
/// use std::convert::TryFrom;
|
|
|
|
|
///
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// let big_number = 1_000_000_000_000i64;
|
|
|
|
|
/// // Silently truncates `big_number`, requires detecting
|
|
|
|
|
/// // and handling the truncation after the fact.
|
|
|
|
|
/// let smaller_number = big_number as i32;
|
|
|
|
|
/// assert_eq!(smaller_number, -727379968);
|
2019-01-31 21:47:18 -05:00
|
|
|
|
///
|
2019-02-13 12:52:35 -05:00
|
|
|
|
/// // Returns an error because `big_number` is too big to
|
|
|
|
|
/// // fit in an `i32`.
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// let try_smaller_number = i32::try_from(big_number);
|
|
|
|
|
/// assert!(try_smaller_number.is_err());
|
2019-01-31 21:47:18 -05:00
|
|
|
|
///
|
2019-02-13 12:52:35 -05:00
|
|
|
|
/// // Returns `Ok(3)`.
|
2019-01-30 19:42:37 -05:00
|
|
|
|
/// let try_successful_smaller_number = i32::try_from(3);
|
|
|
|
|
/// assert!(try_successful_smaller_number.is_ok());
|
|
|
|
|
/// ```
|
2019-02-13 12:52:35 -05:00
|
|
|
|
///
|
|
|
|
|
/// [`try_from`]: trait.TryFrom.html#tymethod.try_from
|
|
|
|
|
/// [`TryInto`]: trait.TryInto.html
|
2019-02-08 15:00:47 +01:00
|
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2016-05-04 22:42:14 -07:00
|
|
|
|
pub trait TryFrom<T>: Sized {
|
|
|
|
|
/// The type returned in the event of a conversion error.
|
2019-02-08 15:00:47 +01:00
|
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-03-05 13:00:32 -08:00
|
|
|
|
type Error;
|
2016-05-04 22:42:14 -07:00
|
|
|
|
|
|
|
|
|
/// Performs the conversion.
|
2019-02-08 15:00:47 +01:00
|
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-03-05 13:00:32 -08:00
|
|
|
|
fn try_from(value: T) -> Result<Self, Self::Error>;
|
2016-05-04 22:42:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// GENERIC IMPLS
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// As lifts over &
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-09-03 04:50:14 -07:00
|
|
|
|
impl<T: ?Sized, U: ?Sized> AsRef<U> for &T where T: AsRef<U>
|
2017-04-01 15:35:03 +11:00
|
|
|
|
{
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_ref(&self) -> &U {
|
|
|
|
|
<T as AsRef<U>>::as_ref(*self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// As lifts over &mut
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-09-03 04:50:14 -07:00
|
|
|
|
impl<T: ?Sized, U: ?Sized> AsRef<U> for &mut T where T: AsRef<U>
|
2017-04-01 15:35:03 +11:00
|
|
|
|
{
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_ref(&self) -> &U {
|
|
|
|
|
<T as AsRef<U>>::as_ref(*self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-17 11:04:33 +02:00
|
|
|
|
// FIXME (#45742): replace the above impls for &/&mut with the following more general one:
|
2015-03-26 13:39:23 -07:00
|
|
|
|
// // As lifts over Deref
|
|
|
|
|
// impl<D: ?Sized + Deref, U: ?Sized> AsRef<U> for D where D::Target: AsRef<U> {
|
|
|
|
|
// fn as_ref(&self) -> &U {
|
|
|
|
|
// self.deref().as_ref()
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
|
// AsMut lifts over &mut
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-09-03 04:50:14 -07:00
|
|
|
|
impl<T: ?Sized, U: ?Sized> AsMut<U> for &mut T where T: AsMut<U>
|
2017-04-01 15:35:03 +11:00
|
|
|
|
{
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_mut(&mut self) -> &mut U {
|
|
|
|
|
(*self).as_mut()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-17 11:04:33 +02:00
|
|
|
|
// FIXME (#45742): replace the above impl for &mut with the following more general one:
|
2015-03-26 13:39:23 -07:00
|
|
|
|
// // AsMut lifts over DerefMut
|
|
|
|
|
// impl<D: ?Sized + Deref, U: ?Sized> AsMut<U> for D where D::Target: AsMut<U> {
|
|
|
|
|
// fn as_mut(&mut self) -> &mut U {
|
|
|
|
|
// self.deref_mut().as_mut()
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
|
// From implies Into
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-04-18 08:29:05 +10:00
|
|
|
|
impl<T, U> Into<U> for T where U: From<T>
|
2017-04-01 15:35:03 +11:00
|
|
|
|
{
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn into(self) -> U {
|
|
|
|
|
U::from(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 15:15:27 -07:00
|
|
|
|
// From (and thus Into) is reflexive
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<T> From<T> for T {
|
2017-04-18 08:29:05 +10:00
|
|
|
|
fn from(t: T) -> T { t }
|
2015-03-30 15:15:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-04 22:42:14 -07:00
|
|
|
|
|
|
|
|
|
// TryFrom implies TryInto
|
2019-02-08 15:00:47 +01:00
|
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-04-18 08:29:05 +10:00
|
|
|
|
impl<T, U> TryInto<U> for T where U: TryFrom<T>
|
2017-04-01 15:35:03 +11:00
|
|
|
|
{
|
2017-03-05 13:00:32 -08:00
|
|
|
|
type Error = U::Error;
|
2016-05-04 22:42:14 -07:00
|
|
|
|
|
2017-03-05 13:00:32 -08:00
|
|
|
|
fn try_into(self) -> Result<U, U::Error> {
|
2016-05-04 22:42:14 -07:00
|
|
|
|
U::try_from(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-29 22:13:21 -07:00
|
|
|
|
// Infallible conversions are semantically equivalent to fallible conversions
|
|
|
|
|
// with an uninhabited error type.
|
2019-02-08 15:00:47 +01:00
|
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2018-12-13 16:53:50 -05:00
|
|
|
|
impl<T, U> TryFrom<U> for T where U: Into<T> {
|
2019-02-08 14:55:44 +01:00
|
|
|
|
type Error = Infallible;
|
2017-08-29 22:13:21 -07:00
|
|
|
|
|
|
|
|
|
fn try_from(value: U) -> Result<Self, Self::Error> {
|
2018-12-13 16:53:50 -05:00
|
|
|
|
Ok(U::into(value))
|
2017-08-29 22:13:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// CONCRETE IMPLS
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
impl<T> AsRef<[T]> for [T] {
|
|
|
|
|
fn as_ref(&self) -> &[T] {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
impl<T> AsMut<[T]> for [T] {
|
|
|
|
|
fn as_mut(&mut self) -> &mut [T] {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
impl AsRef<str> for str {
|
2015-05-10 14:06:41 +02:00
|
|
|
|
#[inline]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-08 14:54:22 +01:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// THE NO-ERROR ERROR TYPE
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
/// ```ignore (illustrates std code, duplicating the impl in a doctest would be an error)
|
|
|
|
|
/// 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:
|
|
|
|
|
///
|
|
|
|
|
/// ```ignore (illustrates future std change)
|
|
|
|
|
/// pub type Infallible = !;
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// … and eventually deprecate `Infallible`.
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// However there is one case where `!` syntax can be used
|
|
|
|
|
/// before `!` is stabilized as a full-fleged type: in the position of a function’s return type.
|
|
|
|
|
/// Specifically, it is possible 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 `impl`s will start to overlap
|
|
|
|
|
/// and therefore will be disallowed by the language’s trait coherence rules.
|
|
|
|
|
///
|
|
|
|
|
/// [`Ok`]: ../result/enum.Result.html#variant.Ok
|
|
|
|
|
/// [`Result`]: ../result/enum.Result.html
|
|
|
|
|
/// [`TryFrom`]: trait.TryFrom.html
|
|
|
|
|
/// [`Into`]: trait.Into.html
|
|
|
|
|
/// [never]: ../../std/primitive.never.html
|
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
#[derive(Copy)]
|
|
|
|
|
pub enum Infallible {}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
impl Clone for Infallible {
|
|
|
|
|
fn clone(&self) -> Infallible {
|
|
|
|
|
match *self {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
impl fmt::Debug for Infallible {
|
|
|
|
|
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match *self {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
impl fmt::Display for Infallible {
|
|
|
|
|
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match *self {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
impl PartialEq for Infallible {
|
|
|
|
|
fn eq(&self, _: &Infallible) -> bool {
|
|
|
|
|
match *self {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
impl Eq for Infallible {}
|
2019-02-13 18:01:37 +01:00
|
|
|
|
|
2019-02-17 21:30:00 +01:00
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
impl PartialOrd for Infallible {
|
|
|
|
|
fn partial_cmp(&self, _other: &Self) -> Option<crate::cmp::Ordering> {
|
|
|
|
|
match *self {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
impl Ord for Infallible {
|
|
|
|
|
fn cmp(&self, _other: &Self) -> crate::cmp::Ordering {
|
|
|
|
|
match *self {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 18:01:37 +01:00
|
|
|
|
#[stable(feature = "convert_infallible", since = "1.34.0")]
|
|
|
|
|
impl From<!> for Infallible {
|
|
|
|
|
fn from(x: !) -> Self {
|
|
|
|
|
x
|
|
|
|
|
}
|
|
|
|
|
}
|