1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Traits for conversions between types.
13 //! The traits in this module provide a general way to talk about conversions from one type to
14 //! another. They follow the standard Rust conventions of `as`/`into`/`from`.
16 //! Like many traits, these are often used as bounds for generic functions, to support arguments of
19 //! See each trait for usage examples.
21 #![stable(feature = "rust1", since = "1.0.0")]
25 /// A cheap, reference-to-reference conversion.
27 /// `AsRef` is very similar to, but different than, `Borrow`. See
28 /// [the book][book] for more.
30 /// [book]: ../../book/borrow-and-asref.html
34 /// Both `String` and `&str` implement `AsRef<str>`:
37 /// fn is_hello<T: AsRef<str>>(s: T) {
38 /// assert_eq!("hello", s.as_ref());
44 /// let s = "hello".to_string();
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub trait AsRef<T: ?Sized> {
49 /// Performs the conversion.
50 #[stable(feature = "rust1", since = "1.0.0")]
51 fn as_ref(&self) -> &T;
54 /// A cheap, mutable reference-to-mutable reference conversion.
55 #[stable(feature = "rust1", since = "1.0.0")]
56 pub trait AsMut<T: ?Sized> {
57 /// Performs the conversion.
58 #[stable(feature = "rust1", since = "1.0.0")]
59 fn as_mut(&mut self) -> &mut T;
62 /// A conversion that consumes `self`, which may or may not be expensive.
66 /// `String` implements `Into<Vec<u8>>`:
69 /// fn is_hello<T: Into<Vec<u8>>>(s: T) {
70 /// let bytes = b"hello".to_vec();
71 /// assert_eq!(bytes, s.into());
74 /// let s = "hello".to_string();
77 #[stable(feature = "rust1", since = "1.0.0")]
78 pub trait Into<T>: Sized {
79 /// Performs the conversion.
80 #[stable(feature = "rust1", since = "1.0.0")]
84 /// Construct `Self` via a conversion.
88 /// `String` implements `From<&str>`:
91 /// let string = "hello".to_string();
92 /// let other_string = String::from("hello");
94 /// assert_eq!(string, other_string);
96 #[stable(feature = "rust1", since = "1.0.0")]
98 /// Performs the conversion.
99 #[stable(feature = "rust1", since = "1.0.0")]
103 ////////////////////////////////////////////////////////////////////////////////
105 ////////////////////////////////////////////////////////////////////////////////
108 #[stable(feature = "rust1", since = "1.0.0")]
109 impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T where T: AsRef<U> {
110 fn as_ref(&self) -> &U {
111 <T as AsRef<U>>::as_ref(*self)
115 // As lifts over &mut
116 #[stable(feature = "rust1", since = "1.0.0")]
117 impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
118 fn as_ref(&self) -> &U {
119 <T as AsRef<U>>::as_ref(*self)
123 // FIXME (#23442): replace the above impls for &/&mut with the following more general one:
124 // // As lifts over Deref
125 // impl<D: ?Sized + Deref, U: ?Sized> AsRef<U> for D where D::Target: AsRef<U> {
126 // fn as_ref(&self) -> &U {
127 // self.deref().as_ref()
131 // AsMut lifts over &mut
132 #[stable(feature = "rust1", since = "1.0.0")]
133 impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
134 fn as_mut(&mut self) -> &mut U {
139 // FIXME (#23442): replace the above impl for &mut with the following more general one:
140 // // AsMut lifts over DerefMut
141 // impl<D: ?Sized + Deref, U: ?Sized> AsMut<U> for D where D::Target: AsMut<U> {
142 // fn as_mut(&mut self) -> &mut U {
143 // self.deref_mut().as_mut()
148 #[stable(feature = "rust1", since = "1.0.0")]
149 impl<T, U> Into<U> for T where U: From<T> {
155 // From (and thus Into) is reflexive
156 #[stable(feature = "rust1", since = "1.0.0")]
157 impl<T> From<T> for T {
158 fn from(t: T) -> T { t }
161 ////////////////////////////////////////////////////////////////////////////////
163 ////////////////////////////////////////////////////////////////////////////////
165 #[stable(feature = "rust1", since = "1.0.0")]
166 impl<T> AsRef<[T]> for [T] {
167 fn as_ref(&self) -> &[T] {
172 #[stable(feature = "rust1", since = "1.0.0")]
173 impl<T> AsMut<[T]> for [T] {
174 fn as_mut(&mut self) -> &mut [T] {
179 #[stable(feature = "rust1", since = "1.0.0")]
180 impl AsRef<str> for str {
182 fn as_ref(&self) -> &str {