Delete send_str, rewrite clients on top of MaybeOwned<'static>

Declare a `type SendStr = MaybeOwned<'static>` to ease readibility of
types that needed the old SendStr behavior.

Implement all the traits for MaybeOwned that SendStr used to implement.
This commit is contained in:
Kevin Ballard 2014-02-07 16:36:59 -08:00
parent 122c94d2f3
commit 086c0dd33f
14 changed files with 310 additions and 391 deletions

View File

@ -87,8 +87,7 @@ syn keyword rustTrait Primitive Int Float ToStrRadix ToPrimitive FromPrimitive
syn keyword rustTrait GenericPath Path PosixPath WindowsPath
syn keyword rustTrait RawPtr
syn keyword rustTrait Buffer Writer Reader Seek
syn keyword rustTrait SendStr SendStrOwned SendStrStatic IntoSendStr
syn keyword rustTrait Str StrVector StrSlice OwnedStr
syn keyword rustTrait Str StrVector StrSlice OwnedStr IntoMaybeOwned
syn keyword rustTrait IterBytes
syn keyword rustTrait ToStr IntoStr
syn keyword rustTrait CloneableTuple ImmutableTuple

View File

@ -893,8 +893,8 @@ pub fn run_test(force_ignore: bool,
spawn(proc() {
let mut task = task::task();
task.name(match desc.name {
DynTestName(ref name) => SendStrOwned(name.clone()),
StaticTestName(name) => SendStrStatic(name),
DynTestName(ref name) => name.to_owned().into_maybe_owned(),
StaticTestName(name) => name.into_maybe_owned()
});
let result_future = task.future_result();
task.spawn(testfn);

View File

@ -257,7 +257,7 @@ pub fn run(main: proc()) -> int {
let (port, chan) = Chan::new();
let mut opts = TaskOpts::new();
opts.notify_chan = Some(chan);
opts.name = Some(SendStrStatic("<main>"));
opts.name = Some("<main>".into_maybe_owned());
pool.spawn(opts, main);
// Wait for the main task to return, and set the process error code

View File

@ -510,7 +510,7 @@ mod tests {
#[test]
fn smoke_opts() {
let mut opts = TaskOpts::new();
opts.name = Some(SendStrStatic("test"));
opts.name = Some("test".into_maybe_owned());
opts.stack_size = Some(20 * 4096);
let (p, c) = Chan::new();
opts.notify_chan = Some(c);

View File

@ -294,7 +294,7 @@ mod tests {
#[test]
fn smoke_opts() {
let mut opts = TaskOpts::new();
opts.name = Some(SendStrStatic("test"));
opts.name = Some("test".into_maybe_owned());
opts.stack_size = Some(20 * 4096);
let (p, c) = Chan::new();
opts.notify_chan = Some(c);

View File

@ -117,7 +117,6 @@ pub mod vec_ng;
pub mod str;
pub mod ascii;
pub mod send_str;
pub mod ptr;
pub mod owned;

View File

@ -63,8 +63,7 @@ pub use num::{Primitive, Int, Float, ToStrRadix, ToPrimitive, FromPrimitive};
pub use path::{GenericPath, Path, PosixPath, WindowsPath};
pub use ptr::RawPtr;
pub use io::{Buffer, Writer, Reader, Seek};
pub use send_str::{SendStr, SendStrOwned, SendStrStatic, IntoSendStr};
pub use str::{Str, StrVector, StrSlice, OwnedStr};
pub use str::{Str, StrVector, StrSlice, OwnedStr, IntoMaybeOwned};
pub use to_bytes::IterBytes;
pub use to_str::{ToStr, IntoStr};
pub use tuple::{CloneableTuple, ImmutableTuple};

View File

@ -30,7 +30,7 @@ use rt::local::Local;
use rt::local_heap::LocalHeap;
use rt::rtio::LocalIo;
use rt::unwind::Unwinder;
use send_str::SendStr;
use str::SendStr;
use sync::arc::UnsafeArc;
use sync::atomics::{AtomicUint, SeqCst};
use task::{TaskResult, TaskOpts};

View File

@ -1,246 +0,0 @@
// Copyright 2013-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.
//! The `SendStr` trait for optionally static strings
use clone::{Clone, DeepClone};
use cmp::{Eq, TotalEq, Ord, TotalOrd, Equiv};
use cmp::Ordering;
use container::Container;
use default::Default;
use str::{Str, StrSlice};
use to_str::ToStr;
use to_bytes::{IterBytes, Cb};
/// A SendStr is a string that can hold either a ~str or a &'static str.
/// This can be useful as an optimization when an allocation is sometimes
/// needed but the common case is statically known.
#[allow(missing_doc)]
pub enum SendStr {
SendStrOwned(~str),
SendStrStatic(&'static str)
}
impl SendStr {
/// Returns `true` if this `SendStr` wraps an owned string
#[inline]
pub fn is_owned(&self) -> bool {
match *self {
SendStrOwned(_) => true,
SendStrStatic(_) => false
}
}
/// Returns `true` if this `SendStr` wraps a static string
#[inline]
pub fn is_static(&self) -> bool {
match *self {
SendStrOwned(_) => false,
SendStrStatic(_) => true
}
}
}
/// Trait for moving into an `SendStr`
pub trait IntoSendStr {
/// Moves self into an `SendStr`
fn into_send_str(self) -> SendStr;
}
impl IntoSendStr for ~str {
#[inline]
fn into_send_str(self) -> SendStr { SendStrOwned(self) }
}
impl IntoSendStr for &'static str {
#[inline]
fn into_send_str(self) -> SendStr { SendStrStatic(self) }
}
impl IntoSendStr for SendStr {
#[inline]
fn into_send_str(self) -> SendStr { self }
}
/*
Section: String trait impls.
`SendStr` should behave like a normal string, so we don't derive.
*/
impl ToStr for SendStr {
#[inline]
fn to_str(&self) -> ~str { self.as_slice().to_owned() }
}
impl Eq for SendStr {
#[inline]
fn eq(&self, other: &SendStr) -> bool {
self.as_slice().equals(&other.as_slice())
}
}
impl TotalEq for SendStr {
#[inline]
fn equals(&self, other: &SendStr) -> bool {
self.as_slice().equals(&other.as_slice())
}
}
impl Ord for SendStr {
#[inline]
fn lt(&self, other: &SendStr) -> bool {
self.as_slice().lt(&other.as_slice())
}
}
impl TotalOrd for SendStr {
#[inline]
fn cmp(&self, other: &SendStr) -> Ordering {
self.as_slice().cmp(&other.as_slice())
}
}
impl<'a, S: Str> Equiv<S> for SendStr {
#[inline]
fn equiv(&self, other: &S) -> bool {
self.as_slice().equals(&other.as_slice())
}
}
impl Str for SendStr {
#[inline]
fn as_slice<'r>(&'r self) -> &'r str {
match *self {
SendStrOwned(ref s) => s.as_slice(),
// FIXME: Borrowchecker doesn't recognize lifetime as static unless prompted
// SendStrStatic(s) => s.as_slice()
SendStrStatic(s) => {let tmp: &'static str = s; tmp}
}
}
#[inline]
fn into_owned(self) -> ~str {
match self {
SendStrOwned(s) => s,
SendStrStatic(s) => s.to_owned()
}
}
}
impl Container for SendStr {
#[inline]
fn len(&self) -> uint { self.as_slice().len() }
}
impl Clone for SendStr {
#[inline]
fn clone(&self) -> SendStr {
match *self {
SendStrOwned(ref s) => SendStrOwned(s.to_owned()),
SendStrStatic(s) => SendStrStatic(s)
}
}
}
impl DeepClone for SendStr {
#[inline]
fn deep_clone(&self) -> SendStr {
match *self {
SendStrOwned(ref s) => SendStrOwned(s.to_owned()),
SendStrStatic(s) => SendStrStatic(s)
}
}
}
impl Default for SendStr {
#[inline]
fn default() -> SendStr { SendStrStatic("") }
}
impl IterBytes for SendStr {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
match *self {
SendStrOwned(ref s) => s.iter_bytes(lsb0, f),
SendStrStatic(s) => s.iter_bytes(lsb0, f)
}
}
}
#[cfg(test)]
mod tests {
use prelude::*;
use send_str::{SendStrOwned, SendStrStatic};
#[test]
fn test_send_str_traits() {
let s = SendStrStatic("abcde");
assert_eq!(s.len(), 5);
assert_eq!(s.as_slice(), "abcde");
assert_eq!(s.to_str(), ~"abcde");
assert!(s.lt(&SendStrOwned(~"bcdef")));
assert_eq!(SendStrStatic(""), Default::default());
let o = SendStrOwned(~"abcde");
assert_eq!(o.len(), 5);
assert_eq!(o.as_slice(), "abcde");
assert_eq!(o.to_str(), ~"abcde");
assert!(o.lt(&SendStrStatic("bcdef")));
assert_eq!(SendStrOwned(~""), Default::default());
assert_eq!(s.cmp(&o), Equal);
assert!(s.equals(&o));
assert!(s.equiv(&o));
assert_eq!(o.cmp(&s), Equal);
assert!(o.equals(&s));
assert!(o.equiv(&s));
}
#[test]
fn test_send_str_methods() {
let s = SendStrStatic("abcde");
assert!(s.is_static());
assert!(!s.is_owned());
let o = SendStrOwned(~"abcde");
assert!(!o.is_static());
assert!(o.is_owned());
}
#[test]
fn test_send_str_clone() {
assert_eq!(SendStrOwned(~"abcde"), SendStrStatic("abcde").clone());
assert_eq!(SendStrOwned(~"abcde"), SendStrStatic("abcde").deep_clone());
assert_eq!(SendStrOwned(~"abcde"), SendStrOwned(~"abcde").clone());
assert_eq!(SendStrOwned(~"abcde"), SendStrOwned(~"abcde").deep_clone());
assert_eq!(SendStrStatic("abcde"), SendStrStatic("abcde").clone());
assert_eq!(SendStrStatic("abcde"), SendStrStatic("abcde").deep_clone());
assert_eq!(SendStrStatic("abcde"), SendStrOwned(~"abcde").clone());
assert_eq!(SendStrStatic("abcde"), SendStrOwned(~"abcde").deep_clone());
}
#[test]
fn test_send_str_into_owned() {
assert_eq!(SendStrStatic("abcde").into_owned(), ~"abcde");
assert_eq!(SendStrOwned(~"abcde").into_owned(), ~"abcde");
}
#[test]
fn test_into_send_str() {
assert_eq!("abcde".into_send_str(), SendStrStatic("abcde"));
assert_eq!((~"abcde").into_send_str(), SendStrStatic("abcde"));
assert_eq!("abcde".into_send_str(), SendStrOwned(~"abcde"));
assert_eq!((~"abcde").into_send_str(), SendStrOwned(~"abcde"));
}
}

View File

@ -86,7 +86,9 @@ use cast::transmute;
use char;
use char::Char;
use clone::{Clone, DeepClone};
use cmp::{Eq, TotalEq, Ord, TotalOrd, Equiv, Ordering};
use container::{Container, Mutable};
use fmt;
use iter::{Iterator, FromIterator, Extendable, range};
use iter::{Filter, AdditiveIterator, Map};
use iter::{Rev, DoubleEndedIterator, ExactSize};
@ -100,7 +102,7 @@ use from_str::FromStr;
use vec;
use vec::{OwnedVector, OwnedCloneableVector, ImmutableVector, MutableVector};
use default::Default;
use send_str::{SendStr, SendStrOwned};
use to_bytes::{IterBytes, Cb};
use unstable::raw::Repr;
/*
@ -915,53 +917,6 @@ macro_rules! utf8_acc_cont_byte(
static TAG_CONT_U8: u8 = 128u8;
/// Enum that represents either a borrowed or an owned string.
#[deriving(Eq,Clone)]
pub enum MaybeOwned<'a> {
/// A borrowed string
Slice(&'a str),
/// An owned string
Owned(~str)
}
impl<'a> Str for MaybeOwned<'a> {
#[inline]
fn as_slice<'b>(&'b self) -> &'b str {
match *self {
Slice(s) => s,
Owned(ref s) => s.as_slice()
}
}
#[inline]
fn into_owned(self) -> ~str {
match self {
Slice(s) => s.to_owned(),
Owned(s) => s
}
}
}
impl<'a> ToStr for MaybeOwned<'a> {
#[inline]
fn to_str(&self) -> ~str {
match *self {
Slice(s) => s.to_str(),
Owned(ref s) => s.clone()
}
}
}
impl<'a> ::fmt::Show for MaybeOwned<'a> {
#[inline]
fn fmt(mo: &MaybeOwned, f: &mut ::fmt::Formatter) -> ::fmt::Result {
match *mo {
Slice(ref s) => ::fmt::Show::fmt(s, f),
Owned(ref s) => ::fmt::Show::fmt(&s.as_slice(), f)
}
}
}
/// Converts a vector of bytes to a new utf-8 string.
/// Any invalid utf-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
///
@ -1083,6 +1038,172 @@ pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> MaybeOwned<'a> {
Owned(res)
}
/*
Section: MaybeOwned
*/
/// A MaybeOwned is a string that can hold either a ~str or a &str.
/// This can be useful as an optimization when an allocation is sometimes
/// needed but not always.
pub enum MaybeOwned<'a> {
/// A borrowed string
Slice(&'a str),
/// An owned string
Owned(~str)
}
/// SendStr is a specialization of `MaybeOwned` to be sendable
pub type SendStr = MaybeOwned<'static>;
impl<'a> MaybeOwned<'a> {
/// Returns `true` if this `MaybeOwned` wraps an owned string
#[inline]
pub fn is_owned(&self) -> bool {
match *self {
Slice(_) => false,
Owned(_) => true
}
}
/// Returns `true` if this `MaybeOwned` wraps a borrowed string
#[inline]
pub fn is_slice(&self) -> bool {
match *self {
Slice(_) => true,
Owned(_) => false
}
}
}
/// Trait for moving into a `MaybeOwned`
pub trait IntoMaybeOwned<'a> {
/// Moves self into a `MaybeOwned`
fn into_maybe_owned(self) -> MaybeOwned<'a>;
}
impl<'a> IntoMaybeOwned<'a> for ~str {
#[inline]
fn into_maybe_owned(self) -> MaybeOwned<'a> { Owned(self) }
}
impl<'a> IntoMaybeOwned<'a> for &'a str {
#[inline]
fn into_maybe_owned(self) -> MaybeOwned<'a> { Slice(self) }
}
impl<'a> IntoMaybeOwned<'a> for MaybeOwned<'a> {
#[inline]
fn into_maybe_owned(self) -> MaybeOwned<'a> { self }
}
impl<'a> ToStr for MaybeOwned<'a> {
#[inline]
fn to_str(&self) -> ~str { self.as_slice().to_owned() }
}
impl<'a> Eq for MaybeOwned<'a> {
#[inline]
fn eq(&self, other: &MaybeOwned) -> bool {
self.as_slice().equals(&other.as_slice())
}
}
impl<'a> TotalEq for MaybeOwned<'a> {
#[inline]
fn equals(&self, other: &MaybeOwned) -> bool {
self.as_slice().equals(&other.as_slice())
}
}
impl<'a> Ord for MaybeOwned<'a> {
#[inline]
fn lt(&self, other: &MaybeOwned) -> bool {
self.as_slice().lt(&other.as_slice())
}
}
impl<'a> TotalOrd for MaybeOwned<'a> {
#[inline]
fn cmp(&self, other: &MaybeOwned) -> Ordering {
self.as_slice().cmp(&other.as_slice())
}
}
impl<'a, S: Str> Equiv<S> for MaybeOwned<'a> {
#[inline]
fn equiv(&self, other: &S) -> bool {
self.as_slice().equals(&other.as_slice())
}
}
impl<'a> Str for MaybeOwned<'a> {
#[inline]
fn as_slice<'b>(&'b self) -> &'b str {
match *self {
Slice(s) => s,
Owned(ref s) => s.as_slice()
}
}
#[inline]
fn into_owned(self) -> ~str {
match self {
Slice(s) => s.to_owned(),
Owned(s) => s
}
}
}
impl<'a> Container for MaybeOwned<'a> {
#[inline]
fn len(&self) -> uint { self.as_slice().len() }
}
impl<'a> Clone for MaybeOwned<'a> {
#[inline]
fn clone(&self) -> MaybeOwned<'a> {
match *self {
Slice(s) => Slice(s),
Owned(ref s) => Owned(s.to_owned())
}
}
}
impl<'a> DeepClone for MaybeOwned<'a> {
#[inline]
fn deep_clone(&self) -> MaybeOwned<'a> {
match *self {
Slice(s) => Slice(s),
Owned(ref s) => Owned(s.to_owned())
}
}
}
impl<'a> Default for MaybeOwned<'a> {
#[inline]
fn default() -> MaybeOwned<'a> { Slice("") }
}
impl<'a> IterBytes for MaybeOwned<'a> {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
match *self {
Slice(s) => s.iter_bytes(lsb0, f),
Owned(ref s) => s.iter_bytes(lsb0, f)
}
}
}
impl<'a> fmt::Show for MaybeOwned<'a> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Slice(ref s) => s.fmt(f),
Owned(ref s) => s.fmt(f)
}
}
}
/// Unsafe operations
pub mod raw {
use cast;
@ -1837,9 +1958,6 @@ pub trait StrSlice<'a> {
/// Converts to a vector of `u16` encoded as UTF-16.
fn to_utf16(&self) -> ~[u16];
/// Copy a slice into a new `SendStr`.
fn to_send_str(&self) -> SendStr;
/// Check that `index`-th byte lies at the start and/or end of a
/// UTF-8 code point sequence.
///
@ -2364,11 +2482,6 @@ impl<'a> StrSlice<'a> for &'a str {
u
}
#[inline]
fn to_send_str(&self) -> SendStr {
SendStrOwned(self.to_owned())
}
#[inline]
fn is_char_boundary(&self, index: uint) -> bool {
if index == self.len() { return true; }
@ -2808,7 +2921,6 @@ mod tests {
use prelude::*;
use ptr;
use str::*;
use send_str::{SendStrOwned, SendStrStatic};
#[test]
fn test_eq() {
@ -4038,17 +4150,76 @@ mod tests {
assert_eq!(from_utf8_lossy(xs), Owned(~"\uFFFD\uFFFD\uFFFDfoo\uFFFD\uFFFD\uFFFDbar"));
}
#[test]
fn test_to_send_str() {
assert_eq!("abcde".to_send_str(), SendStrStatic("abcde"));
assert_eq!("abcde".to_send_str(), SendStrOwned(~"abcde"));
}
#[test]
fn test_from_str() {
let owned: Option<~str> = from_str(&"string");
assert_eq!(owned, Some(~"string"));
}
#[test]
fn test_maybe_owned_traits() {
let s = Slice("abcde");
assert_eq!(s.len(), 5);
assert_eq!(s.as_slice(), "abcde");
assert_eq!(s.to_str(), ~"abcde");
assert!(s.lt(&Owned(~"bcdef")));
assert_eq!(Slice(""), Default::default());
let o = Owned(~"abcde");
assert_eq!(o.len(), 5);
assert_eq!(o.as_slice(), "abcde");
assert_eq!(o.to_str(), ~"abcde");
assert!(o.lt(&Slice("bcdef")));
assert_eq!(Owned(~""), Default::default());
assert_eq!(s.cmp(&o), Equal);
assert!(s.equals(&o));
assert!(s.equiv(&o));
assert_eq!(o.cmp(&s), Equal);
assert!(o.equals(&s));
assert!(o.equiv(&s));
}
#[test]
fn test_maybe_owned_methods() {
let s = Slice("abcde");
assert!(s.is_slice());
assert!(!s.is_owned());
let o = Owned(~"abcde");
assert!(!o.is_slice());
assert!(o.is_owned());
}
#[test]
fn test_maybe_owned_clone() {
assert_eq!(Owned(~"abcde"), Slice("abcde").clone());
assert_eq!(Owned(~"abcde"), Slice("abcde").deep_clone());
assert_eq!(Owned(~"abcde"), Owned(~"abcde").clone());
assert_eq!(Owned(~"abcde"), Owned(~"abcde").deep_clone());
assert_eq!(Slice("abcde"), Slice("abcde").clone());
assert_eq!(Slice("abcde"), Slice("abcde").deep_clone());
assert_eq!(Slice("abcde"), Owned(~"abcde").clone());
assert_eq!(Slice("abcde"), Owned(~"abcde").deep_clone());
}
#[test]
fn test_maybe_owned_into_owned() {
assert_eq!(Slice("abcde").into_owned(), ~"abcde");
assert_eq!(Owned(~"abcde").into_owned(), ~"abcde");
}
#[test]
fn test_into_maybe_owned() {
assert_eq!("abcde".into_maybe_owned(), Slice("abcde"));
assert_eq!((~"abcde").into_maybe_owned(), Slice("abcde"));
assert_eq!("abcde".into_maybe_owned(), Owned(~"abcde"));
assert_eq!((~"abcde").into_maybe_owned(), Owned(~"abcde"));
}
}
#[cfg(test)]

View File

@ -62,8 +62,7 @@ use option::{None, Some, Option};
use result::{Result, Ok, Err};
use rt::local::Local;
use rt::task::Task;
use send_str::{SendStr, IntoSendStr};
use str::Str;
use str::{Str, SendStr, IntoMaybeOwned};
#[cfg(test)] use any::{AnyOwnExt, AnyRefExt};
#[cfg(test)] use comm::SharedChan;
@ -190,8 +189,8 @@ impl TaskBuilder {
/// Name the task-to-be. Currently the name is used for identification
/// only in failure messages.
pub fn name<S: IntoSendStr>(&mut self, name: S) {
self.opts.name = Some(name.into_send_str());
pub fn name<S: IntoMaybeOwned<'static>>(&mut self, name: S) {
self.opts.name = Some(name.into_maybe_owned());
}
/**
@ -396,7 +395,7 @@ fn test_static_named_task() {
#[test]
fn test_send_named_task() {
let mut t = task();
t.name("ada lovelace".into_send_str());
t.name("ada lovelace".into_maybe_owned());
t.spawn(proc() {
with_task_name(|name| {
assert!(name.unwrap() == "ada lovelace");

View File

@ -12,7 +12,7 @@
fn main() {
let mut t = ::std::task::task();
t.name("send name".to_send_str());
t.name("send name".into_maybe_owned());
t.try(proc() {
fail!("test");
3

View File

@ -13,45 +13,44 @@ use std::cmp::{TotalEq, Ord, TotalOrd, Equiv};
use std::cmp::Equal;
use std::container::{Container, Map, MutableMap};
use std::default::Default;
use std::send_str::{SendStr, SendStrOwned, SendStrStatic};
use std::str::Str;
use std::str::{Str, SendStr, Owned, Slice};
use std::to_str::ToStr;
use std::hashmap::HashMap;
use std::option::Some;
pub fn main() {
let mut map: HashMap<SendStr, uint> = HashMap::new();
assert!(map.insert(SendStrStatic("foo"), 42));
assert!(!map.insert(SendStrOwned(~"foo"), 42));
assert!(!map.insert(SendStrStatic("foo"), 42));
assert!(!map.insert(SendStrOwned(~"foo"), 42));
assert!(map.insert(Slice("foo"), 42));
assert!(!map.insert(Owned(~"foo"), 42));
assert!(!map.insert(Slice("foo"), 42));
assert!(!map.insert(Owned(~"foo"), 42));
assert!(!map.insert(SendStrStatic("foo"), 43));
assert!(!map.insert(SendStrOwned(~"foo"), 44));
assert!(!map.insert(SendStrStatic("foo"), 45));
assert!(!map.insert(SendStrOwned(~"foo"), 46));
assert!(!map.insert(Slice("foo"), 43));
assert!(!map.insert(Owned(~"foo"), 44));
assert!(!map.insert(Slice("foo"), 45));
assert!(!map.insert(Owned(~"foo"), 46));
let v = 46;
assert_eq!(map.find(&SendStrOwned(~"foo")), Some(&v));
assert_eq!(map.find(&SendStrStatic("foo")), Some(&v));
assert_eq!(map.find(&Owned(~"foo")), Some(&v));
assert_eq!(map.find(&Slice("foo")), Some(&v));
let (a, b, c, d) = (50, 51, 52, 53);
assert!(map.insert(SendStrStatic("abc"), a));
assert!(map.insert(SendStrOwned(~"bcd"), b));
assert!(map.insert(SendStrStatic("cde"), c));
assert!(map.insert(SendStrOwned(~"def"), d));
assert!(map.insert(Slice("abc"), a));
assert!(map.insert(Owned(~"bcd"), b));
assert!(map.insert(Slice("cde"), c));
assert!(map.insert(Owned(~"def"), d));
assert!(!map.insert(SendStrStatic("abc"), a));
assert!(!map.insert(SendStrOwned(~"bcd"), b));
assert!(!map.insert(SendStrStatic("cde"), c));
assert!(!map.insert(SendStrOwned(~"def"), d));
assert!(!map.insert(Slice("abc"), a));
assert!(!map.insert(Owned(~"bcd"), b));
assert!(!map.insert(Slice("cde"), c));
assert!(!map.insert(Owned(~"def"), d));
assert!(!map.insert(SendStrOwned(~"abc"), a));
assert!(!map.insert(SendStrStatic("bcd"), b));
assert!(!map.insert(SendStrOwned(~"cde"), c));
assert!(!map.insert(SendStrStatic("def"), d));
assert!(!map.insert(Owned(~"abc"), a));
assert!(!map.insert(Slice("bcd"), b));
assert!(!map.insert(Owned(~"cde"), c));
assert!(!map.insert(Slice("def"), d));
assert_eq!(map.find_equiv(&("abc")), Some(&a));
assert_eq!(map.find_equiv(&("bcd")), Some(&b));
@ -63,13 +62,13 @@ pub fn main() {
assert_eq!(map.find_equiv(&(~"cde")), Some(&c));
assert_eq!(map.find_equiv(&(~"def")), Some(&d));
assert_eq!(map.find_equiv(&SendStrStatic("abc")), Some(&a));
assert_eq!(map.find_equiv(&SendStrStatic("bcd")), Some(&b));
assert_eq!(map.find_equiv(&SendStrStatic("cde")), Some(&c));
assert_eq!(map.find_equiv(&SendStrStatic("def")), Some(&d));
assert_eq!(map.find_equiv(&Slice("abc")), Some(&a));
assert_eq!(map.find_equiv(&Slice("bcd")), Some(&b));
assert_eq!(map.find_equiv(&Slice("cde")), Some(&c));
assert_eq!(map.find_equiv(&Slice("def")), Some(&d));
assert_eq!(map.find_equiv(&SendStrOwned(~"abc")), Some(&a));
assert_eq!(map.find_equiv(&SendStrOwned(~"bcd")), Some(&b));
assert_eq!(map.find_equiv(&SendStrOwned(~"cde")), Some(&c));
assert_eq!(map.find_equiv(&SendStrOwned(~"def")), Some(&d));
assert_eq!(map.find_equiv(&Owned(~"abc")), Some(&a));
assert_eq!(map.find_equiv(&Owned(~"bcd")), Some(&b));
assert_eq!(map.find_equiv(&Owned(~"cde")), Some(&c));
assert_eq!(map.find_equiv(&Owned(~"def")), Some(&d));
}

View File

@ -15,57 +15,56 @@ use std::cmp::{TotalEq, Ord, TotalOrd, Equiv};
use std::cmp::Equal;
use std::container::{Container, Map, MutableMap};
use std::default::Default;
use std::send_str::{SendStr, SendStrOwned, SendStrStatic};
use std::str::Str;
use std::str::{Str, SendStr, Owned, Slice};
use std::to_str::ToStr;
use self::collections::TreeMap;
use std::option::Some;
pub fn main() {
let mut map: TreeMap<SendStr, uint> = TreeMap::new();
assert!(map.insert(SendStrStatic("foo"), 42));
assert!(!map.insert(SendStrOwned(~"foo"), 42));
assert!(!map.insert(SendStrStatic("foo"), 42));
assert!(!map.insert(SendStrOwned(~"foo"), 42));
assert!(map.insert(Slice("foo"), 42));
assert!(!map.insert(Owned(~"foo"), 42));
assert!(!map.insert(Slice("foo"), 42));
assert!(!map.insert(Owned(~"foo"), 42));
assert!(!map.insert(SendStrStatic("foo"), 43));
assert!(!map.insert(SendStrOwned(~"foo"), 44));
assert!(!map.insert(SendStrStatic("foo"), 45));
assert!(!map.insert(SendStrOwned(~"foo"), 46));
assert!(!map.insert(Slice("foo"), 43));
assert!(!map.insert(Owned(~"foo"), 44));
assert!(!map.insert(Slice("foo"), 45));
assert!(!map.insert(Owned(~"foo"), 46));
let v = 46;
assert_eq!(map.find(&SendStrOwned(~"foo")), Some(&v));
assert_eq!(map.find(&SendStrStatic("foo")), Some(&v));
assert_eq!(map.find(&Owned(~"foo")), Some(&v));
assert_eq!(map.find(&Slice("foo")), Some(&v));
let (a, b, c, d) = (50, 51, 52, 53);
assert!(map.insert(SendStrStatic("abc"), a));
assert!(map.insert(SendStrOwned(~"bcd"), b));
assert!(map.insert(SendStrStatic("cde"), c));
assert!(map.insert(SendStrOwned(~"def"), d));
assert!(map.insert(Slice("abc"), a));
assert!(map.insert(Owned(~"bcd"), b));
assert!(map.insert(Slice("cde"), c));
assert!(map.insert(Owned(~"def"), d));
assert!(!map.insert(SendStrStatic("abc"), a));
assert!(!map.insert(SendStrOwned(~"bcd"), b));
assert!(!map.insert(SendStrStatic("cde"), c));
assert!(!map.insert(SendStrOwned(~"def"), d));
assert!(!map.insert(Slice("abc"), a));
assert!(!map.insert(Owned(~"bcd"), b));
assert!(!map.insert(Slice("cde"), c));
assert!(!map.insert(Owned(~"def"), d));
assert!(!map.insert(SendStrOwned(~"abc"), a));
assert!(!map.insert(SendStrStatic("bcd"), b));
assert!(!map.insert(SendStrOwned(~"cde"), c));
assert!(!map.insert(SendStrStatic("def"), d));
assert!(!map.insert(Owned(~"abc"), a));
assert!(!map.insert(Slice("bcd"), b));
assert!(!map.insert(Owned(~"cde"), c));
assert!(!map.insert(Slice("def"), d));
assert_eq!(map.find(&SendStrStatic("abc")), Some(&a));
assert_eq!(map.find(&SendStrStatic("bcd")), Some(&b));
assert_eq!(map.find(&SendStrStatic("cde")), Some(&c));
assert_eq!(map.find(&SendStrStatic("def")), Some(&d));
assert_eq!(map.find(&Slice("abc")), Some(&a));
assert_eq!(map.find(&Slice("bcd")), Some(&b));
assert_eq!(map.find(&Slice("cde")), Some(&c));
assert_eq!(map.find(&Slice("def")), Some(&d));
assert_eq!(map.find(&SendStrOwned(~"abc")), Some(&a));
assert_eq!(map.find(&SendStrOwned(~"bcd")), Some(&b));
assert_eq!(map.find(&SendStrOwned(~"cde")), Some(&c));
assert_eq!(map.find(&SendStrOwned(~"def")), Some(&d));
assert_eq!(map.find(&Owned(~"abc")), Some(&a));
assert_eq!(map.find(&Owned(~"bcd")), Some(&b));
assert_eq!(map.find(&Owned(~"cde")), Some(&c));
assert_eq!(map.find(&Owned(~"def")), Some(&d));
assert!(map.pop(&SendStrStatic("foo")).is_some());
assert!(map.pop(&Slice("foo")).is_some());
assert_eq!(map.move_iter().map(|(k, v)| k.to_str() + v.to_str())
.to_owned_vec()
.concat(),