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
This commit is contained in:
Marvin Löbel
2013-09-14 19:37:45 +02:00
parent 3e1803f3af
commit 76c3e8a38c
9 changed files with 406 additions and 16 deletions

View File

@@ -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)]