Add impl IntoStr for ::std::vec_ng::Vec<Ascii>

This commit is contained in:
Eunchong Yu 2014-03-17 15:30:27 +09:00 committed by Alex Crichton
parent 2265512033
commit e627bce939

View file

@ -20,6 +20,7 @@ use cast;
use fmt;
use iter::Iterator;
use vec::{ImmutableVector, MutableVector, Vector};
use vec_ng::Vec;
use option::{Option, Some, None};
/// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero.
@ -305,6 +306,14 @@ impl IntoStr for ~[Ascii] {
}
}
impl IntoStr for Vec<Ascii> {
#[inline]
fn into_str(self) -> ~str {
let v: ~[Ascii] = self.move_iter().collect();
unsafe { cast::transmute(v) }
}
}
/// Trait to convert to an owned byte array by consuming self
pub trait IntoBytes {
/// Converts to an owned byte array by consuming self
@ -473,6 +482,7 @@ mod tests {
use super::*;
use str::from_char;
use char::from_u32;
use vec_ng::Vec;
macro_rules! v2ascii (
( [$($e:expr),*]) => (&[$(Ascii{chr:$e}),*]);
@ -480,6 +490,10 @@ mod tests {
(~[$($e:expr),*]) => (~[$(Ascii{chr:$e}),*]);
)
macro_rules! vec2ascii (
($($e:expr),*) => (Vec::from_slice([$(Ascii{chr:$e}),*]));
)
#[test]
fn test_ascii() {
assert_eq!(65u8.to_ascii().to_byte(), 65u8);
@ -535,6 +549,17 @@ mod tests {
}
#[test]
fn test_ascii_vec_ng() {
assert_eq!(Vec::from_slice("abCDef&?#".to_ascii().to_lower()).into_str(), ~"abcdef&?#");
assert_eq!(Vec::from_slice("abCDef&?#".to_ascii().to_upper()).into_str(), ~"ABCDEF&?#");
assert_eq!(Vec::from_slice("".to_ascii().to_lower()).into_str(), ~"");
assert_eq!(Vec::from_slice("YMCA".to_ascii().to_lower()).into_str(), ~"ymca");
assert_eq!(Vec::from_slice("abcDEFxyz:.;".to_ascii().to_upper()).into_str(),
~"ABCDEFXYZ:.;");
}
#[test]
fn test_owned_ascii_vec() {
assert_eq!((~"( ;").into_ascii(), v2ascii!(~[40, 32, 59]));
@ -550,6 +575,7 @@ mod tests {
#[test]
fn test_ascii_into_str() {
assert_eq!(v2ascii!(~[40, 32, 59]).into_str(), ~"( ;");
assert_eq!(vec2ascii!(40, 32, 59).into_str(), ~"( ;");
}
#[test]