|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! `SendStr` definition and trait implementations |
| 12 | +
|
| 13 | +use clone::{Clone, DeepClone}; |
| 14 | +use cmp::{Eq, TotalEq, Ord, TotalOrd, Equiv}; |
| 15 | +use cmp::{Ordering, Less}; |
| 16 | +use container::Container; |
| 17 | +use default::Default; |
| 18 | +use str::{Str, StrSlice}; |
| 19 | +use to_str::ToStr; |
| 20 | +use to_bytes::{IterBytes, Cb}; |
| 21 | + |
| 22 | +/// A SendStr is a string that can hold either a ~str or a &'static str. |
| 23 | +/// This can be useful as an optimization when an allocation is sometimes |
| 24 | +/// needed but the common case is statically known. |
| 25 | +pub enum SendStr { |
| 26 | + SendStrOwned(~str), |
| 27 | + SendStrStatic(&'static str) |
| 28 | +} |
| 29 | + |
| 30 | +impl SendStr { |
| 31 | + /// Returns `true` if this `SendStr` wraps an owned string |
| 32 | + #[inline] |
| 33 | + pub fn is_owned(&self) -> bool { |
| 34 | + match *self { |
| 35 | + SendStrOwned(_) => true, |
| 36 | + SendStrStatic(_) => false |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// Returns `true` if this `SendStr` wraps an static string |
| 41 | + #[inline] |
| 42 | + pub fn is_static(&self) -> bool { |
| 43 | + match *self { |
| 44 | + SendStrOwned(_) => false, |
| 45 | + SendStrStatic(_) => true |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Trait for moving into an `SendStr` |
| 51 | +pub trait IntoSendStr { |
| 52 | + /// Moves self into an `SendStr` |
| 53 | + fn into_send_str(self) -> SendStr; |
| 54 | +} |
| 55 | + |
| 56 | +impl IntoSendStr for ~str { |
| 57 | + #[inline] |
| 58 | + fn into_send_str(self) -> SendStr { SendStrOwned(self) } |
| 59 | +} |
| 60 | + |
| 61 | +impl IntoSendStr for &'static str { |
| 62 | + #[inline] |
| 63 | + fn into_send_str(self) -> SendStr { SendStrStatic(self) } |
| 64 | +} |
| 65 | + |
| 66 | +/* |
| 67 | +Section: string trait impls |
| 68 | +
|
| 69 | +`SendStr `should behave like a normal string, so we don't derive. |
| 70 | +*/ |
| 71 | + |
| 72 | +impl ToStr for SendStr { |
| 73 | + #[inline] |
| 74 | + fn to_str(&self) -> ~str { self.as_slice().to_owned() } |
| 75 | +} |
| 76 | + |
| 77 | +impl Eq for SendStr { |
| 78 | + #[inline] |
| 79 | + fn eq(&self, other: &SendStr) -> bool { |
| 80 | + self.as_slice().equals(&other.as_slice()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl TotalEq for SendStr { |
| 85 | + #[inline] |
| 86 | + fn equals(&self, other: &SendStr) -> bool { |
| 87 | + self.as_slice().equals(&other.as_slice()) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl Ord for SendStr { |
| 92 | + #[inline] |
| 93 | + fn lt(&self, other: &SendStr) -> bool { self.cmp(other) == Less } |
| 94 | +} |
| 95 | + |
| 96 | +impl TotalOrd for SendStr { |
| 97 | + #[inline] |
| 98 | + fn cmp(&self, other: &SendStr) -> Ordering { |
| 99 | + self.as_slice().cmp(&other.as_slice()) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl<'self, S: Str> Equiv<S> for SendStr { |
| 104 | + #[inline] |
| 105 | + fn equiv(&self, other: &S) -> bool { |
| 106 | + self.as_slice().equals(&other.as_slice()) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl Str for SendStr { |
| 111 | + #[inline] |
| 112 | + fn as_slice<'r>(&'r self) -> &'r str { |
| 113 | + match *self { |
| 114 | + SendStrOwned(ref s) => s.as_slice(), |
| 115 | + // XXX: Borrowchecker doesn't recognize lifetime as static unless prompted |
| 116 | + // SendStrStatic(s) => s.as_slice() |
| 117 | + SendStrStatic(s) => {let tmp: &'static str = s; tmp} |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + #[inline] |
| 122 | + fn into_owned(self) -> ~str { |
| 123 | + match self { |
| 124 | + SendStrOwned(s) => s, |
| 125 | + SendStrStatic(s) => s.to_owned() |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl Container for SendStr { |
| 131 | + #[inline] |
| 132 | + fn len(&self) -> uint { self.as_slice().len() } |
| 133 | +} |
| 134 | + |
| 135 | +impl Clone for SendStr { |
| 136 | + #[inline] |
| 137 | + fn clone(&self) -> SendStr { |
| 138 | + match *self { |
| 139 | + SendStrOwned(ref s) => SendStrOwned(s.to_owned()), |
| 140 | + SendStrStatic(s) => SendStrStatic(s) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl DeepClone for SendStr { |
| 146 | + #[inline] |
| 147 | + fn deep_clone(&self) -> SendStr { |
| 148 | + match *self { |
| 149 | + SendStrOwned(ref s) => SendStrOwned(s.to_owned()), |
| 150 | + SendStrStatic(s) => SendStrStatic(s) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl Default for SendStr { |
| 156 | + #[inline] |
| 157 | + fn default() -> SendStr { SendStrStatic("") } |
| 158 | +} |
| 159 | + |
| 160 | +impl IterBytes for SendStr { |
| 161 | + #[inline] |
| 162 | + fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool { |
| 163 | + match *self { |
| 164 | + SendStrOwned(ref s) => s.iter_bytes(lsb0, f), |
| 165 | + SendStrStatic(s) => s.iter_bytes(lsb0, f) |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +#[cfg(test)] |
| 171 | +mod tests { |
| 172 | + use clone::{Clone, DeepClone}; |
| 173 | + use cmp::{TotalEq, Ord, TotalOrd, Equiv}; |
| 174 | + use cmp::Equal; |
| 175 | + use container::Container; |
| 176 | + use default::Default; |
| 177 | + use send_str::{SendStrOwned, SendStrStatic}; |
| 178 | + use str::Str; |
| 179 | + use to_str::ToStr; |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_send_str() { |
| 183 | + let s = SendStrStatic("abcde"); |
| 184 | + assert_eq!(s.len(), 5); |
| 185 | + assert_eq!(s.as_slice(), "abcde"); |
| 186 | + assert_eq!(s.to_str(), ~"abcde"); |
| 187 | + assert!(s.equiv(&@"abcde")); |
| 188 | + assert!(s.lt(&SendStrOwned(~"bcdef"))); |
| 189 | + assert_eq!(SendStrStatic(""), Default::default()); |
| 190 | + assert!(s.is_static()); |
| 191 | + assert!(!s.is_owned()); |
| 192 | +
|
| 193 | + assert_eq!(s.clone(), s.clone()); |
| 194 | + assert_eq!(s.clone().into_owned(), ~"abcde"); |
| 195 | + assert_eq!(s.clone().deep_clone(), s.clone()); |
| 196 | +
|
| 197 | + let o = SendStrOwned(~"abcde"); |
| 198 | + assert_eq!(o.len(), 5); |
| 199 | + assert_eq!(o.as_slice(), "abcde"); |
| 200 | + assert_eq!(o.to_str(), ~"abcde"); |
| 201 | + assert!(o.equiv(&@"abcde")); |
| 202 | + assert!(o.lt(&SendStrStatic("bcdef"))); |
| 203 | + assert_eq!(SendStrOwned(~""), Default::default()); |
| 204 | + assert!(!o.is_static()); |
| 205 | + assert!(o.is_owned()); |
| 206 | +
|
| 207 | + assert_eq!(o.clone(), o.clone()); |
| 208 | + assert_eq!(o.clone().into_owned(), ~"abcde"); |
| 209 | + assert_eq!(o.clone().deep_clone(), o.clone()); |
| 210 | +
|
| 211 | + assert_eq!(s.cmp(&o), Equal); |
| 212 | + assert!(s.equals(&o)); |
| 213 | + assert!(s.equiv(&o)); |
| 214 | + assert_eq!(o.cmp(&s), Equal); |
| 215 | + assert!(o.equals(&s)); |
| 216 | + assert!(o.equiv(&s)); |
| 217 | +
|
| 218 | + assert_eq!("abcde".into_send_str(), SendStrStatic("abcde")); |
| 219 | + assert_eq!((~"abcde").into_send_str(), SendStrStatic("abcde")); |
| 220 | + assert_eq!("abcde".into_send_str(), SendStrOwned(~"abcde")); |
| 221 | + assert_eq!((~"abcde").into_send_str(), SendStrOwned(~"abcde")); |
| 222 | + } |
| 223 | +} |
0 commit comments