Skip to content

Commit bab9564

Browse files
committed
libs: make Cow usable, improve documentation
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.
1 parent 7936242 commit bab9564

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

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

612+
#[unstable = "trait is unstable"]
613+
impl ToOwned<String> for str {
614+
fn to_owned(&self) -> String { self.to_string() }
615+
}
616+
612617
/// Unsafe string operations.
613618
pub mod raw {
614619
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

@@ -84,15 +84,31 @@ impl<T> ToOwned<T> for T where T: Clone {
8484
}
8585

8686
/// A clone-on-write smart pointer.
87-
pub enum Cow<'a, T, B: 'a> where B: ToOwned<T> {
87+
///
88+
/// # Example
89+
///
90+
/// ```rust
91+
/// use std::borrow::Cow;
92+
///
93+
/// fn abs_all(input: &mut Cow<Vec<int>, [int]>) {
94+
/// for i in range(0, input.len()) {
95+
/// let v = input[i];
96+
/// if v < 0 {
97+
/// // clones into a vector the first time (if not already owned)
98+
/// input.to_mut()[i] = -v;
99+
/// }
100+
/// }
101+
/// }
102+
/// ```
103+
pub enum Cow<'a, T, Sized? B: 'a> where B: ToOwned<T> {
88104
/// Borrowed data.
89105
Borrowed(&'a B),
90106

91107
/// Owned data.
92108
Owned(T)
93109
}
94110

95-
impl<'a, T, B> Cow<'a, T, B> where B: ToOwned<T> {
111+
impl<'a, T, Sized? B> Cow<'a, T, B> where B: ToOwned<T> {
96112
/// Acquire a mutable reference to the owned form of the data.
97113
///
98114
/// Copies the data if it is not already owned.
@@ -117,7 +133,7 @@ impl<'a, T, B> Cow<'a, T, B> where B: ToOwned<T> {
117133
}
118134
}
119135

120-
impl<'a, T, B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> {
136+
impl<'a, T, Sized? B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> {
121137
fn deref(&self) -> &B {
122138
match *self {
123139
Borrowed(borrowed) => borrowed,

0 commit comments

Comments
 (0)