Skip to content

Commit c408b78

Browse files
committed
Move the Borrow and BorrowMut traits to libcore.
1 parent 94ee3b5 commit c408b78

File tree

6 files changed

+129
-110
lines changed

6 files changed

+129
-110
lines changed

src/liballoc/arc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ use boxed::Box;
7373

7474
use core::sync::atomic;
7575
use core::sync::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
76+
use core::borrow;
7677
use core::fmt;
7778
use core::cmp::Ordering;
7879
use core::mem::{align_of_val, size_of_val};
@@ -1109,3 +1110,7 @@ mod tests {
11091110
assert!(y.upgrade().is_none());
11101111
}
11111112
}
1113+
1114+
impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
1115+
fn borrow(&self) -> &T { &**self }
1116+
}

src/liballoc/boxed.rs

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use heap;
5757
use raw_vec::RawVec;
5858

5959
use core::any::Any;
60+
use core::borrow;
6061
use core::cmp::Ordering;
6162
use core::fmt;
6263
use core::hash::{self, Hash};
@@ -562,3 +563,10 @@ impl<T: Clone> Clone for Box<[T]> {
562563
}
563564
}
564565

566+
impl<T: ?Sized> borrow::Borrow<T> for Box<T> {
567+
fn borrow(&self) -> &T { &**self }
568+
}
569+
570+
impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> {
571+
fn borrow_mut(&mut self) -> &mut T { &mut **self }
572+
}

src/liballoc/rc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ use boxed::Box;
158158
#[cfg(test)]
159159
use std::boxed::Box;
160160

161+
use core::borrow;
161162
use core::cell::Cell;
162163
use core::cmp::Ordering;
163164
use core::fmt;
@@ -1091,3 +1092,7 @@ mod tests {
10911092
assert_eq!(foo, foo.clone());
10921093
}
10931094
}
1095+
1096+
impl<T: ?Sized> borrow::Borrow<T> for Rc<T> {
1097+
fn borrow(&self) -> &T { &**self }
1098+
}

src/libcollections/borrow.rs

+1-110
Original file line numberDiff line numberDiff line change
@@ -21,119 +21,10 @@ use core::ops::Deref;
2121
use core::option::Option;
2222

2323
use fmt;
24-
use alloc::{boxed, rc, arc};
2524

2625
use 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")]
13930
impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B> where B: ToOwned, <B as ToOwned>::Owned: 'a {

src/libcore/borrow.rs

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.
4+
//
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.
10+
11+
//! A module for working with borrowed data.
12+
13+
#![stable(feature = "rust1", since = "1.0.0")]
14+
15+
use marker::Sized;
16+
17+
/// A trait for borrowing data.
18+
///
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]`.
23+
///
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]>`.
29+
///
30+
/// If you are implementing `Borrow` and both `Self` and `Borrowed` implement
31+
/// `Hash`, `Eq`, and/or `Ord`, they must produce the same result.
32+
///
33+
/// `Borrow` is very similar to, but different than, `AsRef`. See
34+
/// [the book][book] for more.
35+
///
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.
40+
///
41+
/// # Examples
42+
///
43+
/// ```
44+
/// use std::borrow::Borrow;
45+
///
46+
/// fn check<T: Borrow<str>>(s: T) {
47+
/// assert_eq!("Hello", s.borrow());
48+
/// }
49+
///
50+
/// let s = "Hello".to_string();
51+
///
52+
/// check(s);
53+
///
54+
/// let s = "Hello";
55+
///
56+
/// check(s);
57+
/// ```
58+
#[stable(feature = "rust1", since = "1.0.0")]
59+
fn borrow(&self) -> &Borrowed;
60+
}
61+
62+
/// A trait for mutably borrowing data.
63+
///
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.
68+
///
69+
/// # Examples
70+
///
71+
/// ```
72+
/// use std::borrow::BorrowMut;
73+
///
74+
/// fn check<T: BorrowMut<[i32]>>(mut v: T) {
75+
/// assert_eq!(&mut [1, 2, 3], v.borrow_mut());
76+
/// }
77+
///
78+
/// let v = vec![1, 2, 3];
79+
///
80+
/// check(v);
81+
/// ```
82+
#[stable(feature = "rust1", since = "1.0.0")]
83+
fn borrow_mut(&mut self) -> &mut Borrowed;
84+
}
85+
86+
#[stable(feature = "rust1", since = "1.0.0")]
87+
impl<T: ?Sized> Borrow<T> for T {
88+
fn borrow(&self) -> &T { self }
89+
}
90+
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 }
94+
}
95+
96+
#[stable(feature = "rust1", since = "1.0.0")]
97+
impl<'a, T: ?Sized> Borrow<T> for &'a T {
98+
fn borrow(&self) -> &T { &**self }
99+
}
100+
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 }
104+
}
105+
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 }
109+
}

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub mod cmp;
139139
pub mod clone;
140140
pub mod default;
141141
pub mod convert;
142+
pub mod borrow;
142143

143144
/* Core types and methods on primitives */
144145

0 commit comments

Comments
 (0)