mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-21 20:23:21 +00:00
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]
This commit is contained in:
parent
9bb3b3772d
commit
a99e698628
@ -73,7 +73,6 @@ use core::prelude::*;
|
||||
|
||||
use core::atomic;
|
||||
use core::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
|
||||
use core::borrow::BorrowFrom;
|
||||
use core::fmt;
|
||||
use core::cmp::{Ordering};
|
||||
use core::default::Default;
|
||||
@ -244,12 +243,6 @@ impl<T> Clone for Arc<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> BorrowFrom<Arc<T>> for T {
|
||||
fn borrow_from(owned: &Arc<T>) -> &T {
|
||||
&**owned
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> Deref for Arc<T> {
|
||||
type Target = T;
|
||||
|
@ -144,7 +144,6 @@
|
||||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
use core::borrow::BorrowFrom;
|
||||
use core::cell::Cell;
|
||||
use core::clone::Clone;
|
||||
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
|
||||
@ -349,12 +348,6 @@ impl<T: Clone> Rc<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> BorrowFrom<Rc<T>> for T {
|
||||
fn borrow_from(owned: &Rc<T>) -> &T {
|
||||
&**owned
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> Deref for Rc<T> {
|
||||
type Target = T;
|
||||
|
306
src/libcollections/borrow.rs
Normal file
306
src/libcollections/borrow.rs
Normal file
@ -0,0 +1,306 @@
|
||||
// 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.
|
||||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
use core::clone::Clone;
|
||||
use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
|
||||
use core::hash::{Hash, Hasher};
|
||||
use core::marker::Sized;
|
||||
use core::ops::Deref;
|
||||
use core::option::Option;
|
||||
|
||||
use fmt;
|
||||
use alloc::{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]>`.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub trait Borrow<Borrowed: ?Sized> {
|
||||
/// Immutably borrow from an owned value.
|
||||
#[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 borrow from an owned value.
|
||||
#[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> Borrow<T> for rc::Rc<T> {
|
||||
fn borrow(&self) -> &T { &**self }
|
||||
}
|
||||
|
||||
impl<T> Borrow<T> for arc::Arc<T> {
|
||||
fn borrow(&self) -> &T { &**self }
|
||||
}
|
||||
|
||||
#[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 {
|
||||
fn borrow(&self) -> &B {
|
||||
&**self
|
||||
}
|
||||
}
|
||||
|
||||
/// A generalization of Clone to borrowed data.
|
||||
///
|
||||
/// 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.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub trait ToOwned {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
type Owned: Borrow<Self>;
|
||||
|
||||
/// Create owned data from borrowed data, usually by copying.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn to_owned(&self) -> Self::Owned;
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> ToOwned for T where T: Clone {
|
||||
type Owned = T;
|
||||
fn to_owned(&self) -> T { self.clone() }
|
||||
}
|
||||
|
||||
/// A clone-on-write smart pointer.
|
||||
///
|
||||
/// 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 `Borrow` 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.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::borrow::Cow;
|
||||
///
|
||||
/// fn abs_all(input: &mut Cow<[int]>) {
|
||||
/// 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;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub enum Cow<'a, B: ?Sized + 'a> where B: ToOwned {
|
||||
/// Borrowed data.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
Borrowed(&'a B),
|
||||
|
||||
/// Owned data.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
Owned(<B as ToOwned>::Owned)
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> Clone for Cow<'a, B> where B: ToOwned {
|
||||
fn clone(&self) -> Cow<'a, B> {
|
||||
match *self {
|
||||
Borrowed(b) => Borrowed(b),
|
||||
Owned(ref o) => {
|
||||
let b: &B = o.borrow();
|
||||
Owned(b.to_owned())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, B: ?Sized> Cow<'a, B> where B: ToOwned {
|
||||
/// Acquire a mutable reference to the owned form of the data.
|
||||
///
|
||||
/// Copies the data if it is not already owned.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned {
|
||||
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.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn into_owned(self) -> <B as ToOwned>::Owned {
|
||||
match self {
|
||||
Borrowed(borrowed) => borrowed.to_owned(),
|
||||
Owned(owned) => owned
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if this `Cow` wraps a borrowed value
|
||||
#[deprecated(since = "1.0.0", reason = "match on the enum instead")]
|
||||
#[unstable(feature = "std_misc")]
|
||||
pub fn is_borrowed(&self) -> bool {
|
||||
match *self {
|
||||
Borrowed(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if this `Cow` wraps an owned value
|
||||
#[deprecated(since = "1.0.0", reason = "match on the enum instead")]
|
||||
#[unstable(feature = "std_misc")]
|
||||
pub fn is_owned(&self) -> bool {
|
||||
match *self {
|
||||
Owned(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> Deref for Cow<'a, B> where B: ToOwned {
|
||||
type Target = B;
|
||||
|
||||
fn deref(&self) -> &B {
|
||||
match *self {
|
||||
Borrowed(borrowed) => borrowed,
|
||||
Owned(ref owned) => owned.borrow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> Eq for Cow<'a, B> where B: Eq + ToOwned {}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> Ord for Cow<'a, B> where B: Ord + ToOwned {
|
||||
#[inline]
|
||||
fn cmp(&self, other: &Cow<'a, B>) -> Ordering {
|
||||
Ord::cmp(&**self, &**other)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, C>> for Cow<'a, B> where
|
||||
B: PartialEq<C> + ToOwned, C: ToOwned,
|
||||
{
|
||||
#[inline]
|
||||
fn eq(&self, other: &Cow<'b, C>) -> bool {
|
||||
PartialEq::eq(&**self, &**other)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> PartialOrd for Cow<'a, B> where B: PartialOrd + ToOwned,
|
||||
{
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &Cow<'a, B>) -> Option<Ordering> {
|
||||
PartialOrd::partial_cmp(&**self, &**other)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> fmt::Debug for Cow<'a, B> where
|
||||
B: fmt::Debug + ToOwned,
|
||||
<B as ToOwned>::Owned: 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, B: ?Sized> fmt::Display for Cow<'a, B> where
|
||||
B: fmt::Display + ToOwned,
|
||||
<B as ToOwned>::Owned: 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized, S: Hasher> Hash<S> for Cow<'a, B> where B: Hash<S> + ToOwned
|
||||
{
|
||||
#[inline]
|
||||
fn hash(&self, state: &mut S) {
|
||||
Hash::hash(&**self, state)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for moving into a `Cow`
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {
|
||||
/// Moves `self` into `Cow`
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn into_cow(self) -> Cow<'a, B>;
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> IntoCow<'a, B> for Cow<'a, B> where B: ToOwned {
|
||||
fn into_cow(self) -> Cow<'a, B> {
|
||||
self
|
||||
}
|
||||
}
|
313
src/libcollections/borrow_stage0.rs
Normal file
313
src/libcollections/borrow_stage0.rs
Normal file
@ -0,0 +1,313 @@
|
||||
// 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.
|
||||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
use core::clone::Clone;
|
||||
use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
|
||||
use core::hash::{Hash, Hasher};
|
||||
use core::marker::Sized;
|
||||
use core::ops::Deref;
|
||||
use core::option::Option;
|
||||
|
||||
use fmt;
|
||||
use alloc::{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]>`.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub trait Borrow<Borrowed: ?Sized> {
|
||||
/// Immutably borrow from an owned value.
|
||||
#[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 borrow from an owned value.
|
||||
#[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> Borrow<T> for rc::Rc<T> {
|
||||
fn borrow(&self) -> &T { &**self }
|
||||
}
|
||||
|
||||
impl<T> Borrow<T> for arc::Arc<T> {
|
||||
fn borrow(&self) -> &T { &**self }
|
||||
}
|
||||
|
||||
#[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 {
|
||||
fn borrow(&self) -> &B {
|
||||
&**self
|
||||
}
|
||||
}
|
||||
|
||||
/// A generalization of Clone to borrowed data.
|
||||
///
|
||||
/// 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.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub trait ToOwned {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
type Owned: Borrow<Self>;
|
||||
|
||||
/// Create owned data from borrowed data, usually by copying.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn to_owned(&self) -> Self::Owned;
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> ToOwned for T where T: Clone {
|
||||
type Owned = T;
|
||||
fn to_owned(&self) -> T { self.clone() }
|
||||
}
|
||||
|
||||
/// A clone-on-write smart pointer.
|
||||
///
|
||||
/// 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 `Borrow` 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.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::borrow::Cow;
|
||||
///
|
||||
/// fn abs_all(input: &mut Cow<[int]>) {
|
||||
/// 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;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub enum Cow<'a, B: ?Sized + 'a> where B: ToOwned {
|
||||
/// Borrowed data.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
Borrowed(&'a B),
|
||||
|
||||
/// Owned data.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
Owned(<B as ToOwned>::Owned)
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> Clone for Cow<'a, B> where B: ToOwned {
|
||||
fn clone(&self) -> Cow<'a, B> {
|
||||
match *self {
|
||||
Borrowed(b) => Borrowed(b),
|
||||
Owned(ref o) => {
|
||||
let b: &B = o.borrow();
|
||||
Owned(b.to_owned())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, B: ?Sized> Cow<'a, B> where B: ToOwned, <B as ToOwned>::Owned: 'a {
|
||||
/// Acquire a mutable reference to the owned form of the data.
|
||||
///
|
||||
/// Copies the data if it is not already owned.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned where <B as ToOwned>::Owned: 'a {
|
||||
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.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn into_owned(self) -> <B as ToOwned>::Owned {
|
||||
match self {
|
||||
Borrowed(borrowed) => borrowed.to_owned(),
|
||||
Owned(owned) => owned
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if this `Cow` wraps a borrowed value
|
||||
#[deprecated(since = "1.0.0", reason = "match on the enum instead")]
|
||||
#[unstable(feature = "std_misc")]
|
||||
pub fn is_borrowed(&self) -> bool {
|
||||
match *self {
|
||||
Borrowed(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if this `Cow` wraps an owned value
|
||||
#[deprecated(since = "1.0.0", reason = "match on the enum instead")]
|
||||
#[unstable(feature = "std_misc")]
|
||||
pub fn is_owned(&self) -> bool {
|
||||
match *self {
|
||||
Owned(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> Deref for Cow<'a, B> where
|
||||
B: ToOwned, <B as ToOwned>::Owned: 'a
|
||||
{
|
||||
type Target = B;
|
||||
|
||||
fn deref(&self) -> &B {
|
||||
match *self {
|
||||
Borrowed(borrowed) => borrowed,
|
||||
Owned(ref owned) => owned.borrow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> Eq for Cow<'a, B> where B: Eq + ToOwned, <B as ToOwned>::Owned: 'a {}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> Ord for Cow<'a, B> where
|
||||
B: Ord + ToOwned, <B as ToOwned>::Owned: 'a
|
||||
{
|
||||
#[inline]
|
||||
fn cmp(&self, other: &Cow<'a, B>) -> Ordering {
|
||||
Ord::cmp(&**self, &**other)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, C>> for Cow<'a, B> where
|
||||
B: PartialEq<C> + ToOwned, C: ToOwned,
|
||||
<B as ToOwned>::Owned: 'a, <C as ToOwned>::Owned: 'b,
|
||||
{
|
||||
#[inline]
|
||||
fn eq(&self, other: &Cow<'b, C>) -> bool {
|
||||
PartialEq::eq(&**self, &**other)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> PartialOrd for Cow<'a, B> where
|
||||
B: PartialOrd + ToOwned, <B as ToOwned>::Owned: 'a
|
||||
{
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &Cow<'a, B>) -> Option<Ordering> {
|
||||
PartialOrd::partial_cmp(&**self, &**other)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> fmt::Debug for Cow<'a, B> where
|
||||
B: fmt::Debug + ToOwned,
|
||||
<B as ToOwned>::Owned: 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, B: ?Sized> fmt::Display for Cow<'a, B> where
|
||||
B: fmt::Display + ToOwned,
|
||||
<B as ToOwned>::Owned: 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized, S: Hasher> Hash<S> for Cow<'a, B> where
|
||||
B: Hash<S> + ToOwned, <B as ToOwned>::Owned: 'a
|
||||
{
|
||||
#[inline]
|
||||
fn hash(&self, state: &mut S) {
|
||||
Hash::hash(&**self, state)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for moving into a `Cow`
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {
|
||||
/// Moves `self` into `Cow`
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn into_cow(self) -> Cow<'a, B>;
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, B: ?Sized> IntoCow<'a, B> for Cow<'a, B> where B: ToOwned {
|
||||
fn into_cow(self) -> Cow<'a, B> {
|
||||
self
|
||||
}
|
||||
}
|
@ -19,7 +19,6 @@ use self::Entry::*;
|
||||
|
||||
use core::prelude::*;
|
||||
|
||||
use core::borrow::BorrowFrom;
|
||||
use core::cmp::Ordering;
|
||||
use core::default::Default;
|
||||
use core::fmt::Debug;
|
||||
@ -29,6 +28,7 @@ use core::ops::{Index, IndexMut};
|
||||
use core::{iter, fmt, mem};
|
||||
use Bound::{self, Included, Excluded, Unbounded};
|
||||
|
||||
use borrow::Borrow;
|
||||
use ring_buf::RingBuf;
|
||||
|
||||
use self::Continuation::{Continue, Finished};
|
||||
@ -208,7 +208,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// assert_eq!(map.get(&2), None);
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where Q: BorrowFrom<K> + Ord {
|
||||
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where K: Borrow<Q>, Q: Ord {
|
||||
let mut cur_node = &self.root;
|
||||
loop {
|
||||
match Node::search(cur_node, key) {
|
||||
@ -240,7 +240,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// assert_eq!(map.contains_key(&2), false);
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where Q: BorrowFrom<K> + Ord {
|
||||
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where K: Borrow<Q>, Q: Ord {
|
||||
self.get(key).is_some()
|
||||
}
|
||||
|
||||
@ -264,7 +264,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// ```
|
||||
// See `get` for implementation notes, this is basically a copy-paste with mut's added
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where Q: BorrowFrom<K> + Ord {
|
||||
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Ord {
|
||||
// temp_node is a Borrowck hack for having a mutable value outlive a loop iteration
|
||||
let mut temp_node = &mut self.root;
|
||||
loop {
|
||||
@ -434,7 +434,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// assert_eq!(map.remove(&1), None);
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where Q: BorrowFrom<K> + Ord {
|
||||
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where K: Borrow<Q>, Q: Ord {
|
||||
// See `swap` for a more thorough description of the stuff going on in here
|
||||
let mut stack = stack::PartialSearchStack::new(self);
|
||||
loop {
|
||||
@ -903,7 +903,7 @@ impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<K: Ord, Q: ?Sized, V> Index<Q> for BTreeMap<K, V>
|
||||
where Q: BorrowFrom<K> + Ord
|
||||
where K: Borrow<Q>, Q: Ord
|
||||
{
|
||||
type Output = V;
|
||||
|
||||
@ -914,7 +914,7 @@ impl<K: Ord, Q: ?Sized, V> Index<Q> for BTreeMap<K, V>
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<K: Ord, Q: ?Sized, V> IndexMut<Q> for BTreeMap<K, V>
|
||||
where Q: BorrowFrom<K> + Ord
|
||||
where K: Borrow<Q>, Q: Ord
|
||||
{
|
||||
fn index_mut(&mut self, key: &Q) -> &mut V {
|
||||
self.get_mut(key).expect("no entry found for key")
|
||||
|
@ -18,7 +18,6 @@ pub use self::TraversalItem::*;
|
||||
|
||||
use core::prelude::*;
|
||||
|
||||
use core::borrow::BorrowFrom;
|
||||
use core::cmp::Ordering::{Greater, Less, Equal};
|
||||
use core::iter::Zip;
|
||||
use core::ops::{Deref, DerefMut, Index, IndexMut};
|
||||
@ -26,6 +25,8 @@ use core::ptr::Unique;
|
||||
use core::{slice, mem, ptr, cmp, num, raw};
|
||||
use alloc::heap;
|
||||
|
||||
use borrow::Borrow;
|
||||
|
||||
/// Represents the result of an Insertion: either the item fit, or the node had to split
|
||||
pub enum InsertionResult<K, V> {
|
||||
/// The inserted element fit
|
||||
@ -543,7 +544,7 @@ impl<K: Ord, V> Node<K, V> {
|
||||
/// `Found` will be yielded with the matching index. If it doesn't find an exact match,
|
||||
/// `GoDown` will be yielded with the index of the subtree the key must lie in.
|
||||
pub fn search<Q: ?Sized, NodeRef: Deref<Target=Node<K, V>>>(node: NodeRef, key: &Q)
|
||||
-> SearchResult<NodeRef> where Q: BorrowFrom<K> + Ord {
|
||||
-> SearchResult<NodeRef> where K: Borrow<Q>, Q: Ord {
|
||||
// FIXME(Gankro): Tune when to search linear or binary based on B (and maybe K/V).
|
||||
// For the B configured as of this writing (B = 6), binary search was *significantly*
|
||||
// worse for usizes.
|
||||
@ -1491,9 +1492,9 @@ macro_rules! node_slice_impl {
|
||||
impl<'a, K: Ord + 'a, V: 'a> $NodeSlice<'a, K, V> {
|
||||
/// Performs linear search in a slice. Returns a tuple of (index, is_exact_match).
|
||||
fn search_linear<Q: ?Sized>(&self, key: &Q) -> (usize, bool)
|
||||
where Q: BorrowFrom<K> + Ord {
|
||||
where K: Borrow<Q>, Q: Ord {
|
||||
for (i, k) in self.keys.iter().enumerate() {
|
||||
match key.cmp(BorrowFrom::borrow_from(k)) {
|
||||
match key.cmp(k.borrow()) {
|
||||
Greater => {},
|
||||
Equal => return (i, true),
|
||||
Less => return (i, false),
|
||||
|
@ -13,7 +13,6 @@
|
||||
|
||||
use core::prelude::*;
|
||||
|
||||
use core::borrow::BorrowFrom;
|
||||
use core::cmp::Ordering::{self, Less, Greater, Equal};
|
||||
use core::default::Default;
|
||||
use core::fmt::Debug;
|
||||
@ -21,6 +20,7 @@ use core::fmt;
|
||||
use core::iter::{Peekable, Map, FromIterator, IntoIterator};
|
||||
use core::ops::{BitOr, BitAnd, BitXor, Sub};
|
||||
|
||||
use borrow::Borrow;
|
||||
use btree_map::{BTreeMap, Keys};
|
||||
use Bound;
|
||||
|
||||
@ -336,7 +336,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// assert_eq!(set.contains(&4), false);
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool where Q: BorrowFrom<T> + Ord {
|
||||
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool where T: Borrow<Q>, Q: Ord {
|
||||
self.map.contains_key(value)
|
||||
}
|
||||
|
||||
@ -466,7 +466,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// assert_eq!(set.remove(&2), false);
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool where Q: BorrowFrom<T> + Ord {
|
||||
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool where T: Borrow<Q>, Q: Ord {
|
||||
self.map.remove(value).is_some()
|
||||
}
|
||||
}
|
||||
|
@ -81,6 +81,13 @@ pub mod string;
|
||||
pub mod vec;
|
||||
pub mod vec_map;
|
||||
|
||||
#[cfg(stage0)]
|
||||
#[path = "borrow_stage0.rs"]
|
||||
pub mod borrow;
|
||||
|
||||
#[cfg(not(stage0))]
|
||||
pub mod borrow;
|
||||
|
||||
#[unstable(feature = "collections",
|
||||
reason = "RFC 509")]
|
||||
pub mod bitv {
|
||||
|
@ -88,7 +88,6 @@
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use core::borrow::{BorrowFrom, BorrowFromMut, ToOwned};
|
||||
use core::clone::Clone;
|
||||
use core::cmp::Ordering::{self, Greater, Less};
|
||||
use core::cmp::{self, Ord, PartialEq};
|
||||
@ -105,6 +104,7 @@ use core::result::Result;
|
||||
use core::slice as core_slice;
|
||||
use self::Direction::*;
|
||||
|
||||
use borrow::{Borrow, BorrowMut, ToOwned};
|
||||
use vec::Vec;
|
||||
|
||||
pub use core::slice::{Chunks, AsSlice, Windows};
|
||||
@ -1175,18 +1175,19 @@ impl ElementSwaps {
|
||||
// Standard trait implementations for slices
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[unstable(feature = "collections", reason = "trait is unstable")]
|
||||
impl<T> BorrowFrom<Vec<T>> for [T] {
|
||||
fn borrow_from(owned: &Vec<T>) -> &[T] { &owned[] }
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> Borrow<[T]> for Vec<T> {
|
||||
fn borrow(&self) -> &[T] { &self[] }
|
||||
}
|
||||
|
||||
#[unstable(feature = "collections", reason = "trait is unstable")]
|
||||
impl<T> BorrowFromMut<Vec<T>> for [T] {
|
||||
fn borrow_from_mut(owned: &mut Vec<T>) -> &mut [T] { &mut owned[] }
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> BorrowMut<[T]> for Vec<T> {
|
||||
fn borrow_mut(&mut self) -> &mut [T] { &mut self[] }
|
||||
}
|
||||
|
||||
#[unstable(feature = "collections", reason = "trait is unstable")]
|
||||
impl<T: Clone> ToOwned<Vec<T>> for [T] {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: Clone> ToOwned for [T] {
|
||||
type Owned = Vec<T>;
|
||||
fn to_owned(&self) -> Vec<T> { self.to_vec() }
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,6 @@
|
||||
use self::RecompositionState::*;
|
||||
use self::DecompositionType::*;
|
||||
|
||||
use core::borrow::{BorrowFrom, ToOwned};
|
||||
use core::char::CharExt;
|
||||
use core::clone::Clone;
|
||||
use core::iter::AdditiveIterator;
|
||||
@ -68,6 +67,7 @@ use core::slice::AsSlice;
|
||||
use core::str as core_str;
|
||||
use unicode::str::{UnicodeStr, Utf16Encoder};
|
||||
|
||||
use borrow::{Borrow, ToOwned};
|
||||
use ring_buf::RingBuf;
|
||||
use slice::SliceExt;
|
||||
use string::String;
|
||||
@ -386,13 +386,14 @@ macro_rules! utf8_acc_cont_byte {
|
||||
($ch:expr, $byte:expr) => (($ch << 6) | ($byte & 63u8) as u32)
|
||||
}
|
||||
|
||||
#[unstable(feature = "collections", reason = "trait is unstable")]
|
||||
impl BorrowFrom<String> for str {
|
||||
fn borrow_from(owned: &String) -> &str { &owned[] }
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl Borrow<str> for String {
|
||||
fn borrow(&self) -> &str { &self[] }
|
||||
}
|
||||
|
||||
#[unstable(feature = "collections", reason = "trait is unstable")]
|
||||
impl ToOwned<String> for str {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl ToOwned for str {
|
||||
type Owned = String;
|
||||
fn to_owned(&self) -> String {
|
||||
unsafe {
|
||||
String::from_utf8_unchecked(self.as_bytes().to_owned())
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
use core::prelude::*;
|
||||
|
||||
use core::borrow::{Cow, IntoCow};
|
||||
use core::default::Default;
|
||||
use core::error::Error;
|
||||
use core::fmt;
|
||||
@ -29,6 +28,7 @@ use core::raw::Slice as RawSlice;
|
||||
use unicode::str as unicode_str;
|
||||
use unicode::str::Utf16Item;
|
||||
|
||||
use borrow::{Cow, IntoCow};
|
||||
use str::{self, CharRange, FromStr, Utf8Error};
|
||||
use vec::{DerefVec, Vec, as_vec};
|
||||
|
||||
@ -142,7 +142,7 @@ impl String {
|
||||
/// assert_eq!(output.as_slice(), "Hello \u{FFFD}World");
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> CowString<'a> {
|
||||
pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> Cow<'a, str> {
|
||||
let mut i = 0;
|
||||
match str::from_utf8(v) {
|
||||
Ok(s) => return Cow::Borrowed(s),
|
||||
@ -780,10 +780,10 @@ macro_rules! impl_eq {
|
||||
}
|
||||
|
||||
impl_eq! { String, &'a str }
|
||||
impl_eq! { CowString<'a>, String }
|
||||
impl_eq! { Cow<'a, str>, String }
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, 'b> PartialEq<&'b str> for CowString<'a> {
|
||||
impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
#[inline]
|
||||
@ -791,11 +791,11 @@ impl<'a, 'b> PartialEq<&'b str> for CowString<'a> {
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, 'b> PartialEq<CowString<'a>> for &'b str {
|
||||
impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str {
|
||||
#[inline]
|
||||
fn eq(&self, other: &CowString<'a>) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
fn eq(&self, other: &Cow<'a, str>) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
#[inline]
|
||||
fn ne(&self, other: &CowString<'a>) -> bool { PartialEq::ne(&**self, &**other) }
|
||||
fn ne(&self, other: &Cow<'a, str>) -> bool { PartialEq::ne(&**self, &**other) }
|
||||
}
|
||||
|
||||
#[unstable(feature = "collections", reason = "waiting on Str stabilization")]
|
||||
@ -958,31 +958,34 @@ impl<T: fmt::Display + ?Sized> ToString for T {
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoCow<'static, String, str> for String {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl IntoCow<'static, str> for String {
|
||||
#[inline]
|
||||
fn into_cow(self) -> CowString<'static> {
|
||||
fn into_cow(self) -> Cow<'static, str> {
|
||||
Cow::Owned(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoCow<'a, String, str> for &'a str {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a> IntoCow<'a, str> for &'a str {
|
||||
#[inline]
|
||||
fn into_cow(self) -> CowString<'a> {
|
||||
fn into_cow(self) -> Cow<'a, str> {
|
||||
Cow::Borrowed(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// A clone-on-write string
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub type CowString<'a> = Cow<'a, String, str>;
|
||||
|
||||
impl<'a> Str for CowString<'a> {
|
||||
impl<'a> Str for Cow<'a, str> {
|
||||
#[inline]
|
||||
fn as_slice<'b>(&'b self) -> &'b str {
|
||||
&**self
|
||||
}
|
||||
}
|
||||
|
||||
/// A clone-on-write string
|
||||
#[deprecated(since = "1.0.0", reason = "use Cow<'a, str> instead")]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub type CowString<'a> = Cow<'a, str>;
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl fmt::Write for String {
|
||||
#[inline]
|
||||
|
@ -50,7 +50,6 @@ use core::prelude::*;
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::heap::{EMPTY, allocate, reallocate, deallocate};
|
||||
use core::borrow::{Cow, IntoCow};
|
||||
use core::cmp::max;
|
||||
use core::cmp::{Ordering};
|
||||
use core::default::Default;
|
||||
@ -68,6 +67,8 @@ use core::raw::Slice as RawSlice;
|
||||
use core::slice;
|
||||
use core::usize;
|
||||
|
||||
use borrow::{Cow, IntoCow};
|
||||
|
||||
/// A growable list type, written `Vec<T>` but pronounced 'vector.'
|
||||
///
|
||||
/// # Examples
|
||||
@ -1517,34 +1518,34 @@ macro_rules! impl_eq {
|
||||
impl_eq! { Vec<A>, &'b [B] }
|
||||
impl_eq! { Vec<A>, &'b mut [B] }
|
||||
|
||||
impl<'a, A, B> PartialEq<Vec<B>> for CowVec<'a, A> where A: PartialEq<B> + Clone {
|
||||
impl<'a, A, B> PartialEq<Vec<B>> for Cow<'a, [A]> where A: PartialEq<B> + Clone {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
#[inline]
|
||||
fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
|
||||
}
|
||||
|
||||
impl<'a, A, B> PartialEq<CowVec<'a, A>> for Vec<B> where A: Clone, B: PartialEq<A> {
|
||||
impl<'a, A, B> PartialEq<Cow<'a, [A]>> for Vec<B> where A: Clone, B: PartialEq<A> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
fn eq(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
#[inline]
|
||||
fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) }
|
||||
fn ne(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::ne(&**self, &**other) }
|
||||
}
|
||||
|
||||
macro_rules! impl_eq_for_cowvec {
|
||||
($rhs:ty) => {
|
||||
impl<'a, 'b, A, B> PartialEq<$rhs> for CowVec<'a, A> where A: PartialEq<B> + Clone {
|
||||
impl<'a, 'b, A, B> PartialEq<$rhs> for Cow<'a, [A]> where A: PartialEq<B> + Clone {
|
||||
#[inline]
|
||||
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
#[inline]
|
||||
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
|
||||
}
|
||||
|
||||
impl<'a, 'b, A, B> PartialEq<CowVec<'a, A>> for $rhs where A: Clone, B: PartialEq<A> {
|
||||
impl<'a, 'b, A, B> PartialEq<Cow<'a, [A]>> for $rhs where A: Clone, B: PartialEq<A> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
fn eq(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::eq(&**self, &**other) }
|
||||
#[inline]
|
||||
fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) }
|
||||
fn ne(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::ne(&**self, &**other) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1552,8 +1553,7 @@ macro_rules! impl_eq_for_cowvec {
|
||||
impl_eq_for_cowvec! { &'b [B] }
|
||||
impl_eq_for_cowvec! { &'b mut [B] }
|
||||
|
||||
#[unstable(feature = "collections",
|
||||
reason = "waiting on PartialOrd stability")]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: PartialOrd> PartialOrd for Vec<T> {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
|
||||
@ -1561,10 +1561,10 @@ impl<T: PartialOrd> PartialOrd for Vec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "collections", reason = "waiting on Eq stability")]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: Eq> Eq for Vec<T> {}
|
||||
|
||||
#[unstable(feature = "collections", reason = "waiting on Ord stability")]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: Ord> Ord for Vec<T> {
|
||||
#[inline]
|
||||
fn cmp(&self, other: &Vec<T>) -> Ordering {
|
||||
@ -1643,26 +1643,26 @@ impl<T: fmt::Debug> fmt::Debug for Vec<T> {
|
||||
// Clone-on-write
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[unstable(feature = "collections",
|
||||
reason = "unclear how valuable this alias is")]
|
||||
/// A clone-on-write vector
|
||||
pub type CowVec<'a, T> = Cow<'a, Vec<T>, [T]>;
|
||||
#[deprecated(since = "1.0.0", reason = "use Cow<'a, [T]> instead")]
|
||||
#[unstable(feature = "collections")]
|
||||
pub type CowVec<'a, T> = Cow<'a, [T]>;
|
||||
|
||||
#[unstable(feature = "collections")]
|
||||
impl<'a, T> FromIterator<T> for CowVec<'a, T> where T: Clone {
|
||||
impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
|
||||
fn from_iter<I: Iterator<Item=T>>(it: I) -> CowVec<'a, T> {
|
||||
Cow::Owned(FromIterator::from_iter(it))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'a> IntoCow<'a, Vec<T>, [T]> for Vec<T> where T: Clone {
|
||||
fn into_cow(self) -> CowVec<'a, T> {
|
||||
impl<'a, T: 'a> IntoCow<'a, [T]> for Vec<T> where T: Clone {
|
||||
fn into_cow(self) -> Cow<'a, [T]> {
|
||||
Cow::Owned(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> IntoCow<'a, Vec<T>, [T]> for &'a [T] where T: Clone {
|
||||
fn into_cow(self) -> CowVec<'a, T> {
|
||||
impl<'a, T> IntoCow<'a, [T]> for &'a [T] where T: Clone {
|
||||
fn into_cow(self) -> Cow<'a, [T]> {
|
||||
Cow::Borrowed(self)
|
||||
}
|
||||
}
|
||||
|
@ -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<int>, [int]>) {
|
||||
/// 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),
|
||||
}
|
||||
}
|
||||
}
|
@ -61,7 +61,6 @@
|
||||
|
||||
use prelude::*;
|
||||
|
||||
use borrow::{Cow, ToOwned};
|
||||
use default::Default;
|
||||
use mem;
|
||||
use num::Int;
|
||||
@ -243,12 +242,3 @@ impl<S: Writer + Hasher, T> Hash<S> for *mut T {
|
||||
(*self as uint).hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, B: ?Sized, S: Hasher> Hash<S> for Cow<'a, T, B>
|
||||
where B: Hash<S> + ToOwned<T>
|
||||
{
|
||||
#[inline]
|
||||
fn hash(&self, state: &mut S) {
|
||||
Hash::hash(&**self, state)
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -358,7 +358,7 @@ impl<'a> Id<'a> {
|
||||
///
|
||||
/// Passing an invalid string (containing spaces, brackets,
|
||||
/// quotes, ...) will return an empty `Err` value.
|
||||
pub fn new<Name: IntoCow<'a, String, str>>(name: Name) -> Result<Id<'a>, ()> {
|
||||
pub fn new<Name: IntoCow<'a, str>>(name: Name) -> Result<Id<'a>, ()> {
|
||||
let name = name.into_cow();
|
||||
{
|
||||
let mut chars = name.chars();
|
||||
@ -427,11 +427,11 @@ pub trait Labeller<'a,N,E> {
|
||||
}
|
||||
|
||||
impl<'a> LabelText<'a> {
|
||||
pub fn label<S:IntoCow<'a, String, str>>(s: S) -> LabelText<'a> {
|
||||
pub fn label<S:IntoCow<'a, str>>(s: S) -> LabelText<'a> {
|
||||
LabelStr(s.into_cow())
|
||||
}
|
||||
|
||||
pub fn escaped<S:IntoCow<'a, String, str>>(s: S) -> LabelText<'a> {
|
||||
pub fn escaped<S:IntoCow<'a, str>>(s: S) -> LabelText<'a> {
|
||||
EscStr(s.into_cow())
|
||||
}
|
||||
|
||||
|
@ -68,7 +68,7 @@ use util::nodemap::{NodeMap, NodeSet, DefIdMap, DefIdSet};
|
||||
use util::nodemap::{FnvHashMap};
|
||||
|
||||
use arena::TypedArena;
|
||||
use std::borrow::{BorrowFrom, Cow};
|
||||
use std::borrow::{Borrow, Cow};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::cmp;
|
||||
use std::fmt;
|
||||
@ -986,9 +986,9 @@ impl<'tcx, S: Writer + Hasher> Hash<S> for InternedTy<'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> BorrowFrom<InternedTy<'tcx>> for sty<'tcx> {
|
||||
fn borrow_from<'a>(ty: &'a InternedTy<'tcx>) -> &'a sty<'tcx> {
|
||||
&ty.ty.sty
|
||||
impl<'tcx> Borrow<sty<'tcx>> for InternedTy<'tcx> {
|
||||
fn borrow<'a>(&'a self) -> &'a sty<'tcx> {
|
||||
&self.ty.sty
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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>
|
||||
{
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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() }
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
use ast::Name;
|
||||
|
||||
use std::borrow::BorrowFrom;
|
||||
use std::borrow::Borrow;
|
||||
use std::cell::RefCell;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
@ -79,7 +79,7 @@ impl<T: Eq + Hash<Hasher> + Clone + 'static> Interner<T> {
|
||||
}
|
||||
|
||||
pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
|
||||
where Q: BorrowFrom<T> + Eq + Hash<Hasher> {
|
||||
where T: Borrow<Q>, Q: Eq + Hash<Hasher> {
|
||||
let map = self.map.borrow();
|
||||
match (*map).get(val) {
|
||||
Some(v) => Some(*v),
|
||||
@ -128,9 +128,9 @@ impl fmt::Display for RcStr {
|
||||
}
|
||||
}
|
||||
|
||||
impl BorrowFrom<RcStr> for str {
|
||||
fn borrow_from(owned: &RcStr) -> &str {
|
||||
&owned.string[]
|
||||
impl Borrow<str> for RcStr {
|
||||
fn borrow(&self) -> &str {
|
||||
&self.string[]
|
||||
}
|
||||
}
|
||||
|
||||
@ -211,7 +211,7 @@ impl StrInterner {
|
||||
}
|
||||
|
||||
pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
|
||||
where Q: BorrowFrom<RcStr> + Eq + Hash<Hasher> {
|
||||
where RcStr: Borrow<Q>, Q: Eq + Hash<Hasher> {
|
||||
match (*self.map.borrow()).get(val) {
|
||||
Some(v) => Some(*v),
|
||||
None => None,
|
||||
|
@ -12,6 +12,5 @@ use std::borrow::IntoCow;
|
||||
|
||||
fn main() {
|
||||
<String as IntoCow>::into_cow("foo".to_string());
|
||||
//~^ ERROR wrong number of type arguments: expected 2, found 0
|
||||
//~^ ERROR wrong number of type arguments: expected 1, found 0
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ use std::mem::size_of;
|
||||
static uni: &'static str = "Les Miséééééééérables";
|
||||
static yy: usize = 25us;
|
||||
|
||||
static bob: Option<std::vec::CowVec<'static, isize>> = None;
|
||||
static bob: Option<std::borrow::Cow<'static, [isize]>> = None;
|
||||
|
||||
// buglink test - see issue #1337.
|
||||
|
||||
|
@ -100,8 +100,8 @@ tests! {
|
||||
Add::add, fn(i32, i32) -> i32, (5, 6);
|
||||
<i32 as Add<_>>::add, fn(i32, i32) -> i32, (5, 6);
|
||||
<i32 as Add<i32>>::add, fn(i32, i32) -> i32, (5, 6);
|
||||
<String as IntoCow<_, _>>::into_cow, fn(String) -> Cow<'static, String, str>,
|
||||
<String as IntoCow<_>>::into_cow, fn(String) -> Cow<'static, str>,
|
||||
("foo".to_string());
|
||||
<String as IntoCow<'static, _, _>>::into_cow, fn(String) -> Cow<'static, String, str>,
|
||||
<String as IntoCow<'static, _>>::into_cow, fn(String) -> Cow<'static, str>,
|
||||
("foo".to_string());
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ extern crate collections;
|
||||
use std::collections::HashMap;
|
||||
use std::borrow::{Cow, IntoCow};
|
||||
|
||||
type SendStr = Cow<'static, String, str>;
|
||||
type SendStr = Cow<'static, str>;
|
||||
|
||||
pub fn main() {
|
||||
let mut map: HashMap<SendStr, uint> = HashMap::new();
|
||||
|
@ -13,7 +13,7 @@ extern crate collections;
|
||||
use self::collections::BTreeMap;
|
||||
use std::borrow::{Cow, IntoCow};
|
||||
|
||||
type SendStr = Cow<'static, String, str>;
|
||||
type SendStr = Cow<'static, str>;
|
||||
|
||||
pub fn main() {
|
||||
let mut map: BTreeMap<SendStr, uint> = BTreeMap::new();
|
||||
|
@ -23,18 +23,18 @@ pub type Node<'a> = &'a CFGNode;
|
||||
|
||||
pub trait GraphWalk<'c, N> {
|
||||
/// Returns all the nodes in this graph.
|
||||
fn nodes(&'c self) where [N]:ToOwned<Vec<N>>;
|
||||
fn nodes(&'c self) where [N]:ToOwned<Owned=Vec<N>>;
|
||||
}
|
||||
|
||||
impl<'g> GraphWalk<'g, Node<'g>> for u32
|
||||
{
|
||||
fn nodes(&'g self) where [Node<'g>]:ToOwned<Vec<Node<'g>>>
|
||||
fn nodes(&'g self) where [Node<'g>]:ToOwned<Owned=Vec<Node<'g>>>
|
||||
{ loop { } }
|
||||
}
|
||||
|
||||
impl<'h> GraphWalk<'h, Node<'h>> for u64
|
||||
{
|
||||
fn nodes(&'h self) where [Node<'h>]:ToOwned<Vec<Node<'h>>>
|
||||
fn nodes(&'h self) where [Node<'h>]:ToOwned<Owned=Vec<Node<'h>>>
|
||||
{ loop { } }
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user