2015-03-18 09:14:54 -07:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Traits for conversions between types.
|
|
|
|
//!
|
2015-06-09 11:18:03 -07:00
|
|
|
//! The traits in this module provide a general way to talk about conversions
|
|
|
|
//! from one type to another. They follow the standard Rust conventions of
|
|
|
|
//! `as`/`into`/`from`.
|
2015-04-06 14:25:39 -04:00
|
|
|
//!
|
2015-06-09 11:18:03 -07:00
|
|
|
//! Like many traits, these are often used as bounds for generic functions, to
|
|
|
|
//! support arguments of multiple types.
|
2015-04-06 14:25:39 -04:00
|
|
|
//!
|
2017-04-01 15:35:03 +11:00
|
|
|
//! - Implement the `As*` traits for reference-to-reference conversions
|
|
|
|
//! - Implement the [`Into`] trait when you want to consume the value in the conversion
|
2017-01-29 23:33:12 +01:00
|
|
|
//! - The [`From`] trait is the most flexible, useful for value _and_ reference conversions
|
|
|
|
//! - The [`TryFrom`] and [`TryInto`] traits behave like [`From`] and [`Into`], but allow for the
|
2016-05-04 22:42:14 -07:00
|
|
|
//! conversion to fail
|
2016-01-14 16:06:53 +01:00
|
|
|
//!
|
2017-01-29 23:33:12 +01:00
|
|
|
//! As a library author, you should prefer implementing [`From<T>`][`From`] or
|
|
|
|
//! [`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
|
|
|
|
//! blanket implementation in the standard library.
|
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
|
|
|
|
//! `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
|
|
|
|
2017-03-05 13:00:32 -08:00
|
|
|
use str::FromStr;
|
|
|
|
|
2017-04-03 07:57:20 +10:00
|
|
|
/// A cheap reference-to-reference conversion. Used to convert a value to a
|
|
|
|
/// reference value within generic code.
|
|
|
|
///
|
|
|
|
/// `AsRef` is very similar to, but serves a slightly different purpose than,
|
|
|
|
/// [`Borrow`].
|
|
|
|
///
|
|
|
|
/// `AsRef` is to be used when wishing to convert to a reference of another
|
|
|
|
/// type.
|
|
|
|
/// `Borrow` is more related to the notion of taking the reference. It is
|
|
|
|
/// useful when wishing to abstract
|
|
|
|
/// over the type of reference (`&T`, `&mut T`) or allow both the referenced
|
|
|
|
/// and owned type to be treated in the same manner.
|
2017-04-01 15:35:03 +11:00
|
|
|
/// The key difference between the two traits is the intention:
|
|
|
|
///
|
|
|
|
/// - Use `AsRef` when goal is to simply convert into a reference
|
2017-04-03 07:57:20 +10:00
|
|
|
/// - Use `Borrow` when goal is related to writing code that is agnostic to the
|
|
|
|
/// type of borrow and if is reference or value
|
2017-04-01 15:35:03 +11:00
|
|
|
///
|
|
|
|
/// See [the book][book] for a more detailed comparison.
|
2015-04-30 14:40:38 -04:00
|
|
|
///
|
|
|
|
/// [book]: ../../book/borrow-and-asref.html
|
2016-08-29 00:07:03 +02:00
|
|
|
/// [`Borrow`]: ../../std/borrow/trait.Borrow.html
|
2015-04-30 14:40:38 -04:00
|
|
|
///
|
2017-04-03 07:57:20 +10:00
|
|
|
/// **Note: this trait must not fail**. If the conversion can fail, use a
|
|
|
|
/// 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-03 07:57:20 +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
|
|
|
|
///
|
2016-08-29 00:07:03 +02:00
|
|
|
/// Both [`String`] and `&str` implement `AsRef<str>`:
|
|
|
|
///
|
|
|
|
/// [`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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A cheap, mutable reference-to-mutable reference conversion.
|
2016-01-14 16:06:53 +01:00
|
|
|
///
|
2017-04-04 08:27:20 +10:00
|
|
|
/// This trait is similar to `AsRef` but used for converting between mutable
|
|
|
|
/// references.
|
2017-04-01 15:35:03 +11:00
|
|
|
///
|
2017-04-03 07:57:20 +10:00
|
|
|
/// **Note: this trait must not fail**. If the conversion can fail, use a
|
|
|
|
/// 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
|
|
|
|
///
|
|
|
|
/// - `AsMut` auto-dereferences if the inner type is a reference or a mutable
|
2017-04-03 07:57:20 +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
|
|
|
///
|
2016-09-15 19:47:04 -06:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`Box<T>`] implements `AsMut<T>`:
|
|
|
|
///
|
|
|
|
/// [`Box<T>`]: ../../std/boxed/struct.Box.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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);
|
|
|
|
/// ```
|
|
|
|
///
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-03 07:57:20 +10:00
|
|
|
/// A conversion that consumes `self`, which may or may not be expensive. The
|
|
|
|
/// reciprocal of [`From`][From].
|
2015-04-06 14:25:39 -04:00
|
|
|
///
|
2017-04-03 07:57:20 +10:00
|
|
|
/// **Note: this trait must not fail**. If the conversion can fail, use
|
|
|
|
/// [`TryInto`] or a dedicated method which returns an [`Option<T>`] or a
|
|
|
|
/// [`Result<T, E>`].
|
2016-01-15 01:24:33 +01:00
|
|
|
///
|
2017-04-03 07:57:20 +10:00
|
|
|
/// Library authors should not directly implement this trait, but should prefer
|
|
|
|
/// implementing the [`From`][From] trait, which offers greater flexibility and
|
|
|
|
/// provides an equivalent `Into` implementation for free, thanks to a blanket
|
|
|
|
/// implementation in the standard library.
|
2016-01-14 16:06:53 +01:00
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
/// # Generic Implementations
|
|
|
|
///
|
|
|
|
/// - [`From<T>`][From]` for U` implies `Into<U> for T`
|
|
|
|
/// - [`into`] is reflexive, which means that `Into<T> for T` is implemented
|
|
|
|
///
|
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
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2017-04-03 07:57:20 +10:00
|
|
|
/// Simple and safe type conversions in to `Self`. It is the reciprocal of
|
|
|
|
/// `Into`.
|
2017-04-01 15:35:03 +11:00
|
|
|
///
|
2017-04-03 07:57:20 +10:00
|
|
|
/// This trait is useful when performing error handling as described by
|
|
|
|
/// [the book][book] and is closely related to the `?` operator.
|
2017-04-01 15:35:03 +11:00
|
|
|
///
|
2017-04-03 07:57:20 +10:00
|
|
|
/// When constructing a function that is capable of failing the return type
|
|
|
|
/// will generally be of the form `Result<T, E>`.
|
|
|
|
/// The `From` trait allows for simplification of error handling by providing a
|
|
|
|
/// means of returning a single error type that encapsulates numerous possible
|
|
|
|
/// erroneous situations.
|
|
|
|
/// This trait is not limited to error handling, rather the general case for
|
|
|
|
/// this trait would be in any type conversions to have an explicit definition
|
|
|
|
/// of how they are performed.
|
2015-04-06 14:25:39 -04:00
|
|
|
///
|
2017-04-03 07:57:20 +10:00
|
|
|
/// **Note: this trait must not fail**. If the conversion can fail, use
|
|
|
|
/// [`TryFrom`] or a dedicated method which returns an [`Option<T>`] or a
|
|
|
|
/// [`Result<T, E>`].
|
2016-01-14 16:06:53 +01:00
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
/// # Generic Implementations
|
|
|
|
///
|
|
|
|
/// - `From<T> for U` implies [`Into<U>`]` for T`
|
|
|
|
/// - [`from`] is reflexive, which means that `From<T> for T` is implemented
|
|
|
|
///
|
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
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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
|
|
|
///
|
2017-04-01 15:35:03 +11:00
|
|
|
/// An example usage for error handling:
|
|
|
|
///
|
|
|
|
/// ```
|
2017-04-04 08:27:20 +10:00
|
|
|
/// use std::io::{self, Read};
|
|
|
|
/// 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> {
|
|
|
|
/// let mut file = std::fs::File::open("test")?;
|
2017-04-01 15:35:03 +11:00
|
|
|
/// let mut contents = String::new();
|
|
|
|
/// file.read_to_string(&mut contents)?;
|
|
|
|
/// 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
|
2017-04-03 07:57:20 +10:00
|
|
|
/// [book]: ../../book/error-handling.html
|
2015-03-30 15:15:27 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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")]
|
2015-03-18 09:14:54 -07:00
|
|
|
fn from(T) -> Self;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
///
|
2017-04-03 07:57:20 +10:00
|
|
|
/// Library authors should 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.
|
2016-08-29 00:07:03 +02:00
|
|
|
///
|
|
|
|
/// [`TryFrom`]: trait.TryFrom.html
|
2016-05-04 22:42:14 -07:00
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
|
|
|
pub trait TryInto<T>: Sized {
|
|
|
|
/// The type returned in the event of a conversion error.
|
2017-03-05 13:00:32 -08:00
|
|
|
type Error;
|
2016-05-04 22:42:14 -07:00
|
|
|
|
|
|
|
/// Performs the conversion.
|
2017-03-05 13:00:32 -08:00
|
|
|
fn try_into(self) -> Result<T, Self::Error>;
|
2016-05-04 22:42:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to construct `Self` via a conversion.
|
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
|
|
|
pub trait TryFrom<T>: Sized {
|
|
|
|
/// The type returned in the event of a conversion error.
|
2017-03-05 13:00:32 -08:00
|
|
|
type Error;
|
2016-05-04 22:42:14 -07:00
|
|
|
|
|
|
|
/// Performs the conversion.
|
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")]
|
2017-04-01 15:35:03 +11:00
|
|
|
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T
|
|
|
|
where T: AsRef<U>
|
|
|
|
{
|
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")]
|
2017-04-01 15:35:03 +11:00
|
|
|
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T
|
|
|
|
where T: AsRef<U>
|
|
|
|
{
|
2015-03-18 09:14:54 -07:00
|
|
|
fn as_ref(&self) -> &U {
|
|
|
|
<T as AsRef<U>>::as_ref(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 13:39:23 -07:00
|
|
|
// FIXME (#23442): replace the above impls for &/&mut with the following more general one:
|
|
|
|
// // 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")]
|
2017-04-01 15:35:03 +11:00
|
|
|
impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T
|
|
|
|
where T: AsMut<U>
|
|
|
|
{
|
2015-03-18 09:14:54 -07:00
|
|
|
fn as_mut(&mut self) -> &mut U {
|
|
|
|
(*self).as_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 13:39:23 -07:00
|
|
|
// FIXME (#23442): replace the above impl for &mut with the following more general one:
|
|
|
|
// // 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-01 15:35:03 +11:00
|
|
|
impl<T, U> Into<U> for T
|
|
|
|
where U: From<T>
|
|
|
|
{
|
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-01 15:35:03 +11: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
|
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
2017-04-01 15:35:03 +11:00
|
|
|
impl<T, U> TryInto<U> for T
|
|
|
|
where U: TryFrom<T>
|
|
|
|
{
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 13:00:32 -08:00
|
|
|
|
|
|
|
// FromStr implies TryFrom<&str>
|
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
2017-04-01 15:35:03 +11:00
|
|
|
impl<'a, T> TryFrom<&'a str> for T
|
|
|
|
where T: FromStr
|
|
|
|
{
|
2017-03-05 13:00:32 -08:00
|
|
|
type Error = <T as FromStr>::Err;
|
|
|
|
|
|
|
|
fn try_from(s: &'a str) -> Result<T, Self::Error> {
|
|
|
|
FromStr::from_str(s)
|
|
|
|
}
|
|
|
|
}
|