diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2015-08-19 16:03:59 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2015-08-22 13:58:39 +0200 |
| commit | c408b7863389aa2bdb253ffa363e693bcd02439f (patch) | |
| tree | d24554daf2d34ade8df07a7a68c4a5ced316cf0b /src/libcollections/borrow.rs | |
| parent | 94ee3b5a54a9f4965b82f5e4eda512966e96ac63 (diff) | |
| download | rust-c408b7863389aa2bdb253ffa363e693bcd02439f.tar.gz rust-c408b7863389aa2bdb253ffa363e693bcd02439f.zip | |
Move the Borrow and BorrowMut traits to libcore.
Diffstat (limited to 'src/libcollections/borrow.rs')
| -rw-r--r-- | src/libcollections/borrow.rs | 111 |
1 files changed, 1 insertions, 110 deletions
diff --git a/src/libcollections/borrow.rs b/src/libcollections/borrow.rs index bfd06915250..bd1864b28cd 100644 --- a/src/libcollections/borrow.rs +++ b/src/libcollections/borrow.rs @@ -21,119 +21,10 @@ use core::ops::Deref; use core::option::Option; use fmt; -use alloc::{boxed, rc, arc}; use self::Cow::*; -/// A trait for borrowing data. -/// -/// In general, there may be several ways to "borrow" a piece of data. The -/// typical ways of borrowing a type `T` are `&T` (a shared borrow) and `&mut T` -/// (a mutable borrow). But types like `Vec<T>` provide additional kinds of -/// borrows: the borrowed slices `&[T]` and `&mut [T]`. -/// -/// When writing generic code, it is often desirable to abstract over all ways -/// of borrowing data from a given type. That is the role of the `Borrow` -/// trait: if `T: Borrow<U>`, then `&U` can be borrowed from `&T`. A given -/// type can be borrowed as multiple different types. In particular, `Vec<T>: -/// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`. -/// -/// If you are implementing `Borrow` and both `Self` and `Borrowed` implement -/// `Hash`, `Eq`, and/or `Ord`, they must produce the same result. -/// -/// `Borrow` is very similar to, but different than, `AsRef`. See -/// [the book][book] for more. -/// -/// [book]: ../../book/borrow-and-asref.html -#[stable(feature = "rust1", since = "1.0.0")] -pub trait Borrow<Borrowed: ?Sized> { - /// Immutably borrows from an owned value. - /// - /// # Examples - /// - /// ``` - /// use std::borrow::Borrow; - /// - /// fn check<T: Borrow<str>>(s: T) { - /// assert_eq!("Hello", s.borrow()); - /// } - /// - /// let s = "Hello".to_string(); - /// - /// check(s); - /// - /// let s = "Hello"; - /// - /// check(s); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn borrow(&self) -> &Borrowed; -} - -/// A trait for mutably borrowing data. -/// -/// Similar to `Borrow`, but for mutable borrows. -#[stable(feature = "rust1", since = "1.0.0")] -pub trait BorrowMut<Borrowed: ?Sized> : Borrow<Borrowed> { - /// Mutably borrows from an owned value. - /// - /// # Examples - /// - /// ``` - /// use std::borrow::BorrowMut; - /// - /// fn check<T: BorrowMut<[i32]>>(mut v: T) { - /// assert_eq!(&mut [1, 2, 3], v.borrow_mut()); - /// } - /// - /// let v = vec![1, 2, 3]; - /// - /// check(v); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn borrow_mut(&mut self) -> &mut Borrowed; -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> Borrow<T> for T { - fn borrow(&self) -> &T { self } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> BorrowMut<T> for T { - fn borrow_mut(&mut self) -> &mut T { self } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T: ?Sized> Borrow<T> for &'a T { - fn borrow(&self) -> &T { &**self } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T: ?Sized> Borrow<T> for &'a mut T { - fn borrow(&self) -> &T { &**self } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T: ?Sized> BorrowMut<T> for &'a mut T { - fn borrow_mut(&mut self) -> &mut T { &mut **self } -} - -impl<T: ?Sized> Borrow<T> for boxed::Box<T> { - fn borrow(&self) -> &T { &**self } -} - -impl<T: ?Sized> BorrowMut<T> for boxed::Box<T> { - fn borrow_mut(&mut self) -> &mut T { &mut **self } -} - -impl<T: ?Sized> Borrow<T> for rc::Rc<T> { - fn borrow(&self) -> &T { &**self } -} - -impl<T: ?Sized> Borrow<T> for arc::Arc<T> { - fn borrow(&self) -> &T { &**self } -} +pub use core::borrow::{Borrow, BorrowMut}; #[stable(feature = "rust1", since = "1.0.0")] impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B> where B: ToOwned, <B as ToOwned>::Owned: 'a { |
