diff options
| author | Aaron Turon <aturon@mozilla.com> | 2015-02-11 23:16:32 -0800 |
|---|---|---|
| committer | Aaron Turon <aturon@mozilla.com> | 2015-02-18 15:23:58 -0800 |
| commit | a99e698628cbd396c8100ef776d10ac61d911847 (patch) | |
| tree | 889679e9690bbb6303f75abfb9bdd0ec4beedfcc /src/libstd | |
| parent | 9bb3b3772d4be69b0f619bd9456255a9e3bc7d9e (diff) | |
| download | rust-a99e698628cbd396c8100ef776d10ac61d911847.tar.gz rust-a99e698628cbd396c8100ef776d10ac61d911847.zip | |
Stabilize std::borrow
This commit stabilizes `std::borrow`, making the following modifications to catch up the API with language changes: * It renames `BorrowFrom` to `Borrow`, as was originally intended (but blocked for technical reasons), and reorders the parameters accordingly. * It moves the type parameter of `ToOwned` to an associated type. This is somewhat less flexible, in that each borrowed type must have a unique owned type, but leads to a significant simplification for `Cow`. Flexibility can be regained by using newtyped slices, which is advisable for other reasons anyway. * It removes the owned type parameter from `Cow`, making the type much less verbose. * Deprecates the `is_owned` and `is_borrowed` predicates in favor of direct matching. The above API changes are relatively minor; the basic functionality remains the same, and essentially the whole module is now marked `#[stable]`. [breaking-change]
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 26 | ||||
| -rw-r--r-- | src/libstd/collections/hash/set.rs | 6 | ||||
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 9 | ||||
| -rw-r--r-- | src/libstd/lib.rs | 2 | ||||
| -rwxr-xr-x | src/libstd/path.rs | 18 |
5 files changed, 33 insertions, 28 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 1b9f8b99017..9d3ffb114c1 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -14,7 +14,7 @@ use self::Entry::*; use self::SearchResult::*; use self::VacantEntryState::*; -use borrow::BorrowFrom; +use borrow::Borrow; use clone::Clone; use cmp::{max, Eq, PartialEq}; use default::Default; @@ -453,18 +453,18 @@ impl<K, V, S, H> HashMap<K, V, S> /// If you already have the hash for the key lying around, use /// search_hashed. fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> Option<FullBucketImm<'a, K, V>> - where Q: BorrowFrom<K> + Eq + Hash<H> + where K: Borrow<Q>, Q: Eq + Hash<H> { let hash = self.make_hash(q); - search_hashed(&self.table, hash, |k| q.eq(BorrowFrom::borrow_from(k))) + search_hashed(&self.table, hash, |k| q.eq(k.borrow())) .into_option() } fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> Option<FullBucketMut<'a, K, V>> - where Q: BorrowFrom<K> + Eq + Hash<H> + where K: Borrow<Q>, Q: Eq + Hash<H> { let hash = self.make_hash(q); - search_hashed(&mut self.table, hash, |k| q.eq(BorrowFrom::borrow_from(k))) + search_hashed(&mut self.table, hash, |k| q.eq(k.borrow())) .into_option() } @@ -1037,7 +1037,7 @@ impl<K, V, S, H> HashMap<K, V, S> /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> - where Q: Hash<H> + Eq + BorrowFrom<K> + where K: Borrow<Q>, Q: Hash<H> + Eq { self.search(k).map(|bucket| bucket.into_refs().1) } @@ -1060,7 +1060,7 @@ impl<K, V, S, H> HashMap<K, V, S> /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool - where Q: Hash<H> + Eq + BorrowFrom<K> + where K: Borrow<Q>, Q: Hash<H> + Eq { self.search(k).is_some() } @@ -1086,7 +1086,7 @@ impl<K, V, S, H> HashMap<K, V, S> /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> - where Q: Hash<H> + Eq + BorrowFrom<K> + where K: Borrow<Q>, Q: Hash<H> + Eq { self.search_mut(k).map(|bucket| bucket.into_mut_refs().1) } @@ -1138,7 +1138,7 @@ impl<K, V, S, H> HashMap<K, V, S> /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> - where Q: Hash<H> + Eq + BorrowFrom<K> + where K: Borrow<Q>, Q: Hash<H> + Eq { if self.table.size() == 0 { return None @@ -1247,8 +1247,8 @@ impl<K, V, S, H> Default for HashMap<K, V, S> #[stable(feature = "rust1", since = "1.0.0")] impl<K, Q: ?Sized, V, S, H> Index<Q> for HashMap<K, V, S> - where K: Eq + Hash<H>, - Q: Eq + Hash<H> + BorrowFrom<K>, + where K: Eq + Hash<H> + Borrow<Q>, + Q: Eq + Hash<H>, S: HashState<Hasher=H>, H: hash::Hasher<Output=u64> { @@ -1262,8 +1262,8 @@ impl<K, Q: ?Sized, V, S, H> Index<Q> for HashMap<K, V, S> #[stable(feature = "rust1", since = "1.0.0")] impl<K, V, S, H, Q: ?Sized> IndexMut<Q> for HashMap<K, V, S> - where K: Eq + Hash<H>, - Q: Eq + Hash<H> + BorrowFrom<K>, + where K: Eq + Hash<H> + Borrow<Q>, + Q: Eq + Hash<H>, S: HashState<Hasher=H>, H: hash::Hasher<Output=u64> { diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 5fbbcb3b347..7befaa8c368 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -10,7 +10,7 @@ // // ignore-lexer-test FIXME #15883 -use borrow::BorrowFrom; +use borrow::Borrow; use clone::Clone; use cmp::{Eq, PartialEq}; use core::marker::Sized; @@ -462,7 +462,7 @@ impl<T, S, H> HashSet<T, S> /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool - where Q: BorrowFrom<T> + Hash<H> + Eq + where T: Borrow<Q>, Q: Hash<H> + Eq { self.map.contains_key(value) } @@ -572,7 +572,7 @@ impl<T, S, H> HashSet<T, S> /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool - where Q: BorrowFrom<T> + Hash<H> + Eq + where T: Borrow<Q>, Q: Hash<H> + Eq { self.map.remove(value).is_some() } diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 1d14b141778..8c29b11cb99 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -34,7 +34,7 @@ use core::prelude::*; -use core::borrow::{BorrowFrom, ToOwned}; +use borrow::{Borrow, ToOwned}; use fmt::{self, Debug}; use mem; use string::{String, CowString}; @@ -266,11 +266,12 @@ impl Debug for OsStr { } } -impl BorrowFrom<OsString> for OsStr { - fn borrow_from(owned: &OsString) -> &OsStr { &owned[] } +impl Borrow<OsStr> for OsString { + fn borrow(&self) -> &OsStr { &self[] } } -impl ToOwned<OsString> for OsStr { +impl ToOwned for OsStr { + type Owned = OsString; fn to_owned(&self) -> OsString { self.to_os_string() } } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 7c9a8a7b4b5..fbd403ea593 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -161,7 +161,6 @@ extern crate libc; // NB: These reexports are in the order they should be listed in rustdoc pub use core::any; -pub use core::borrow; pub use core::cell; pub use core::clone; #[cfg(not(test))] pub use core::cmp; @@ -184,6 +183,7 @@ pub use core::error; #[cfg(not(test))] pub use alloc::boxed; pub use alloc::rc; +pub use core_collections::borrow; pub use core_collections::fmt; pub use core_collections::slice; pub use core_collections::str; diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 1d992668900..1f7129bf361 100755 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -108,12 +108,11 @@ use core::prelude::*; use ascii::*; -use borrow::BorrowFrom; +use borrow::{Borrow, ToOwned, Cow}; use cmp; use iter; use mem; use ops::{self, Deref}; -use string::CowString; use vec::Vec; use fmt; @@ -982,12 +981,17 @@ impl ops::Deref for PathBuf { } } -impl BorrowFrom<PathBuf> for Path { - fn borrow_from(owned: &PathBuf) -> &Path { - owned.deref() +impl Borrow<Path> for PathBuf { + fn borrow(&self) -> &Path { + self.deref() } } +impl ToOwned for Path { + type Owned = PathBuf; + fn to_owned(&self) -> PathBuf { self.to_path_buf() } +} + impl cmp::PartialEq for PathBuf { fn eq(&self, other: &PathBuf) -> bool { self.components() == other.components() @@ -1066,10 +1070,10 @@ impl Path { self.inner.to_str() } - /// Convert a `Path` to a `CowString`. + /// Convert a `Path` to a `Cow<str>`. /// /// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER. - pub fn to_string_lossy(&self) -> CowString { + pub fn to_string_lossy(&self) -> Cow<str> { self.inner.to_string_lossy() } |
