Skip to content

Commit 5ff10d5

Browse files
committed
auto merge of #19157 : aturon/rust/cow-doc, r=alexcrichton
This commit makes `Cow` more usable by allowing it to be applied to unsized types (as was intended) and providing some basic `ToOwned` implementations on slice types. It also corrects the documentation for `Cow` to no longer mention `DerefMut`, and adds an example. Closes #19123
2 parents f5212e3 + bab9564 commit 5ff10d5

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

src/libcollections/slice.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
use self::Direction::*;
9191
use alloc::boxed::Box;
92-
use core::borrow::{BorrowFrom, BorrowFromMut};
92+
use core::borrow::{BorrowFrom, BorrowFromMut, ToOwned};
9393
use core::cmp;
9494
use core::kinds::Sized;
9595
use core::mem::size_of;
@@ -658,6 +658,11 @@ impl<T> BorrowFromMut<Vec<T>> for [T] {
658658
fn borrow_from_mut(owned: &mut Vec<T>) -> &mut [T] { owned[mut] }
659659
}
660660

661+
#[unstable = "trait is unstable"]
662+
impl<T: Clone> ToOwned<Vec<T>> for [T] {
663+
fn to_owned(&self) -> Vec<T> { self.to_vec() }
664+
}
665+
661666
/// Unsafe operations
662667
pub mod raw {
663668
pub use core::slice::raw::{buf_as_slice, mut_buf_as_slice};

src/libcollections/str.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
pub use self::MaybeOwned::*;
5555
use self::RecompositionState::*;
5656
use self::DecompositionType::*;
57-
use core::borrow::BorrowFrom;
57+
use core::borrow::{BorrowFrom, ToOwned};
5858
use core::default::Default;
5959
use core::fmt;
6060
use core::cmp;
@@ -67,7 +67,7 @@ use core::prelude::{range};
6767

6868
use hash;
6969
use ring_buf::RingBuf;
70-
use string::String;
70+
use string::{String, ToString};
7171
use unicode;
7272
use vec::Vec;
7373

@@ -611,6 +611,11 @@ impl BorrowFrom<String> for str {
611611
fn borrow_from(owned: &String) -> &str { owned[] }
612612
}
613613

614+
#[unstable = "trait is unstable"]
615+
impl ToOwned<String> for str {
616+
fn to_owned(&self) -> String { self.to_string() }
617+
}
618+
614619
/// Unsafe string operations.
615620
pub mod raw {
616621
pub use core::str::raw::{from_utf8, c_str_to_static_slice, slice_bytes};

src/libcore/borrow.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
//! data lazily when mutation or ownership is required. The type is designed to
3838
//! work with general borrowed data via the `BorrowFrom` trait.
3939
//!
40-
//! `Cow` implements both `Deref` and `DerefMut`, which means that you can call
41-
//! methods directly on the data it encloses. The first time a mutable reference
42-
//! is required, the data will be cloned (via `to_owned`) if it is not
43-
//! already owned.
40+
//! `Cow` implements both `Deref`, which means that you can call
41+
//! non-mutating methods directly on the data it encloses. If mutation
42+
//! is desired, `to_mut` will obtain a mutable references to an owned
43+
//! value, cloning if necessary.
4444
4545
#![unstable = "recently added as part of collections reform"]
4646

@@ -92,15 +92,31 @@ impl<T> ToOwned<T> for T where T: Clone {
9292
}
9393

9494
/// A clone-on-write smart pointer.
95-
pub enum Cow<'a, T, B: 'a> where B: ToOwned<T> {
95+
///
96+
/// # Example
97+
///
98+
/// ```rust
99+
/// use std::borrow::Cow;
100+
///
101+
/// fn abs_all(input: &mut Cow<Vec<int>, [int]>) {
102+
/// for i in range(0, input.len()) {
103+
/// let v = input[i];
104+
/// if v < 0 {
105+
/// // clones into a vector the first time (if not already owned)
106+
/// input.to_mut()[i] = -v;
107+
/// }
108+
/// }
109+
/// }
110+
/// ```
111+
pub enum Cow<'a, T, Sized? B: 'a> where B: ToOwned<T> {
96112
/// Borrowed data.
97113
Borrowed(&'a B),
98114

99115
/// Owned data.
100116
Owned(T)
101117
}
102118

103-
impl<'a, T, B> Cow<'a, T, B> where B: ToOwned<T> {
119+
impl<'a, T, Sized? B> Cow<'a, T, B> where B: ToOwned<T> {
104120
/// Acquire a mutable reference to the owned form of the data.
105121
///
106122
/// Copies the data if it is not already owned.
@@ -125,7 +141,7 @@ impl<'a, T, B> Cow<'a, T, B> where B: ToOwned<T> {
125141
}
126142
}
127143

128-
impl<'a, T, B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> {
144+
impl<'a, T, Sized? B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> {
129145
fn deref(&self) -> &B {
130146
match *self {
131147
Borrowed(borrowed) => borrowed,

0 commit comments

Comments
 (0)