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")]
17 /// A trait for borrowing data.
19 /// In general, there may be several ways to "borrow" a piece of data. The
20 /// typical ways of borrowing a type `T` are `&T` (a shared borrow) and `&mut T`
21 /// (a mutable borrow). But types like `Vec<T>` provide additional kinds of
22 /// borrows: the borrowed slices `&[T]` and `&mut [T]`.
24 /// When writing generic code, it is often desirable to abstract over all ways
25 /// of borrowing data from a given type. That is the role of the `Borrow`
26 /// trait: if `T: Borrow<U>`, then `&U` can be borrowed from `&T`. A given
27 /// type can be borrowed as multiple different types. In particular, `Vec<T>:
28 /// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.
30 /// If you are implementing `Borrow` and both `Self` and `Borrowed` implement
31 /// `Hash`, `Eq`, and/or `Ord`, they must produce the same result.
33 /// `Borrow` is very similar to, but different than, `AsRef`. See
34 /// [the book][book] for more.
36 /// [book]: ../../book/borrow-and-asref.html
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub trait Borrow<Borrowed: ?Sized> {
39 /// Immutably borrows from an owned value.
44 /// use std::borrow::Borrow;
46 /// fn check<T: Borrow<str>>(s: T) {
47 /// assert_eq!("Hello", s.borrow());
50 /// let s = "Hello".to_string();
58 #[stable(feature = "rust1", since = "1.0.0")]
59 fn borrow(&self) -> &Borrowed;
62 /// A trait for mutably borrowing data.
64 /// Similar to `Borrow`, but for mutable borrows.
65 #[stable(feature = "rust1", since = "1.0.0")]
66 pub trait BorrowMut<Borrowed: ?Sized> : Borrow<Borrowed> {
67 /// Mutably borrows from an owned value.
72 /// use std::borrow::BorrowMut;
74 /// fn check<T: BorrowMut<[i32]>>(mut v: T) {
75 /// assert_eq!(&mut [1, 2, 3], v.borrow_mut());
78 /// let v = vec![1, 2, 3];
82 #[stable(feature = "rust1", since = "1.0.0")]
83 fn borrow_mut(&mut self) -> &mut Borrowed;
86 #[stable(feature = "rust1", since = "1.0.0")]
87 impl<T: ?Sized> Borrow<T> for T {
88 fn borrow(&self) -> &T { self }
91 #[stable(feature = "rust1", since = "1.0.0")]
92 impl<T: ?Sized> BorrowMut<T> for T {
93 fn borrow_mut(&mut self) -> &mut T { self }
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl<'a, T: ?Sized> Borrow<T> for &'a T {
98 fn borrow(&self) -> &T { &**self }
101 #[stable(feature = "rust1", since = "1.0.0")]
102 impl<'a, T: ?Sized> Borrow<T> for &'a mut T {
103 fn borrow(&self) -> &T { &**self }
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<'a, T: ?Sized> BorrowMut<T> for &'a mut T {
108 fn borrow_mut(&mut self) -> &mut T { &mut **self }