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`/`to`/`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.
29 /// Both `String` and `&str` implement `AsRef<str>`:
32 /// fn is_hello<T: AsRef<str>>(s: T) {
33 /// assert_eq!("hello", s.as_ref());
39 /// let s = "hello".to_string();
42 #[stable(feature = "rust1", since = "1.0.0")]
43 pub trait AsRef<T: ?Sized> {
44 /// Performs the conversion.
45 #[stable(feature = "rust1", since = "1.0.0")]
46 fn as_ref(&self) -> &T;
49 /// A cheap, mutable reference-to-mutable reference conversion.
50 #[stable(feature = "rust1", since = "1.0.0")]
51 pub trait AsMut<T: ?Sized> {
52 /// Performs the conversion.
53 #[stable(feature = "rust1", since = "1.0.0")]
54 fn as_mut(&mut self) -> &mut T;
57 /// A conversion that consumes `self`, which may or may not be expensive.
61 /// `String` implements `Into<Vec<u8>>`:
64 /// fn is_hello<T: Into<Vec<u8>>>(s: T) {
65 /// let bytes = b"hello".to_vec();
66 /// assert_eq!(bytes, s.into());
69 /// let s = "hello".to_string();
72 #[stable(feature = "rust1", since = "1.0.0")]
73 pub trait Into<T>: Sized {
74 /// Performs the conversion.
75 #[stable(feature = "rust1", since = "1.0.0")]
79 /// Construct `Self` via a conversion.
83 /// `String` implements `From<&str>`:
87 /// let string = "hello".to_string();
89 /// let other_string: String = From::from(s);
91 /// assert_eq!(string, other_string);
93 #[stable(feature = "rust1", since = "1.0.0")]
95 /// Performs the conversion.
96 #[stable(feature = "rust1", since = "1.0.0")]
100 ////////////////////////////////////////////////////////////////////////////////
102 ////////////////////////////////////////////////////////////////////////////////
105 #[stable(feature = "rust1", since = "1.0.0")]
106 impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T where T: AsRef<U> {
107 fn as_ref(&self) -> &U {
108 <T as AsRef<U>>::as_ref(*self)
112 // As lifts over &mut
113 #[stable(feature = "rust1", since = "1.0.0")]
114 impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
115 fn as_ref(&self) -> &U {
116 <T as AsRef<U>>::as_ref(*self)
120 // FIXME (#23442): replace the above impls for &/&mut with the following more general one:
121 // // As lifts over Deref
122 // impl<D: ?Sized + Deref, U: ?Sized> AsRef<U> for D where D::Target: AsRef<U> {
123 // fn as_ref(&self) -> &U {
124 // self.deref().as_ref()
128 // AsMut lifts over &mut
129 #[stable(feature = "rust1", since = "1.0.0")]
130 impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
131 fn as_mut(&mut self) -> &mut U {
136 // FIXME (#23442): replace the above impl for &mut with the following more general one:
137 // // AsMut lifts over DerefMut
138 // impl<D: ?Sized + Deref, U: ?Sized> AsMut<U> for D where D::Target: AsMut<U> {
139 // fn as_mut(&mut self) -> &mut U {
140 // self.deref_mut().as_mut()
145 #[stable(feature = "rust1", since = "1.0.0")]
146 impl<T, U> Into<U> for T where U: From<T> {
152 // From (and thus Into) is reflexive
153 #[stable(feature = "rust1", since = "1.0.0")]
154 impl<T> From<T> for T {
155 fn from(t: T) -> T { t }
158 ////////////////////////////////////////////////////////////////////////////////
160 ////////////////////////////////////////////////////////////////////////////////
162 #[stable(feature = "rust1", since = "1.0.0")]
163 impl<T> AsRef<[T]> for [T] {
164 fn as_ref(&self) -> &[T] {
169 #[stable(feature = "rust1", since = "1.0.0")]
170 impl<T> AsMut<[T]> for [T] {
171 fn as_mut(&mut self) -> &mut [T] {
176 #[stable(feature = "rust1", since = "1.0.0")]
177 impl AsRef<str> for str {
178 fn as_ref(&self) -> &str {