-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add an SendStr type #9192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add an SendStr type #9192
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
// Copyright 2013 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. | ||
|
||
//! `SendStr` definition and trait implementations | ||
|
||
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. | ||
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 an 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) } | ||
} | ||
|
||
/* | ||
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<'self, 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(), | ||
// XXX: 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 clone::{Clone, DeepClone}; | ||
use cmp::{TotalEq, Ord, TotalOrd, Equiv}; | ||
use cmp::Equal; | ||
use container::Container; | ||
use default::Default; | ||
use send_str::{SendStrOwned, SendStrStatic}; | ||
use str::Str; | ||
use to_str::ToStr; | ||
|
||
#[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.equiv(&@"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.equiv(&@"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")); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this go in
str/send_str.rs
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest I'm unsure if grouping similar module in their own subfolders is actually desired - As is it doesn't match the way module source file lookup looks for them (hence the path attribute).
I originally introduced that
str
folder together withascii.rs
when I understood the module system far less than today. The only other modules that do this are the modules in thenum
directory (also introduced by me :| ), but those at least have the excuse of being horrible repetitive and macro generated.If grouping string related modules into their own directory is desirable, even if it doesn't mirror the way module hierarchy source file lookup works, then I'd have to make a separate commit for also moving a bunch of other modules anyway to be consistent. Maybe I should make an issue about this.