about summary refs log tree commit diff
path: root/src/libstd/str.rs
diff options
context:
space:
mode:
authorMarvin Löbel <loebel.marvin@gmail.com>2013-09-14 19:37:45 +0200
committerMarvin Löbel <loebel.marvin@gmail.com>2013-09-16 16:57:50 +0200
commit76c3e8a38cea2fe6342d83158c267e57a6b1f53f (patch)
treedb7c6ca84a3e769ed40343411ecf43f369883418 /src/libstd/str.rs
parent3e1803f3af1adc1b2e5595650f6920f40bbedc2e (diff)
Add an SendStr type
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.

Possible use cases include Maps with both static and owned keys, or propagating error messages across task boundaries.

SendStr implements most basic traits in a way that hides the fact that it is an enum; in particular things like order and equality are only determined by the content of the wrapped strings.

Replaced std::rt:logging::SendableString with SendStr
Added tests for using an SendStr as key in Hash- and Treemaps
Diffstat (limited to 'src/libstd/str.rs')
-rw-r--r--src/libstd/str.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 0f125280c2d..ccb39c605eb 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -37,6 +37,7 @@ use unstable::raw::{Repr, Slice};
 use vec;
 use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
 use default::Default;
+use send_str::{SendStr, SendStrOwned};
 
 /*
 Section: Conditions
@@ -130,10 +131,12 @@ impl ToStr for ~str {
     #[inline]
     fn to_str(&self) -> ~str { self.to_owned() }
 }
+
 impl<'self> ToStr for &'self str {
     #[inline]
     fn to_str(&self) -> ~str { self.to_owned() }
 }
+
 impl ToStr for @str {
     #[inline]
     fn to_str(&self) -> ~str { self.to_owned() }
@@ -330,7 +333,6 @@ impl<'self> DoubleEndedIterator<char> for CharIterator<'self> {
     }
 }
 
-
 /// External iterator for a string's characters and their byte offsets.
 /// Use with the `std::iterator` module.
 #[deriving(Clone)]
@@ -1355,6 +1357,7 @@ pub trait StrSlice<'self> {
     fn to_owned(&self) -> ~str;
     fn to_managed(&self) -> @str;
     fn to_utf16(&self) -> ~[u16];
+    fn to_send_str(&self) -> SendStr;
     fn is_char_boundary(&self, index: uint) -> bool;
     fn char_range_at(&self, start: uint) -> CharRange;
     fn char_at(&self, i: uint) -> char;
@@ -1869,6 +1872,11 @@ impl<'self> StrSlice<'self> for &'self str {
         u
     }
 
+    #[inline]
+    fn to_send_str(&self) -> SendStr {
+        SendStrOwned(self.to_owned())
+    }
+
     /// Returns false if the index points into the middle of a multi-byte
     /// character sequence.
     #[inline]
@@ -2428,6 +2436,7 @@ mod tests {
     use vec;
     use vec::{Vector, ImmutableVector, CopyableVector};
     use cmp::{TotalOrd, Less, Equal, Greater};
+    use send_str::{SendStrOwned, SendStrStatic};
 
     #[test]
     fn test_eq() {
@@ -3724,6 +3733,12 @@ mod tests {
         let xs = bytes!("hello", 0xff).to_owned();
         assert_eq!(from_utf8_owned_opt(xs), None);
     }
+
+    #[test]
+    fn test_to_send_str() {
+        assert_eq!("abcde".to_send_str(), SendStrStatic("abcde"));
+        assert_eq!("abcde".to_send_str(), SendStrOwned(~"abcde"));
+    }
 }
 
 #[cfg(test)]