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::clone::Clone;
16 use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
17 use core::hash::{Hash, Hasher};
18 use core::marker::Sized;
20 use core::option::Option;
27 /// A trait for borrowing data.
29 /// In general, there may be several ways to "borrow" a piece of data. The
30 /// typical ways of borrowing a type `T` are `&T` (a shared borrow) and `&mut T`
31 /// (a mutable borrow). But types like `Vec<T>` provide additional kinds of
32 /// borrows: the borrowed slices `&[T]` and `&mut [T]`.
34 /// When writing generic code, it is often desirable to abstract over all ways
35 /// of borrowing data from a given type. That is the role of the `Borrow`
36 /// trait: if `T: Borrow<U>`, then `&U` can be borrowed from `&T`. A given
37 /// type can be borrowed as multiple different types. In particular, `Vec<T>:
38 /// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub trait Borrow<Borrowed: ?Sized> {
41 /// Immutably borrow from an owned value.
42 #[stable(feature = "rust1", since = "1.0.0")]
43 fn borrow(&self) -> &Borrowed;
46 /// A trait for mutably borrowing data.
48 /// Similar to `Borrow`, but for mutable borrows.
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub trait BorrowMut<Borrowed: ?Sized> : Borrow<Borrowed> {
51 /// Mutably borrow from an owned value.
52 #[stable(feature = "rust1", since = "1.0.0")]
53 fn borrow_mut(&mut self) -> &mut Borrowed;
56 #[stable(feature = "rust1", since = "1.0.0")]
57 impl<T: ?Sized> Borrow<T> for T {
58 fn borrow(&self) -> &T { self }
61 #[stable(feature = "rust1", since = "1.0.0")]
62 impl<T: ?Sized> BorrowMut<T> for T {
63 fn borrow_mut(&mut self) -> &mut T { self }
66 #[stable(feature = "rust1", since = "1.0.0")]
67 impl<'a, T: ?Sized> Borrow<T> for &'a T {
68 fn borrow(&self) -> &T { &**self }
71 #[stable(feature = "rust1", since = "1.0.0")]
72 impl<'a, T: ?Sized> Borrow<T> for &'a mut T {
73 fn borrow(&self) -> &T { &**self }
76 #[stable(feature = "rust1", since = "1.0.0")]
77 impl<'a, T: ?Sized> BorrowMut<T> for &'a mut T {
78 fn borrow_mut(&mut self) -> &mut T { &mut **self }
81 impl<T> Borrow<T> for rc::Rc<T> {
82 fn borrow(&self) -> &T { &**self }
85 impl<T> Borrow<T> for arc::Arc<T> {
86 fn borrow(&self) -> &T { &**self }
89 #[stable(feature = "rust1", since = "1.0.0")]
90 impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B> where B: ToOwned, <B as ToOwned>::Owned: 'a {
91 fn borrow(&self) -> &B {
96 /// A generalization of Clone to borrowed data.
98 /// Some types make it possible to go from borrowed to owned, usually by
99 /// implementing the `Clone` trait. But `Clone` works only for going from `&T`
100 /// to `T`. The `ToOwned` trait generalizes `Clone` to construct owned data
101 /// from any borrow of a given type.
102 #[stable(feature = "rust1", since = "1.0.0")]
104 #[stable(feature = "rust1", since = "1.0.0")]
105 type Owned: Borrow<Self>;
107 /// Create owned data from borrowed data, usually by copying.
108 #[stable(feature = "rust1", since = "1.0.0")]
109 fn to_owned(&self) -> Self::Owned;
112 #[stable(feature = "rust1", since = "1.0.0")]
113 impl<T> ToOwned for T where T: Clone {
115 fn to_owned(&self) -> T { self.clone() }
118 /// A clone-on-write smart pointer.
120 /// The type `Cow` is a smart pointer providing clone-on-write functionality: it
121 /// can enclose and provide immutable access to borrowed data, and clone the
122 /// data lazily when mutation or ownership is required. The type is designed to
123 /// work with general borrowed data via the `Borrow` trait.
125 /// `Cow` implements both `Deref`, which means that you can call
126 /// non-mutating methods directly on the data it encloses. If mutation
127 /// is desired, `to_mut` will obtain a mutable references to an owned
128 /// value, cloning if necessary.
133 /// use std::borrow::Cow;
135 /// fn abs_all(input: &mut Cow<[int]>) {
136 /// for i in 0..input.len() {
137 /// let v = input[i];
139 /// // clones into a vector the first time (if not already owned)
140 /// input.to_mut()[i] = -v;
145 #[stable(feature = "rust1", since = "1.0.0")]
146 pub enum Cow<'a, B: ?Sized + 'a> where B: ToOwned {
148 #[stable(feature = "rust1", since = "1.0.0")]
152 #[stable(feature = "rust1", since = "1.0.0")]
153 Owned(<B as ToOwned>::Owned)
156 #[stable(feature = "rust1", since = "1.0.0")]
157 impl<'a, B: ?Sized> Clone for Cow<'a, B> where B: ToOwned {
158 fn clone(&self) -> Cow<'a, B> {
160 Borrowed(b) => Borrowed(b),
162 let b: &B = o.borrow();
169 impl<'a, B: ?Sized> Cow<'a, B> where B: ToOwned {
170 /// Acquire a mutable reference to the owned form of the data.
172 /// Copies the data if it is not already owned.
173 #[stable(feature = "rust1", since = "1.0.0")]
174 pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned {
176 Borrowed(borrowed) => {
177 *self = Owned(borrowed.to_owned());
180 Owned(ref mut owned) => owned
184 /// Extract the owned data.
186 /// Copies the data if it is not already owned.
187 #[stable(feature = "rust1", since = "1.0.0")]
188 pub fn into_owned(self) -> <B as ToOwned>::Owned {
190 Borrowed(borrowed) => borrowed.to_owned(),
191 Owned(owned) => owned
195 /// Returns true if this `Cow` wraps a borrowed value
196 #[deprecated(since = "1.0.0", reason = "match on the enum instead")]
197 #[unstable(feature = "std_misc")]
198 pub fn is_borrowed(&self) -> bool {
205 /// Returns true if this `Cow` wraps an owned value
206 #[deprecated(since = "1.0.0", reason = "match on the enum instead")]
207 #[unstable(feature = "std_misc")]
208 pub fn is_owned(&self) -> bool {
216 #[stable(feature = "rust1", since = "1.0.0")]
217 impl<'a, B: ?Sized> Deref for Cow<'a, B> where B: ToOwned {
220 fn deref(&self) -> &B {
222 Borrowed(borrowed) => borrowed,
223 Owned(ref owned) => owned.borrow()
228 #[stable(feature = "rust1", since = "1.0.0")]
229 impl<'a, B: ?Sized> Eq for Cow<'a, B> where B: Eq + ToOwned {}
231 #[stable(feature = "rust1", since = "1.0.0")]
232 impl<'a, B: ?Sized> Ord for Cow<'a, B> where B: Ord + ToOwned {
234 fn cmp(&self, other: &Cow<'a, B>) -> Ordering {
235 Ord::cmp(&**self, &**other)
239 #[stable(feature = "rust1", since = "1.0.0")]
240 impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, C>> for Cow<'a, B> where
241 B: PartialEq<C> + ToOwned, C: ToOwned,
244 fn eq(&self, other: &Cow<'b, C>) -> bool {
245 PartialEq::eq(&**self, &**other)
249 #[stable(feature = "rust1", since = "1.0.0")]
250 impl<'a, B: ?Sized> PartialOrd for Cow<'a, B> where B: PartialOrd + ToOwned,
253 fn partial_cmp(&self, other: &Cow<'a, B>) -> Option<Ordering> {
254 PartialOrd::partial_cmp(&**self, &**other)
258 #[stable(feature = "rust1", since = "1.0.0")]
259 impl<'a, B: ?Sized> fmt::Debug for Cow<'a, B> where
260 B: fmt::Debug + ToOwned,
261 <B as ToOwned>::Owned: fmt::Debug,
263 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
265 Borrowed(ref b) => fmt::Debug::fmt(b, f),
266 Owned(ref o) => fmt::Debug::fmt(o, f),
271 #[stable(feature = "rust1", since = "1.0.0")]
272 impl<'a, B: ?Sized> fmt::Display for Cow<'a, B> where
273 B: fmt::Display + ToOwned,
274 <B as ToOwned>::Owned: fmt::Display,
276 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
278 Borrowed(ref b) => fmt::Display::fmt(b, f),
279 Owned(ref o) => fmt::Display::fmt(o, f),
284 #[stable(feature = "rust1", since = "1.0.0")]
286 impl<'a, B: ?Sized, S: Hasher> Hash<S> for Cow<'a, B> where B: Hash<S> + ToOwned
289 fn hash(&self, state: &mut S) {
290 Hash::hash(&**self, state)
293 #[stable(feature = "rust1", since = "1.0.0")]
295 impl<'a, B: ?Sized> Hash for Cow<'a, B> where B: Hash + ToOwned
298 fn hash<H: Hasher>(&self, state: &mut H) {
299 Hash::hash(&**self, state)
303 /// Trait for moving into a `Cow`
304 #[stable(feature = "rust1", since = "1.0.0")]
305 pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {
306 /// Moves `self` into `Cow`
307 #[stable(feature = "rust1", since = "1.0.0")]
308 fn into_cow(self) -> Cow<'a, B>;
311 #[stable(feature = "rust1", since = "1.0.0")]
312 impl<'a, B: ?Sized> IntoCow<'a, B> for Cow<'a, B> where B: ToOwned {
313 fn into_cow(self) -> Cow<'a, B> {