@@ -21,119 +21,10 @@ use core::ops::Deref;
2121use core:: option:: Option ;
2222
2323use fmt;
24- use alloc:: { boxed, rc, arc} ;
2524
2625use self :: Cow :: * ;
2726
28- /// A trait for borrowing data.
29- ///
30- /// In general, there may be several ways to "borrow" a piece of data. The
31- /// typical ways of borrowing a type `T` are `&T` (a shared borrow) and `&mut T`
32- /// (a mutable borrow). But types like `Vec<T>` provide additional kinds of
33- /// borrows: the borrowed slices `&[T]` and `&mut [T]`.
34- ///
35- /// When writing generic code, it is often desirable to abstract over all ways
36- /// of borrowing data from a given type. That is the role of the `Borrow`
37- /// trait: if `T: Borrow<U>`, then `&U` can be borrowed from `&T`. A given
38- /// type can be borrowed as multiple different types. In particular, `Vec<T>:
39- /// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.
40- ///
41- /// If you are implementing `Borrow` and both `Self` and `Borrowed` implement
42- /// `Hash`, `Eq`, and/or `Ord`, they must produce the same result.
43- ///
44- /// `Borrow` is very similar to, but different than, `AsRef`. See
45- /// [the book][book] for more.
46- ///
47- /// [book]: ../../book/borrow-and-asref.html
48- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
49- pub trait Borrow < Borrowed : ?Sized > {
50- /// Immutably borrows from an owned value.
51- ///
52- /// # Examples
53- ///
54- /// ```
55- /// use std::borrow::Borrow;
56- ///
57- /// fn check<T: Borrow<str>>(s: T) {
58- /// assert_eq!("Hello", s.borrow());
59- /// }
60- ///
61- /// let s = "Hello".to_string();
62- ///
63- /// check(s);
64- ///
65- /// let s = "Hello";
66- ///
67- /// check(s);
68- /// ```
69- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
70- fn borrow ( & self ) -> & Borrowed ;
71- }
72-
73- /// A trait for mutably borrowing data.
74- ///
75- /// Similar to `Borrow`, but for mutable borrows.
76- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
77- pub trait BorrowMut < Borrowed : ?Sized > : Borrow < Borrowed > {
78- /// Mutably borrows from an owned value.
79- ///
80- /// # Examples
81- ///
82- /// ```
83- /// use std::borrow::BorrowMut;
84- ///
85- /// fn check<T: BorrowMut<[i32]>>(mut v: T) {
86- /// assert_eq!(&mut [1, 2, 3], v.borrow_mut());
87- /// }
88- ///
89- /// let v = vec![1, 2, 3];
90- ///
91- /// check(v);
92- /// ```
93- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
94- fn borrow_mut ( & mut self ) -> & mut Borrowed ;
95- }
96-
97- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
98- impl < T : ?Sized > Borrow < T > for T {
99- fn borrow ( & self ) -> & T { self }
100- }
101-
102- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
103- impl < T : ?Sized > BorrowMut < T > for T {
104- fn borrow_mut ( & mut self ) -> & mut T { self }
105- }
106-
107- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
108- impl < ' a , T : ?Sized > Borrow < T > for & ' a T {
109- fn borrow ( & self ) -> & T { & * * self }
110- }
111-
112- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
113- impl < ' a , T : ?Sized > Borrow < T > for & ' a mut T {
114- fn borrow ( & self ) -> & T { & * * self }
115- }
116-
117- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
118- impl < ' a , T : ?Sized > BorrowMut < T > for & ' a mut T {
119- fn borrow_mut ( & mut self ) -> & mut T { & mut * * self }
120- }
121-
122- impl < T : ?Sized > Borrow < T > for boxed:: Box < T > {
123- fn borrow ( & self ) -> & T { & * * self }
124- }
125-
126- impl < T : ?Sized > BorrowMut < T > for boxed:: Box < T > {
127- fn borrow_mut ( & mut self ) -> & mut T { & mut * * self }
128- }
129-
130- impl < T : ?Sized > Borrow < T > for rc:: Rc < T > {
131- fn borrow ( & self ) -> & T { & * * self }
132- }
133-
134- impl < T : ?Sized > Borrow < T > for arc:: Arc < T > {
135- fn borrow ( & self ) -> & T { & * * self }
136- }
27+ pub use core:: borrow:: { Borrow , BorrowMut } ;
13728
13829#[ stable( feature = "rust1" , since = "1.0.0" ) ]
13930impl < ' a , B : ?Sized > Borrow < B > for Cow < ' a , B > where B : ToOwned , <B as ToOwned >:: Owned : ' a {
0 commit comments