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 //! A module for working with borrowed data.
13 #![stable(feature = "rust1", since = "1.0.0")]
15 use core::cmp::Ordering;
16 use core::hash::{Hash, Hasher};
23 #[stable(feature = "rust1", since = "1.0.0")]
24 pub use core::borrow::{Borrow, BorrowMut};
26 #[stable(feature = "rust1", since = "1.0.0")]
27 impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B>
29 <B as ToOwned>::Owned: 'a
31 fn borrow(&self) -> &B {
36 /// A generalization of `Clone` to borrowed data.
38 /// Some types make it possible to go from borrowed to owned, usually by
39 /// implementing the `Clone` trait. But `Clone` works only for going from `&T`
40 /// to `T`. The `ToOwned` trait generalizes `Clone` to construct owned data
41 /// from any borrow of a given type.
42 #[stable(feature = "rust1", since = "1.0.0")]
44 #[stable(feature = "rust1", since = "1.0.0")]
45 type Owned: Borrow<Self>;
47 /// Creates owned data from borrowed data, usually by cloning.
54 /// let s = "a"; // &str
55 /// let ss = s.to_owned(); // String
57 /// let v = &[1, 2]; // slice
58 /// let vv = v.to_owned(); // Vec
60 #[stable(feature = "rust1", since = "1.0.0")]
61 fn to_owned(&self) -> Self::Owned;
64 #[stable(feature = "rust1", since = "1.0.0")]
65 impl<T> ToOwned for T where T: Clone {
67 fn to_owned(&self) -> T {
72 /// A clone-on-write smart pointer.
74 /// The type `Cow` is a smart pointer providing clone-on-write functionality: it
75 /// can enclose and provide immutable access to borrowed data, and clone the
76 /// data lazily when mutation or ownership is required. The type is designed to
77 /// work with general borrowed data via the `Borrow` trait.
79 /// `Cow` implements `Deref`, which means that you can call
80 /// non-mutating methods directly on the data it encloses. If mutation
81 /// is desired, `to_mut` will obtain a mutable reference to an owned
82 /// value, cloning if necessary.
87 /// use std::borrow::Cow;
89 /// # #[allow(dead_code)]
90 /// fn abs_all(input: &mut Cow<[i32]>) {
91 /// for i in 0..input.len() {
94 /// // clones into a vector the first time (if not already owned)
95 /// input.to_mut()[i] = -v;
100 #[stable(feature = "rust1", since = "1.0.0")]
101 pub enum Cow<'a, B: ?Sized + 'a>
105 #[stable(feature = "rust1", since = "1.0.0")]
106 Borrowed(#[stable(feature = "rust1", since = "1.0.0")] &'a B),
109 #[stable(feature = "rust1", since = "1.0.0")]
111 #[stable(feature = "rust1", since = "1.0.0")] <B as ToOwned>::Owned
115 #[stable(feature = "rust1", since = "1.0.0")]
116 impl<'a, B: ?Sized> Clone for Cow<'a, B> where B: ToOwned {
117 fn clone(&self) -> Cow<'a, B> {
119 Borrowed(b) => Borrowed(b),
121 let b: &B = o.borrow();
128 impl<'a, B: ?Sized> Cow<'a, B> where B: ToOwned {
129 /// Acquires a mutable reference to the owned form of the data.
131 /// Clones the data if it is not already owned.
136 /// use std::borrow::Cow;
138 /// let mut cow: Cow<[_]> = Cow::Owned(vec![1, 2, 3]);
140 /// let hello = cow.to_mut();
142 /// assert_eq!(hello, &[1, 2, 3]);
144 #[stable(feature = "rust1", since = "1.0.0")]
145 pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned {
147 Borrowed(borrowed) => {
148 *self = Owned(borrowed.to_owned());
151 Owned(ref mut owned) => owned,
155 /// Extracts the owned data.
157 /// Clones the data if it is not already owned.
162 /// use std::borrow::Cow;
164 /// let cow: Cow<[_]> = Cow::Owned(vec![1, 2, 3]);
166 /// let hello = cow.into_owned();
168 /// assert_eq!(vec![1, 2, 3], hello);
170 #[stable(feature = "rust1", since = "1.0.0")]
171 pub fn into_owned(self) -> <B as ToOwned>::Owned {
173 Borrowed(borrowed) => borrowed.to_owned(),
174 Owned(owned) => owned,
179 #[stable(feature = "rust1", since = "1.0.0")]
180 impl<'a, B: ?Sized> Deref for Cow<'a, B> where B: ToOwned {
183 fn deref(&self) -> &B {
185 Borrowed(borrowed) => borrowed,
186 Owned(ref owned) => owned.borrow(),
191 #[stable(feature = "rust1", since = "1.0.0")]
192 impl<'a, B: ?Sized> Eq for Cow<'a, B> where B: Eq + ToOwned {}
194 #[stable(feature = "rust1", since = "1.0.0")]
195 impl<'a, B: ?Sized> Ord for Cow<'a, B> where B: Ord + ToOwned {
197 fn cmp(&self, other: &Cow<'a, B>) -> Ordering {
198 Ord::cmp(&**self, &**other)
202 #[stable(feature = "rust1", since = "1.0.0")]
203 impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, C>> for Cow<'a, B>
204 where B: PartialEq<C> + ToOwned,
208 fn eq(&self, other: &Cow<'b, C>) -> bool {
209 PartialEq::eq(&**self, &**other)
213 #[stable(feature = "rust1", since = "1.0.0")]
214 impl<'a, B: ?Sized> PartialOrd for Cow<'a, B> where B: PartialOrd + ToOwned {
216 fn partial_cmp(&self, other: &Cow<'a, B>) -> Option<Ordering> {
217 PartialOrd::partial_cmp(&**self, &**other)
221 #[stable(feature = "rust1", since = "1.0.0")]
222 impl<'a, B: ?Sized> fmt::Debug for Cow<'a, B>
223 where B: fmt::Debug + ToOwned,
224 <B as ToOwned>::Owned: fmt::Debug
226 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
228 Borrowed(ref b) => fmt::Debug::fmt(b, f),
229 Owned(ref o) => fmt::Debug::fmt(o, f),
234 #[stable(feature = "rust1", since = "1.0.0")]
235 impl<'a, B: ?Sized> fmt::Display for Cow<'a, B>
236 where B: fmt::Display + ToOwned,
237 <B as ToOwned>::Owned: fmt::Display
239 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
241 Borrowed(ref b) => fmt::Display::fmt(b, f),
242 Owned(ref o) => fmt::Display::fmt(o, f),
247 #[stable(feature = "default", since = "1.11.0")]
248 impl<'a, B: ?Sized> Default for Cow<'a, B>
250 <B as ToOwned>::Owned: Default
252 fn default() -> Cow<'a, B> {
253 Owned(<B as ToOwned>::Owned::default())
257 #[stable(feature = "rust1", since = "1.0.0")]
258 impl<'a, B: ?Sized> Hash for Cow<'a, B> where B: Hash + ToOwned {
260 fn hash<H: Hasher>(&self, state: &mut H) {
261 Hash::hash(&**self, state)
265 #[stable(feature = "rust1", since = "1.0.0")]
267 impl<'a, T: ?Sized + ToOwned> AsRef<T> for Cow<'a, T> {
268 fn as_ref(&self) -> &T {