2015-03-18 11:14:54 -05: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 13:18:03 -05: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 13:25:39 -05:00
|
|
|
//!
|
2015-06-09 13:18:03 -05:00
|
|
|
//! Like many traits, these are often used as bounds for generic functions, to
|
|
|
|
//! support arguments of multiple types.
|
2015-04-06 13:25:39 -05:00
|
|
|
//!
|
2016-01-31 05:26:15 -06:00
|
|
|
//! - Impl the `As*` traits for reference-to-reference conversions
|
|
|
|
//! - Impl the `Into` trait when you want to consume the value in the conversion
|
2016-03-29 11:23:09 -05:00
|
|
|
//! - The `From` trait is the most flexible, useful for value _and_ reference conversions
|
2016-05-05 00:42:14 -05:00
|
|
|
//! - The `TryFrom` and `TryInto` traits behave like `From` and `Into`, but allow for the
|
|
|
|
//! conversion to fail
|
2016-01-14 09:06:53 -06:00
|
|
|
//!
|
2016-05-05 00:42:14 -05:00
|
|
|
//! As a library author, you should prefer implementing `From<T>` or `TryFrom<T>` rather than
|
|
|
|
//! `Into<U>` or `TryInto<U>`, as `From` and `TryFrom` provide greater flexibility and offer
|
|
|
|
//! equivalent `Into` or `TryInto` implementations for free, thanks to a blanket implementation
|
|
|
|
//! in the standard library.
|
2016-01-14 09:06:53 -06:00
|
|
|
//!
|
|
|
|
//! # Generic impl
|
|
|
|
//!
|
|
|
|
//! - `AsRef` and `AsMut` auto-dereference if the inner type is a reference
|
|
|
|
//! - `From<U> for T` implies `Into<T> for U`
|
2016-05-05 00:42:14 -05:00
|
|
|
//! - `TryFrom<U> for T` implies `TryInto<T> for U`
|
2016-01-14 09:06:53 -06:00
|
|
|
//! - `From` and `Into` are reflexive, which means that all types can `into()`
|
2016-01-14 18:24:33 -06:00
|
|
|
//! themselves and `from()` themselves
|
2016-01-14 09:06:53 -06:00
|
|
|
//!
|
2015-04-06 13:25:39 -05:00
|
|
|
//! See each trait for usage examples.
|
2015-03-18 11:14:54 -05:00
|
|
|
|
2015-03-30 17:15:27 -05:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
|
|
|
|
use marker::Sized;
|
2016-05-05 00:42:14 -05:00
|
|
|
use result::Result;
|
2015-03-18 11:14:54 -05:00
|
|
|
|
|
|
|
/// A cheap, reference-to-reference conversion.
|
2015-04-06 13:25:39 -05:00
|
|
|
///
|
2015-04-30 13:40:38 -05:00
|
|
|
/// `AsRef` is very similar to, but different than, `Borrow`. See
|
|
|
|
/// [the book][book] for more.
|
|
|
|
///
|
|
|
|
/// [book]: ../../book/borrow-and-asref.html
|
|
|
|
///
|
2016-01-14 18:24:33 -06:00
|
|
|
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
|
2016-03-29 11:23:09 -05:00
|
|
|
/// returns an `Option<T>` or a `Result<T, E>`.
|
2016-01-14 09:06:53 -06:00
|
|
|
///
|
2015-04-06 13:25:39 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Both `String` and `&str` implement `AsRef<str>`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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 09:06:53 -06:00
|
|
|
///
|
|
|
|
/// # Generic Impls
|
|
|
|
///
|
|
|
|
/// - `AsRef` auto-dereference if the inner type is a reference or a mutable
|
2016-01-14 18:24:33 -06:00
|
|
|
/// reference (eg: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
|
2016-01-14 09:06:53 -06:00
|
|
|
///
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub trait AsRef<T: ?Sized> {
|
2015-04-04 09:26:14 -05:00
|
|
|
/// Performs the conversion.
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
fn as_ref(&self) -> &T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A cheap, mutable reference-to-mutable reference conversion.
|
2016-01-14 09:06:53 -06:00
|
|
|
///
|
2016-01-14 18:24:33 -06:00
|
|
|
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
|
2016-03-29 11:23:09 -05:00
|
|
|
/// returns an `Option<T>` or a `Result<T, E>`.
|
2016-01-14 09:06:53 -06:00
|
|
|
///
|
|
|
|
/// # Generic Impls
|
|
|
|
///
|
|
|
|
/// - `AsMut` auto-dereference if the inner type is a reference or a mutable
|
2016-01-14 18:24:33 -06:00
|
|
|
/// reference (eg: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
|
2016-01-14 09:06:53 -06:00
|
|
|
///
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub trait AsMut<T: ?Sized> {
|
2015-04-04 09:26:14 -05:00
|
|
|
/// Performs the conversion.
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
fn as_mut(&mut self) -> &mut T;
|
|
|
|
}
|
|
|
|
|
2015-04-06 13:25:39 -05:00
|
|
|
/// A conversion that consumes `self`, which may or may not be expensive.
|
|
|
|
///
|
2016-05-05 00:42:14 -05: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-14 18:24:33 -06:00
|
|
|
///
|
2016-03-29 11:23:09 -05:00
|
|
|
/// Library authors should not directly implement this trait, but should prefer implementing
|
|
|
|
/// the `From` trait, which offers greater flexibility and provides an equivalent `Into`
|
2016-01-31 05:26:15 -06:00
|
|
|
/// implementation for free, thanks to a blanket implementation in the standard library.
|
2016-01-14 09:06:53 -06:00
|
|
|
///
|
2015-04-06 13:25:39 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// `String` implements `Into<Vec<u8>>`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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 09:06:53 -06:00
|
|
|
///
|
2016-01-31 05:26:15 -06:00
|
|
|
/// # Generic Impls
|
2016-01-14 09:06:53 -06:00
|
|
|
///
|
|
|
|
/// - `From<T> for U` implies `Into<U> for T`
|
|
|
|
/// - `into()` is reflexive, which means that `Into<T> for T` is implemented
|
|
|
|
///
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub trait Into<T>: Sized {
|
2015-04-04 09:26:14 -05:00
|
|
|
/// Performs the conversion.
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
fn into(self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct `Self` via a conversion.
|
2015-04-06 13:25:39 -05:00
|
|
|
///
|
2016-05-05 00:42:14 -05: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 09:06:53 -06:00
|
|
|
///
|
2015-04-06 13:25:39 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// `String` implements `From<&str>`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let string = "hello".to_string();
|
2015-04-26 22:18:19 -05:00
|
|
|
/// let other_string = String::from("hello");
|
2015-04-06 13:25:39 -05:00
|
|
|
///
|
|
|
|
/// assert_eq!(string, other_string);
|
|
|
|
/// ```
|
2016-01-14 09:06:53 -06:00
|
|
|
/// # Generic impls
|
|
|
|
///
|
|
|
|
/// - `From<T> for U` implies `Into<U> for T`
|
|
|
|
/// - `from()` is reflexive, which means that `From<T> for T` is implemented
|
|
|
|
///
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-10-23 20:51:38 -05:00
|
|
|
pub trait From<T>: Sized {
|
2015-04-04 09:26:14 -05:00
|
|
|
/// Performs the conversion.
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
fn from(T) -> Self;
|
|
|
|
}
|
|
|
|
|
2016-05-05 00:42:14 -05:00
|
|
|
/// An attempted conversion that consumes `self`, which may or may not be expensive.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
|
|
|
pub trait TryInto<T>: Sized {
|
|
|
|
/// The type returned in the event of a conversion error.
|
|
|
|
type Err;
|
|
|
|
|
|
|
|
/// Performs the conversion.
|
|
|
|
fn try_into(self) -> Result<T, Self::Err>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
type Err;
|
|
|
|
|
|
|
|
/// Performs the conversion.
|
|
|
|
fn try_from(T) -> Result<Self, Self::Err>;
|
|
|
|
}
|
|
|
|
|
2015-03-18 11:14:54 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// GENERIC IMPLS
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// As lifts over &
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T where T: AsRef<U> {
|
|
|
|
fn as_ref(&self) -> &U {
|
|
|
|
<T as AsRef<U>>::as_ref(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As lifts over &mut
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
|
|
|
|
fn as_ref(&self) -> &U {
|
|
|
|
<T as AsRef<U>>::as_ref(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:39:23 -05: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 11:14:54 -05:00
|
|
|
// AsMut lifts over &mut
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
|
|
|
|
fn as_mut(&mut self) -> &mut U {
|
|
|
|
(*self).as_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:39:23 -05: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 11:14:54 -05:00
|
|
|
// From implies Into
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
impl<T, U> Into<U> for T where U: From<T> {
|
|
|
|
fn into(self) -> U {
|
|
|
|
U::from(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:15:27 -05:00
|
|
|
// From (and thus Into) is reflexive
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T> From<T> for T {
|
|
|
|
fn from(t: T) -> T { t }
|
|
|
|
}
|
|
|
|
|
2016-05-05 00:42:14 -05:00
|
|
|
|
|
|
|
// TryFrom implies TryInto
|
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
|
|
|
impl<T, U> TryInto<U> for T where U: TryFrom<T> {
|
|
|
|
type Err = U::Err;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<U, U::Err> {
|
|
|
|
U::try_from(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 11:14:54 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CONCRETE IMPLS
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
impl<T> AsRef<[T]> for [T] {
|
|
|
|
fn as_ref(&self) -> &[T] {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
impl<T> AsMut<[T]> for [T] {
|
|
|
|
fn as_mut(&mut self) -> &mut [T] {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:15:27 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
impl AsRef<str> for str {
|
2015-05-10 07:06:41 -05:00
|
|
|
#[inline]
|
2015-03-18 11:14:54 -05:00
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|