diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-02-18 15:34:32 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-02-18 15:34:48 -0800 |
| commit | 2cdbd288ac0606902885434e1ccd8d8bde68913d (patch) | |
| tree | 9a7d37f7dd44424182797c76d49ba0ece99f9513 /src/libcore | |
| parent | 365bd9a9e3b9dafa98e26982353fd28a6ca1efef (diff) | |
| parent | a99e698628cbd396c8100ef776d10ac61d911847 (diff) | |
| download | rust-2cdbd288ac0606902885434e1ccd8d8bde68913d.tar.gz rust-2cdbd288ac0606902885434e1ccd8d8bde68913d.zip | |
rollup merge of #22210: aturon/stab-final-borrow
Conflicts: src/libcollections/btree/map.rs src/libcollections/str.rs src/libcollections/vec.rs src/libcore/borrow.rs src/libcore/hash/mod.rs src/libstd/collections/hash/map.rs src/libstd/collections/hash/set.rs
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/borrow.rs | 265 | ||||
| -rw-r--r-- | src/libcore/hash/mod.rs | 118 | ||||
| -rw-r--r-- | src/libcore/lib.rs | 1 |
3 files changed, 2 insertions, 382 deletions
diff --git a/src/libcore/borrow.rs b/src/libcore/borrow.rs deleted file mode 100644 index 3131952d94d..00000000000 --- a/src/libcore/borrow.rs +++ /dev/null @@ -1,265 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! A module for working with borrowed data. -//! -//! # The `BorrowFrom` traits -//! -//! 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 `BorrowFrom` -//! trait: if `T: BorrowFrom<U>`, then `&T` can be borrowed from `&U`. A given -//! type can be borrowed as multiple different types. In particular, `Vec<T>: -//! BorrowFrom<Vec<T>>` and `[T]: BorrowFrom<Vec<T>>`. -//! -//! # The `ToOwned` trait -//! -//! Some types make it possible to go from borrowed to owned, usually by -//! implementing the `Clone` trait. But `Clone` works only for going from `&T` -//! to `T`. The `ToOwned` trait generalizes `Clone` to construct owned data -//! from any borrow of a given type. -//! -//! # The `Cow` (clone-on-write) type -//! -//! The type `Cow` is a smart pointer providing clone-on-write functionality: it -//! can enclose and provide immutable access to borrowed data, and clone the -//! data lazily when mutation or ownership is required. The type is designed to -//! work with general borrowed data via the `BorrowFrom` trait. -//! -//! `Cow` implements both `Deref`, which means that you can call -//! non-mutating methods directly on the data it encloses. If mutation -//! is desired, `to_mut` will obtain a mutable references to an owned -//! value, cloning if necessary. - -#![unstable(feature = "core", - reason = "recently added as part of collections reform")] - -use clone::Clone; -use cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; -use fmt; -use marker::Sized; -use ops::Deref; -use option::Option; -use self::Cow::*; - -/// A trait for borrowing data. -#[old_orphan_check] -pub trait BorrowFrom<Owned: ?Sized> { - /// Immutably borrow from an owned value. - fn borrow_from(owned: &Owned) -> &Self; -} - -/// A trait for mutably borrowing data. -#[old_orphan_check] -pub trait BorrowFromMut<Owned: ?Sized> : BorrowFrom<Owned> { - /// Mutably borrow from an owned value. - fn borrow_from_mut(owned: &mut Owned) -> &mut Self; -} - -impl<T: ?Sized> BorrowFrom<T> for T { - fn borrow_from(owned: &T) -> &T { owned } -} - -impl<T: ?Sized> BorrowFromMut<T> for T { - fn borrow_from_mut(owned: &mut T) -> &mut T { owned } -} - -impl<'a, T: ?Sized> BorrowFrom<&'a T> for T { - fn borrow_from<'b>(owned: &'b &'a T) -> &'b T { &**owned } -} - -impl<'a, T: ?Sized> BorrowFrom<&'a mut T> for T { - fn borrow_from<'b>(owned: &'b &'a mut T) -> &'b T { &**owned } -} - -impl<'a, T: ?Sized> BorrowFromMut<&'a mut T> for T { - fn borrow_from_mut<'b>(owned: &'b mut &'a mut T) -> &'b mut T { &mut **owned } -} - -impl<'a, T, B: ?Sized> BorrowFrom<Cow<'a, T, B>> for B where B: ToOwned<T> { - fn borrow_from<'b>(owned: &'b Cow<'a, T, B>) -> &'b B { - &**owned - } -} - -/// Trait for moving into a `Cow` -#[old_orphan_check] -pub trait IntoCow<'a, T, B: ?Sized> { - /// Moves `self` into `Cow` - fn into_cow(self) -> Cow<'a, T, B>; -} - -impl<'a, T, B: ?Sized> IntoCow<'a, T, B> for Cow<'a, T, B> where B: ToOwned<T> { - fn into_cow(self) -> Cow<'a, T, B> { - self - } -} - -/// A generalization of Clone to borrowed data. -#[old_orphan_check] -pub trait ToOwned<Owned>: BorrowFrom<Owned> { - /// Create owned data from borrowed data, usually by copying. - fn to_owned(&self) -> Owned; -} - -impl<T> ToOwned<T> for T where T: Clone { - fn to_owned(&self) -> T { self.clone() } -} - -/// A clone-on-write smart pointer. -/// -/// # Example -/// -/// ```rust -/// use std::borrow::Cow; -/// -/// fn abs_all(input: &mut Cow<Vec<i32>, [i32]>) { -/// for i in 0..input.len() { -/// let v = input[i]; -/// if v < 0 { -/// // clones into a vector the first time (if not already owned) -/// input.to_mut()[i] = -v; -/// } -/// } -/// } -/// ``` -pub enum Cow<'a, T, B: ?Sized + 'a> where B: ToOwned<T> { - /// Borrowed data. - Borrowed(&'a B), - - /// Owned data. - Owned(T) -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, B: ?Sized> Clone for Cow<'a, T, B> where B: ToOwned<T> { - fn clone(&self) -> Cow<'a, T, B> { - match *self { - Borrowed(b) => Borrowed(b), - Owned(ref o) => { - let b: &B = BorrowFrom::borrow_from(o); - Owned(b.to_owned()) - }, - } - } -} - -impl<'a, T, B: ?Sized> Cow<'a, T, B> where B: ToOwned<T> { - /// Acquire a mutable reference to the owned form of the data. - /// - /// Copies the data if it is not already owned. - pub fn to_mut(&mut self) -> &mut T { - match *self { - Borrowed(borrowed) => { - *self = Owned(borrowed.to_owned()); - self.to_mut() - } - Owned(ref mut owned) => owned - } - } - - /// Extract the owned data. - /// - /// Copies the data if it is not already owned. - pub fn into_owned(self) -> T { - match self { - Borrowed(borrowed) => borrowed.to_owned(), - Owned(owned) => owned - } - } - - /// Returns true if this `Cow` wraps a borrowed value - pub fn is_borrowed(&self) -> bool { - match *self { - Borrowed(_) => true, - _ => false, - } - } - - /// Returns true if this `Cow` wraps an owned value - pub fn is_owned(&self) -> bool { - match *self { - Owned(_) => true, - _ => false, - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, B: ?Sized> Deref for Cow<'a, T, B> where B: ToOwned<T> { - type Target = B; - - fn deref(&self) -> &B { - match *self { - Borrowed(borrowed) => borrowed, - Owned(ref owned) => BorrowFrom::borrow_from(owned) - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, B: ?Sized> Eq for Cow<'a, T, B> where B: Eq + ToOwned<T> {} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, B: ?Sized> Ord for Cow<'a, T, B> where B: Ord + ToOwned<T> { - #[inline] - fn cmp(&self, other: &Cow<'a, T, B>) -> Ordering { - Ord::cmp(&**self, &**other) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, 'b, T, U, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, U, C>> for Cow<'a, T, B> where - B: PartialEq<C> + ToOwned<T>, - C: ToOwned<U>, -{ - #[inline] - fn eq(&self, other: &Cow<'b, U, C>) -> bool { - PartialEq::eq(&**self, &**other) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, B: ?Sized> PartialOrd for Cow<'a, T, B> where B: PartialOrd + ToOwned<T> { - #[inline] - fn partial_cmp(&self, other: &Cow<'a, T, B>) -> Option<Ordering> { - PartialOrd::partial_cmp(&**self, &**other) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, B: ?Sized> fmt::Debug for Cow<'a, T, B> where - B: fmt::Debug + ToOwned<T>, - T: fmt::Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Borrowed(ref b) => fmt::Debug::fmt(b, f), - Owned(ref o) => fmt::Debug::fmt(o, f), - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, B: ?Sized> fmt::Display for Cow<'a, T, B> where - B: fmt::Display + ToOwned<T>, - T: fmt::Display, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Borrowed(ref b) => fmt::Display::fmt(b, f), - Owned(ref o) => fmt::Display::fmt(o, f), - } - } -} diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index b9a5122dd9c..3d0c9761dda 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -58,8 +58,9 @@ #![stable(feature = "rust1", since = "1.0.0")] +use prelude::*; + use default::Default; -use marker::Sized; use mem; pub use self::sip::SipHasher; @@ -398,119 +399,4 @@ mod impls { } )*} } - - impl_write! { - (u8, write_u8), - (u16, write_u16), - (u32, write_u32), - (u64, write_u64), - (usize, write_usize), - (i8, write_i8), - (i16, write_i16), - (i32, write_i32), - (i64, write_i64), - (isize, write_isize), - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl Hash for bool { - fn hash<H: Hasher>(&self, state: &mut H) { - state.write_u8(*self as u8) - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl Hash for char { - fn hash<H: Hasher>(&self, state: &mut H) { - state.write_u32(*self as u32) - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl Hash for str { - fn hash<H: Hasher>(&self, state: &mut H) { - state.write(self.as_bytes()); - state.write_u8(0xff) - } - } - - macro_rules! impl_hash_tuple { - () => ( - #[stable(feature = "rust1", since = "1.0.0")] - impl Hash for () { - fn hash<H: Hasher>(&self, _state: &mut H) {} - } - ); - - ( $($name:ident)+) => ( - #[stable(feature = "rust1", since = "1.0.0")] - impl<$($name: Hash),*> Hash for ($($name,)*) { - #[allow(non_snake_case)] - fn hash<S: Hasher>(&self, state: &mut S) { - let ($(ref $name,)*) = *self; - $($name.hash(state);)* - } - } - ); - } - - impl_hash_tuple! {} - impl_hash_tuple! { A } - impl_hash_tuple! { A B } - impl_hash_tuple! { A B C } - impl_hash_tuple! { A B C D } - impl_hash_tuple! { A B C D E } - impl_hash_tuple! { A B C D E F } - impl_hash_tuple! { A B C D E F G } - impl_hash_tuple! { A B C D E F G H } - impl_hash_tuple! { A B C D E F G H I } - impl_hash_tuple! { A B C D E F G H I J } - impl_hash_tuple! { A B C D E F G H I J K } - impl_hash_tuple! { A B C D E F G H I J K L } - - #[stable(feature = "rust1", since = "1.0.0")] - impl<T: Hash> Hash for [T] { - fn hash<H: Hasher>(&self, state: &mut H) { - self.len().hash(state); - Hash::hash_slice(self, state) - } - } - - - #[stable(feature = "rust1", since = "1.0.0")] - impl<'a, T: ?Sized + Hash> Hash for &'a T { - fn hash<H: Hasher>(&self, state: &mut H) { - (**self).hash(state); - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl<'a, T: ?Sized + Hash> Hash for &'a mut T { - fn hash<H: Hasher>(&self, state: &mut H) { - (**self).hash(state); - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl<T> Hash for *const T { - fn hash<H: Hasher>(&self, state: &mut H) { - state.write_usize(*self as usize) - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl<T> Hash for *mut T { - fn hash<H: Hasher>(&self, state: &mut H) { - state.write_usize(*self as usize) - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl<'a, T, B: ?Sized> Hash for Cow<'a, T, B> - where B: Hash + ToOwned<T> - { - fn hash<H: Hasher>(&self, state: &mut H) { - Hash::hash(&**self, state) - } - } } diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index f0c60ffe4bf..3c58480ff0c 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -126,7 +126,6 @@ pub mod default; pub mod any; pub mod atomic; -pub mod borrow; pub mod cell; pub mod char; pub mod panicking; |
