Skip to content

Commit 3293ab1

Browse files
author
Jorge Aparicio
committed
Deprecate MaybeOwned[Vector] in favor of Cow
1 parent 48ca6d1 commit 3293ab1

File tree

21 files changed

+326
-172
lines changed

21 files changed

+326
-172
lines changed

src/libcollections/hash/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use core::prelude::*;
6767

6868
use alloc::boxed::Box;
6969
use alloc::rc::Rc;
70+
use core::borrow::{Cow, ToOwned};
7071
use core::intrinsics::TypeId;
7172
use core::mem;
7273
use core::num::Int;
@@ -284,6 +285,13 @@ impl<S: Writer, T: Hash<S>, U: Hash<S>> Hash<S> for Result<T, U> {
284285
}
285286
}
286287

288+
impl<'a, T, Sized? B, S> Hash<S> for Cow<'a, T, B> where B: Hash<S> + ToOwned<T> {
289+
#[inline]
290+
fn hash(&self, state: &mut S) {
291+
Hash::hash(&**self, state)
292+
}
293+
}
294+
287295
//////////////////////////////////////////////////////////////////////////////
288296

289297
#[cfg(test)]

src/libcollections/str.rs

+44-10
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, ToOwned};
57+
use core::borrow::{BorrowFrom, Cow, 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, ToString};
70+
use string::String;
7171
use unicode;
7272
use vec::Vec;
7373

@@ -425,22 +425,24 @@ Section: MaybeOwned
425425
/// A string type that can hold either a `String` or a `&str`.
426426
/// This can be useful as an optimization when an allocation is sometimes
427427
/// needed but not always.
428+
#[deprecated = "use std::str::CowString"]
428429
pub enum MaybeOwned<'a> {
429430
/// A borrowed string.
430431
Slice(&'a str),
431432
/// An owned string.
432433
Owned(String)
433434
}
434435

435-
/// A specialization of `MaybeOwned` to be sendable.
436-
pub type SendStr = MaybeOwned<'static>;
436+
/// A specialization of `CowString` to be sendable.
437+
pub type SendStr = CowString<'static>;
437438

439+
#[deprecated = "use std::str::CowString"]
438440
impl<'a> MaybeOwned<'a> {
439441
/// Returns `true` if this `MaybeOwned` wraps an owned string.
440442
///
441443
/// # Example
442444
///
443-
/// ```rust
445+
/// ``` ignore
444446
/// let string = String::from_str("orange");
445447
/// let maybe_owned_string = string.into_maybe_owned();
446448
/// assert_eq!(true, maybe_owned_string.is_owned());
@@ -457,7 +459,7 @@ impl<'a> MaybeOwned<'a> {
457459
///
458460
/// # Example
459461
///
460-
/// ```rust
462+
/// ``` ignore
461463
/// let string = "orange";
462464
/// let maybe_owned_string = string.as_slice().into_maybe_owned();
463465
/// assert_eq!(true, maybe_owned_string.is_slice());
@@ -475,46 +477,56 @@ impl<'a> MaybeOwned<'a> {
475477
pub fn len(&self) -> uint { self.as_slice().len() }
476478

477479
/// Returns true if the string contains no bytes
480+
#[allow(deprecated)]
478481
#[inline]
479482
pub fn is_empty(&self) -> bool { self.len() == 0 }
480483
}
481484

485+
#[deprecated = "use std::borrow::IntoCow"]
482486
/// Trait for moving into a `MaybeOwned`.
483487
pub trait IntoMaybeOwned<'a> {
484488
/// Moves `self` into a `MaybeOwned`.
485489
fn into_maybe_owned(self) -> MaybeOwned<'a>;
486490
}
487491

492+
#[deprecated = "use std::borrow::IntoCow"]
493+
#[allow(deprecated)]
488494
impl<'a> IntoMaybeOwned<'a> for String {
489495
/// # Example
490496
///
491-
/// ```rust
497+
/// ``` ignore
492498
/// let owned_string = String::from_str("orange");
493499
/// let maybe_owned_string = owned_string.into_maybe_owned();
494500
/// assert_eq!(true, maybe_owned_string.is_owned());
495501
/// ```
502+
#[allow(deprecated)]
496503
#[inline]
497504
fn into_maybe_owned(self) -> MaybeOwned<'a> {
498505
Owned(self)
499506
}
500507
}
501508

509+
#[deprecated = "use std::borrow::IntoCow"]
510+
#[allow(deprecated)]
502511
impl<'a> IntoMaybeOwned<'a> for &'a str {
503512
/// # Example
504513
///
505-
/// ```rust
514+
/// ``` ignore
506515
/// let string = "orange";
507516
/// let maybe_owned_str = string.as_slice().into_maybe_owned();
508517
/// assert_eq!(false, maybe_owned_str.is_owned());
509518
/// ```
519+
#[allow(deprecated)]
510520
#[inline]
511521
fn into_maybe_owned(self) -> MaybeOwned<'a> { Slice(self) }
512522
}
513523

524+
#[allow(deprecated)]
525+
#[deprecated = "use std::borrow::IntoCow"]
514526
impl<'a> IntoMaybeOwned<'a> for MaybeOwned<'a> {
515527
/// # Example
516528
///
517-
/// ```rust
529+
/// ``` ignore
518530
/// let str = "orange";
519531
/// let maybe_owned_str = str.as_slice().into_maybe_owned();
520532
/// let maybe_maybe_owned_str = maybe_owned_str.into_maybe_owned();
@@ -524,37 +536,44 @@ impl<'a> IntoMaybeOwned<'a> for MaybeOwned<'a> {
524536
fn into_maybe_owned(self) -> MaybeOwned<'a> { self }
525537
}
526538

539+
#[deprecated = "use std::str::CowString"]
527540
impl<'a> PartialEq for MaybeOwned<'a> {
528541
#[inline]
529542
fn eq(&self, other: &MaybeOwned) -> bool {
530543
self.as_slice() == other.as_slice()
531544
}
532545
}
533546

547+
#[deprecated = "use std::str::CowString"]
534548
impl<'a> Eq for MaybeOwned<'a> {}
535549

550+
#[deprecated = "use std::str::CowString"]
536551
impl<'a> PartialOrd for MaybeOwned<'a> {
537552
#[inline]
538553
fn partial_cmp(&self, other: &MaybeOwned) -> Option<Ordering> {
539554
Some(self.cmp(other))
540555
}
541556
}
542557

558+
#[deprecated = "use std::str::CowString"]
543559
impl<'a> Ord for MaybeOwned<'a> {
544560
#[inline]
545561
fn cmp(&self, other: &MaybeOwned) -> Ordering {
546562
self.as_slice().cmp(other.as_slice())
547563
}
548564
}
549565

566+
#[deprecated = "use std::str::CowString"]
550567
impl<'a, S: Str> Equiv<S> for MaybeOwned<'a> {
551568
#[inline]
552569
fn equiv(&self, other: &S) -> bool {
553570
self.as_slice() == other.as_slice()
554571
}
555572
}
556573

574+
#[deprecated = "use std::str::CowString"]
557575
impl<'a> Str for MaybeOwned<'a> {
576+
#[allow(deprecated)]
558577
#[inline]
559578
fn as_slice<'b>(&'b self) -> &'b str {
560579
match *self {
@@ -564,7 +583,9 @@ impl<'a> Str for MaybeOwned<'a> {
564583
}
565584
}
566585

586+
#[deprecated = "use std::str::CowString"]
567587
impl<'a> StrAllocating for MaybeOwned<'a> {
588+
#[allow(deprecated)]
568589
#[inline]
569590
fn into_string(self) -> String {
570591
match self {
@@ -574,7 +595,9 @@ impl<'a> StrAllocating for MaybeOwned<'a> {
574595
}
575596
}
576597

598+
#[deprecated = "use std::str::CowString"]
577599
impl<'a> Clone for MaybeOwned<'a> {
600+
#[allow(deprecated)]
578601
#[inline]
579602
fn clone(&self) -> MaybeOwned<'a> {
580603
match *self {
@@ -584,18 +607,22 @@ impl<'a> Clone for MaybeOwned<'a> {
584607
}
585608
}
586609

610+
#[deprecated = "use std::str::CowString"]
587611
impl<'a> Default for MaybeOwned<'a> {
612+
#[allow(deprecated)]
588613
#[inline]
589614
fn default() -> MaybeOwned<'a> { Slice("") }
590615
}
591616

617+
#[deprecated = "use std::str::CowString"]
592618
impl<'a, H: hash::Writer> hash::Hash<H> for MaybeOwned<'a> {
593619
#[inline]
594620
fn hash(&self, hasher: &mut H) {
595621
self.as_slice().hash(hasher)
596622
}
597623
}
598624

625+
#[deprecated = "use std::str::CowString"]
599626
impl<'a> fmt::Show for MaybeOwned<'a> {
600627
#[inline]
601628
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -613,7 +640,7 @@ impl BorrowFrom<String> for str {
613640

614641
#[unstable = "trait is unstable"]
615642
impl ToOwned<String> for str {
616-
fn to_owned(&self) -> String { self.to_string() }
643+
fn to_owned(&self) -> String { self.into_string() }
617644
}
618645

619646
/// Unsafe string operations.
@@ -622,6 +649,13 @@ pub mod raw {
622649
pub use core::str::raw::{slice_unchecked};
623650
}
624651

652+
/*
653+
Section: CowString
654+
*/
655+
656+
/// A clone-on-write string
657+
pub type CowString<'a> = Cow<'a, String, str>;
658+
625659
/*
626660
Section: Trait implementations
627661
*/

src/libcollections/string.rs

+30-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
use core::prelude::*;
1616

17+
use core::borrow::{Cow, IntoCow};
1718
use core::default::Default;
1819
use core::fmt;
1920
use core::mem;
@@ -25,8 +26,7 @@ use core::raw::Slice as RawSlice;
2526
use hash;
2627
use slice::CloneSliceAllocPrelude;
2728
use str;
28-
use str::{CharRange, FromStr, StrAllocating, MaybeOwned, Owned};
29-
use str::Slice as MaybeOwnedSlice; // So many `Slice`s...
29+
use str::{CharRange, CowString, FromStr, StrAllocating, Owned};
3030
use vec::{DerefVec, Vec, as_vec};
3131

3232
/// A growable string stored as a UTF-8 encoded buffer.
@@ -121,9 +121,9 @@ impl String {
121121
/// assert_eq!(output.as_slice(), "Hello \uFFFDWorld");
122122
/// ```
123123
#[unstable = "return type may change"]
124-
pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> MaybeOwned<'a> {
124+
pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> CowString<'a> {
125125
if str::is_utf8(v) {
126-
return MaybeOwnedSlice(unsafe { mem::transmute(v) })
126+
return Cow::Borrowed(unsafe { mem::transmute(v) })
127127
}
128128

129129
static TAG_CONT_U8: u8 = 128u8;
@@ -234,7 +234,7 @@ impl String {
234234
res.as_mut_vec().push_all(v[subseqidx..total])
235235
};
236236
}
237-
Owned(res.into_string())
237+
Cow::Owned(res.into_string())
238238
}
239239

240240
/// Decode a UTF-16 encoded vector `v` into a `String`, returning `None`
@@ -868,6 +868,18 @@ impl<T: fmt::Show> ToString for T {
868868
}
869869
}
870870

871+
impl IntoCow<'static, String, str> for String {
872+
fn into_cow(self) -> CowString<'static> {
873+
Cow::Owned(self)
874+
}
875+
}
876+
877+
impl<'a> IntoCow<'a, String, str> for &'a str {
878+
fn into_cow(self) -> CowString<'a> {
879+
Cow::Borrowed(self)
880+
}
881+
}
882+
871883
/// Unsafe operations
872884
#[deprecated]
873885
pub mod raw {
@@ -921,11 +933,11 @@ mod tests {
921933
use std::prelude::*;
922934
use test::Bencher;
923935

936+
use slice::CloneSliceAllocPrelude;
937+
use str::{Str, StrPrelude};
924938
use str;
925-
use str::{Str, StrPrelude, Owned};
926939
use super::{as_string, String, ToString};
927940
use vec::Vec;
928-
use slice::CloneSliceAllocPrelude;
929941

930942
#[test]
931943
fn test_as_string() {
@@ -955,39 +967,39 @@ mod tests {
955967
#[test]
956968
fn test_from_utf8_lossy() {
957969
let xs = b"hello";
958-
assert_eq!(String::from_utf8_lossy(xs), str::Slice("hello"));
970+
assert_eq!(String::from_utf8_lossy(xs), "hello".into_cow());
959971

960972
let xs = "ศไทย中华Việt Nam".as_bytes();
961-
assert_eq!(String::from_utf8_lossy(xs), str::Slice("ศไทย中华Việt Nam"));
973+
assert_eq!(String::from_utf8_lossy(xs), "ศไทย中华Việt Nam".into_cow());
962974

963975
let xs = b"Hello\xC2 There\xFF Goodbye";
964976
assert_eq!(String::from_utf8_lossy(xs),
965-
Owned(String::from_str("Hello\uFFFD There\uFFFD Goodbye")));
977+
String::from_str("Hello\uFFFD There\uFFFD Goodbye").into_cow());
966978

967979
let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
968980
assert_eq!(String::from_utf8_lossy(xs),
969-
Owned(String::from_str("Hello\uFFFD\uFFFD There\uFFFD Goodbye")));
981+
String::from_str("Hello\uFFFD\uFFFD There\uFFFD Goodbye").into_cow());
970982

971983
let xs = b"\xF5foo\xF5\x80bar";
972984
assert_eq!(String::from_utf8_lossy(xs),
973-
Owned(String::from_str("\uFFFDfoo\uFFFD\uFFFDbar")));
985+
String::from_str("\uFFFDfoo\uFFFD\uFFFDbar").into_cow());
974986

975987
let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
976988
assert_eq!(String::from_utf8_lossy(xs),
977-
Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFDbaz")));
989+
String::from_str("\uFFFDfoo\uFFFDbar\uFFFDbaz").into_cow());
978990

979991
let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
980992
assert_eq!(String::from_utf8_lossy(xs),
981-
Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFD\uFFFDbaz")));
993+
String::from_str("\uFFFDfoo\uFFFDbar\uFFFD\uFFFDbaz").into_cow());
982994

983995
let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar";
984-
assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("\uFFFD\uFFFD\uFFFD\uFFFD\
985-
foo\U00010000bar")));
996+
assert_eq!(String::from_utf8_lossy(xs), String::from_str("\uFFFD\uFFFD\uFFFD\uFFFD\
997+
foo\U00010000bar").into_cow());
986998

987999
// surrogates
9881000
let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar";
989-
assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("\uFFFD\uFFFD\uFFFDfoo\
990-
\uFFFD\uFFFD\uFFFDbar")));
1001+
assert_eq!(String::from_utf8_lossy(xs), String::from_str("\uFFFD\uFFFD\uFFFDfoo\
1002+
\uFFFD\uFFFD\uFFFDbar").into_cow());
9911003
}
9921004

9931005
#[test]

0 commit comments

Comments
 (0)