2015-02-11 23:16:32 -08:00
|
|
|
//! A module for working with borrowed data.
|
|
|
|
|
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
2016-08-22 20:16:36 +00:00
|
|
|
use core::cmp::Ordering;
|
2015-02-11 23:16:32 -08:00
|
|
|
use core::hash::{Hash, Hasher};
|
2016-09-12 23:35:20 +02:00
|
|
|
use core::ops::{Add, AddAssign, Deref};
|
2015-02-11 23:16:32 -08:00
|
|
|
|
|
|
|
use fmt;
|
2016-11-04 01:07:00 +00:00
|
|
|
use string::String;
|
2015-02-11 23:16:32 -08:00
|
|
|
|
|
|
|
use self::Cow::*;
|
|
|
|
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-08-19 16:03:59 +02:00
|
|
|
pub use core::borrow::{Borrow, BorrowMut};
|
2015-02-11 23:16:32 -08:00
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B>
|
|
|
|
where B: ToOwned,
|
|
|
|
<B as ToOwned>::Owned: 'a
|
|
|
|
{
|
2015-02-11 23:16:32 -08:00
|
|
|
fn borrow(&self) -> &B {
|
|
|
|
&**self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 12:51:19 -04:00
|
|
|
/// A generalization of `Clone` to borrowed data.
|
2015-02-11 23:16:32 -08:00
|
|
|
///
|
|
|
|
/// 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>;
|
|
|
|
|
2015-04-09 12:51:19 -04:00
|
|
|
/// Creates owned data from borrowed data, usually by cloning.
|
2016-04-05 14:11:08 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
2017-02-14 23:07:51 -05:00
|
|
|
/// let s: &str = "a";
|
|
|
|
/// let ss: String = s.to_owned();
|
2016-04-05 14:11:08 +02:00
|
|
|
///
|
2017-02-14 23:07:51 -05:00
|
|
|
/// let v: &[i32] = &[1, 2];
|
|
|
|
/// let vv: Vec<i32> = v.to_owned();
|
2016-04-05 14:11:08 +02:00
|
|
|
/// ```
|
2015-02-11 23:16:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-30 23:06:05 -07:00
|
|
|
#[must_use = "cloning is often expensive and is not expected to have side effects"]
|
2015-02-11 23:16:32 -08:00
|
|
|
fn to_owned(&self) -> Self::Owned;
|
2017-04-01 19:33:45 -07:00
|
|
|
|
|
|
|
/// Uses borrowed data to replace owned data, usually by cloning.
|
|
|
|
///
|
|
|
|
/// This is borrow-generalized version of `Clone::clone_from`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #![feature(toowned_clone_into)]
|
|
|
|
/// let mut s: String = String::new();
|
|
|
|
/// "hello".clone_into(&mut s);
|
|
|
|
///
|
|
|
|
/// let mut v: Vec<i32> = Vec::new();
|
|
|
|
/// [1, 2][..].clone_into(&mut v);
|
|
|
|
/// ```
|
|
|
|
#[unstable(feature = "toowned_clone_into",
|
|
|
|
reason = "recently added",
|
|
|
|
issue = "41263")]
|
|
|
|
fn clone_into(&self, target: &mut Self::Owned) {
|
|
|
|
*target = self.to_owned();
|
|
|
|
}
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-12-20 09:54:00 +05:30
|
|
|
impl<T> ToOwned for T
|
|
|
|
where T: Clone
|
|
|
|
{
|
2015-02-11 23:16:32 -08:00
|
|
|
type Owned = T;
|
2015-11-24 11:23:48 +13:00
|
|
|
fn to_owned(&self) -> T {
|
|
|
|
self.clone()
|
|
|
|
}
|
2017-04-01 19:33:45 -07:00
|
|
|
|
|
|
|
fn clone_into(&self, target: &mut T) {
|
|
|
|
target.clone_from(self);
|
|
|
|
}
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
2015-04-09 12:51:19 -04:00
|
|
|
/// `Cow` implements `Deref`, which means that you can call
|
2015-02-11 23:16:32 -08:00
|
|
|
/// non-mutating methods directly on the data it encloses. If mutation
|
2015-04-09 12:51:19 -04:00
|
|
|
/// is desired, `to_mut` will obtain a mutable reference to an owned
|
2015-02-11 23:16:32 -08:00
|
|
|
/// value, cloning if necessary.
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-02-11 23:16:32 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-02-11 23:16:32 -08:00
|
|
|
/// use std::borrow::Cow;
|
|
|
|
///
|
2015-02-21 19:43:11 -05:00
|
|
|
/// fn abs_all(input: &mut Cow<[i32]>) {
|
2015-02-11 23:16:32 -08:00
|
|
|
/// for i in 0..input.len() {
|
|
|
|
/// let v = input[i];
|
|
|
|
/// if v < 0 {
|
2016-10-15 00:55:46 -04:00
|
|
|
/// // Clones into a vector if not already owned.
|
2015-02-11 23:16:32 -08:00
|
|
|
/// input.to_mut()[i] = -v;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
2016-10-15 00:55:46 -04:00
|
|
|
///
|
|
|
|
/// // No clone occurs because `input` doesn't need to be mutated.
|
|
|
|
/// let slice = [0, 1, 2];
|
|
|
|
/// let mut input = Cow::from(&slice[..]);
|
|
|
|
/// abs_all(&mut input);
|
|
|
|
///
|
|
|
|
/// // Clone occurs because `input` needs to be mutated.
|
|
|
|
/// let slice = [-1, 0, 1];
|
|
|
|
/// let mut input = Cow::from(&slice[..]);
|
|
|
|
/// abs_all(&mut input);
|
|
|
|
///
|
|
|
|
/// // No clone occurs because `input` is already owned.
|
|
|
|
/// let mut input = Cow::from(vec![-1, 0, 1]);
|
|
|
|
/// abs_all(&mut input);
|
2015-02-11 23:16:32 -08:00
|
|
|
/// ```
|
2018-08-06 17:33:32 +03:00
|
|
|
///
|
2018-08-15 14:00:59 +03:00
|
|
|
/// Another example showing how to keep `Cow` in a struct:
|
2018-08-15 22:06:35 +03:00
|
|
|
///
|
2018-08-06 17:33:32 +03:00
|
|
|
/// ```
|
|
|
|
/// use std::borrow::{Cow, ToOwned};
|
|
|
|
///
|
|
|
|
/// struct Items<'a, X: 'a> where [X]: ToOwned<Owned=Vec<X>> {
|
|
|
|
/// values: Cow<'a, [X]>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl<'a, X: Clone + 'a> Items<'a, X> where [X]: ToOwned<Owned=Vec<X>> {
|
|
|
|
/// fn new(v: Cow<'a, [X]>) -> Self {
|
|
|
|
/// Items { values: v }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Creates a container from borrowed values of a slice
|
|
|
|
/// let readonly = [1, 2];
|
|
|
|
/// let borrowed = Items::new((&readonly[..]).into());
|
|
|
|
/// match borrowed {
|
|
|
|
/// Items { values: Cow::Borrowed(b) } => println!("borrowed {:?}", b),
|
|
|
|
/// _ => panic!("expect borrowed value"),
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let mut clone_on_write = borrowed;
|
|
|
|
/// // Mutates the data from slice into owned vec and pushes a new value on top
|
|
|
|
/// clone_on_write.values.to_mut().push(3);
|
|
|
|
/// println!("clone_on_write = {:?}", clone_on_write.values);
|
|
|
|
///
|
|
|
|
/// // The data was mutated. Let check it out.
|
|
|
|
/// match clone_on_write {
|
|
|
|
/// Items { values: Cow::Owned(_) } => println!("clone_on_write contains owned data"),
|
|
|
|
/// _ => panic!("expect owned data"),
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-11 23:16:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub enum Cow<'a, B: ?Sized + 'a>
|
|
|
|
where B: ToOwned
|
|
|
|
{
|
2015-02-11 23:16:32 -08:00
|
|
|
/// Borrowed data.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-12-20 09:54:00 +05:30
|
|
|
Borrowed(#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
&'a B),
|
2015-02-11 23:16:32 -08:00
|
|
|
|
|
|
|
/// Owned data.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-12-20 09:54:00 +05:30
|
|
|
Owned(#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
<B as ToOwned>::Owned),
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-12-20 09:54:00 +05:30
|
|
|
impl<'a, B: ?Sized> Clone for Cow<'a, B>
|
|
|
|
where B: ToOwned
|
|
|
|
{
|
2015-02-11 23:16:32 -08:00
|
|
|
fn clone(&self) -> Cow<'a, B> {
|
|
|
|
match *self {
|
|
|
|
Borrowed(b) => Borrowed(b),
|
|
|
|
Owned(ref o) => {
|
|
|
|
let b: &B = o.borrow();
|
|
|
|
Owned(b.to_owned())
|
2015-11-24 11:23:48 +13:00
|
|
|
}
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
|
|
|
}
|
2017-04-01 19:33:45 -07:00
|
|
|
|
|
|
|
fn clone_from(&mut self, source: &Cow<'a, B>) {
|
|
|
|
if let Owned(ref mut dest) = *self {
|
|
|
|
if let Owned(ref o) = *source {
|
|
|
|
o.borrow().clone_into(dest);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*self = source.clone();
|
|
|
|
}
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
|
|
|
|
2016-12-20 09:54:00 +05:30
|
|
|
impl<'a, B: ?Sized> Cow<'a, B>
|
|
|
|
where B: ToOwned
|
|
|
|
{
|
2015-04-09 12:51:19 -04:00
|
|
|
/// Acquires a mutable reference to the owned form of the data.
|
2015-02-11 23:16:32 -08:00
|
|
|
///
|
2015-07-18 22:54:55 -05:00
|
|
|
/// Clones the data if it is not already owned.
|
2015-03-24 16:48:57 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::borrow::Cow;
|
|
|
|
///
|
2017-06-03 14:54:17 -04:00
|
|
|
/// let mut cow = Cow::Borrowed("foo");
|
|
|
|
/// cow.to_mut().make_ascii_uppercase();
|
2015-03-24 16:48:57 -04:00
|
|
|
///
|
2017-06-03 14:54:17 -04:00
|
|
|
/// assert_eq!(
|
|
|
|
/// cow,
|
|
|
|
/// Cow::Owned(String::from("FOO")) as Cow<str>
|
|
|
|
/// );
|
2015-03-24 16:48:57 -04:00
|
|
|
/// ```
|
2015-02-11 23:16:32 -08:00
|
|
|
#[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());
|
2016-11-04 18:47:32 +00:00
|
|
|
match *self {
|
|
|
|
Borrowed(..) => unreachable!(),
|
|
|
|
Owned(ref mut owned) => owned,
|
|
|
|
}
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
2015-11-24 11:23:48 +13:00
|
|
|
Owned(ref mut owned) => owned,
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 12:51:19 -04:00
|
|
|
/// Extracts the owned data.
|
2015-02-11 23:16:32 -08:00
|
|
|
///
|
2015-07-18 22:54:55 -05:00
|
|
|
/// Clones the data if it is not already owned.
|
2015-03-24 16:48:57 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-06-03 14:40:23 -04:00
|
|
|
/// Calling `into_owned` on a `Cow::Borrowed` clones the underlying data
|
|
|
|
/// and becomes a `Cow::Owned`:
|
|
|
|
///
|
2015-03-24 16:48:57 -04:00
|
|
|
/// ```
|
|
|
|
/// use std::borrow::Cow;
|
|
|
|
///
|
2017-06-03 14:40:23 -04:00
|
|
|
/// let s = "Hello world!";
|
|
|
|
/// let cow = Cow::Borrowed(s);
|
|
|
|
///
|
|
|
|
/// assert_eq!(
|
|
|
|
/// cow.into_owned(),
|
2017-11-14 18:23:24 -06:00
|
|
|
/// String::from(s)
|
2017-06-03 14:40:23 -04:00
|
|
|
/// );
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Calling `into_owned` on a `Cow::Owned` is a no-op:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::borrow::Cow;
|
2015-03-24 16:48:57 -04:00
|
|
|
///
|
2017-06-03 14:40:23 -04:00
|
|
|
/// let s = "Hello world!";
|
|
|
|
/// let cow: Cow<str> = Cow::Owned(String::from(s));
|
2015-03-24 16:48:57 -04:00
|
|
|
///
|
2017-06-03 14:40:23 -04:00
|
|
|
/// assert_eq!(
|
|
|
|
/// cow.into_owned(),
|
2017-11-14 18:23:24 -06:00
|
|
|
/// String::from(s)
|
2017-06-03 14:40:23 -04:00
|
|
|
/// );
|
2015-03-24 16:48:57 -04:00
|
|
|
/// ```
|
2015-02-11 23:16:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub fn into_owned(self) -> <B as ToOwned>::Owned {
|
|
|
|
match self {
|
|
|
|
Borrowed(borrowed) => borrowed.to_owned(),
|
2015-11-24 11:23:48 +13:00
|
|
|
Owned(owned) => owned,
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-12-20 09:54:00 +05:30
|
|
|
impl<'a, B: ?Sized> Deref for Cow<'a, B>
|
|
|
|
where B: ToOwned
|
|
|
|
{
|
2015-02-11 23:16:32 -08:00
|
|
|
type Target = B;
|
|
|
|
|
|
|
|
fn deref(&self) -> &B {
|
|
|
|
match *self {
|
|
|
|
Borrowed(borrowed) => borrowed,
|
2015-11-24 11:23:48 +13:00
|
|
|
Owned(ref owned) => owned.borrow(),
|
2015-02-11 23:16:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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")]
|
2016-12-20 09:54:00 +05:30
|
|
|
impl<'a, B: ?Sized> Ord for Cow<'a, B>
|
|
|
|
where B: Ord + ToOwned
|
|
|
|
{
|
2015-02-11 23:16:32 -08:00
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &Cow<'a, B>) -> Ordering {
|
|
|
|
Ord::cmp(&**self, &**other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, C>> for Cow<'a, B>
|
|
|
|
where B: PartialEq<C> + ToOwned,
|
|
|
|
C: ToOwned
|
2015-02-11 23:16:32 -08:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Cow<'b, C>) -> bool {
|
|
|
|
PartialEq::eq(&**self, &**other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-12-20 09:54:00 +05:30
|
|
|
impl<'a, B: ?Sized> PartialOrd for Cow<'a, B>
|
|
|
|
where B: PartialOrd + ToOwned
|
|
|
|
{
|
2015-02-11 23:16:32 -08:00
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &Cow<'a, B>) -> Option<Ordering> {
|
|
|
|
PartialOrd::partial_cmp(&**self, &**other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
impl<'a, B: ?Sized> fmt::Debug for Cow<'a, B>
|
|
|
|
where B: fmt::Debug + ToOwned,
|
|
|
|
<B as ToOwned>::Owned: fmt::Debug
|
2015-02-11 23:16:32 -08:00
|
|
|
{
|
|
|
|
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")]
|
2015-11-24 11:23:48 +13:00
|
|
|
impl<'a, B: ?Sized> fmt::Display for Cow<'a, B>
|
|
|
|
where B: fmt::Display + ToOwned,
|
|
|
|
<B as ToOwned>::Owned: fmt::Display
|
2015-02-11 23:16:32 -08:00
|
|
|
{
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:44:56 +01:00
|
|
|
#[stable(feature = "default", since = "1.11.0")]
|
|
|
|
impl<'a, B: ?Sized> Default for Cow<'a, B>
|
|
|
|
where B: ToOwned,
|
|
|
|
<B as ToOwned>::Owned: Default
|
|
|
|
{
|
2016-09-11 22:58:01 +05:30
|
|
|
/// Creates an owned Cow<'a, B> with the default value for the contained owned value.
|
2016-06-16 14:44:56 +01:00
|
|
|
fn default() -> Cow<'a, B> {
|
|
|
|
Owned(<B as ToOwned>::Owned::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:16:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-12-20 09:54:00 +05:30
|
|
|
impl<'a, B: ?Sized> Hash for Cow<'a, B>
|
|
|
|
where B: Hash + ToOwned
|
|
|
|
{
|
2015-02-18 15:34:32 -08:00
|
|
|
#[inline]
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
Hash::hash(&**self, state)
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 23:16:32 -08:00
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-02 22:22:44 -07:00
|
|
|
impl<'a, T: ?Sized + ToOwned> AsRef<T> for Cow<'a, T> {
|
2015-03-18 09:14:54 -07:00
|
|
|
fn as_ref(&self) -> &T {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 23:35:20 +02:00
|
|
|
|
2016-11-04 01:07:00 +00:00
|
|
|
#[stable(feature = "cow_add", since = "1.14.0")]
|
2016-09-12 23:35:20 +02:00
|
|
|
impl<'a> Add<&'a str> for Cow<'a, str> {
|
|
|
|
type Output = Cow<'a, str>;
|
|
|
|
|
2016-11-04 01:07:00 +00:00
|
|
|
#[inline]
|
|
|
|
fn add(mut self, rhs: &'a str) -> Self::Output {
|
|
|
|
self += rhs;
|
|
|
|
self
|
2016-09-12 23:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 01:07:00 +00:00
|
|
|
#[stable(feature = "cow_add", since = "1.14.0")]
|
2016-09-12 23:35:20 +02:00
|
|
|
impl<'a> Add<Cow<'a, str>> for Cow<'a, str> {
|
|
|
|
type Output = Cow<'a, str>;
|
|
|
|
|
2016-11-04 01:07:00 +00:00
|
|
|
#[inline]
|
|
|
|
fn add(mut self, rhs: Cow<'a, str>) -> Self::Output {
|
|
|
|
self += rhs;
|
|
|
|
self
|
2016-09-12 23:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 01:07:00 +00:00
|
|
|
#[stable(feature = "cow_add", since = "1.14.0")]
|
2016-09-12 23:35:20 +02:00
|
|
|
impl<'a> AddAssign<&'a str> for Cow<'a, str> {
|
|
|
|
fn add_assign(&mut self, rhs: &'a str) {
|
2016-11-04 01:07:00 +00:00
|
|
|
if self.is_empty() {
|
|
|
|
*self = Cow::Borrowed(rhs)
|
|
|
|
} else if rhs.is_empty() {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if let Cow::Borrowed(lhs) = *self {
|
|
|
|
let mut s = String::with_capacity(lhs.len() + rhs.len());
|
|
|
|
s.push_str(lhs);
|
|
|
|
*self = Cow::Owned(s);
|
|
|
|
}
|
|
|
|
self.to_mut().push_str(rhs);
|
|
|
|
}
|
2016-09-12 23:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 01:07:00 +00:00
|
|
|
#[stable(feature = "cow_add", since = "1.14.0")]
|
2016-09-12 23:35:20 +02:00
|
|
|
impl<'a> AddAssign<Cow<'a, str>> for Cow<'a, str> {
|
|
|
|
fn add_assign(&mut self, rhs: Cow<'a, str>) {
|
2016-11-04 01:07:00 +00:00
|
|
|
if self.is_empty() {
|
|
|
|
*self = rhs
|
|
|
|
} else if rhs.is_empty() {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if let Cow::Borrowed(lhs) = *self {
|
|
|
|
let mut s = String::with_capacity(lhs.len() + rhs.len());
|
|
|
|
s.push_str(lhs);
|
|
|
|
*self = Cow::Owned(s);
|
|
|
|
}
|
|
|
|
self.to_mut().push_str(&rhs);
|
|
|
|
}
|
2016-09-12 23:35:20 +02:00
|
|
|
}
|
|
|
|
}
|