Skip to content

Commit

Permalink
Deprecate str::from_chars
Browse files Browse the repository at this point in the history
Use `String::from_chars` instead

[breaking-change]
  • Loading branch information
aochagavia committed Jul 15, 2014
1 parent 211f1ca commit 20a6894
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
c
}
} ).collect();
str::from_chars(c.as_slice()).to_string()
String::from_chars(c.as_slice())
}

#[cfg(target_os = "win32")]
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ pub fn from_char(ch: char) -> String {
/// # Example
///
/// ```rust
/// use std::str;
/// let chars = ['h', 'e', 'l', 'l', 'o'];
/// let string = str::from_chars(chars);
/// let string = String::from_chars(chars);
/// assert_eq!(string.as_slice(), "hello");
/// ```
#[deprecated = "use String::from_chars instead"]
pub fn from_chars(chs: &[char]) -> String {
chs.iter().map(|c| *c).collect()
}
Expand Down
14 changes: 14 additions & 0 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ impl String {
Err(vec)
}
}

/// Convert a vector of chars to a string
///
/// # Example
///
/// ```rust
/// let chars = ['h', 'e', 'l', 'l', 'o'];
/// let string = String::from_chars(chars);
/// assert_eq!(string.as_slice(), "hello");
/// ```
#[inline]
pub fn from_chars(chs: &[char]) -> String {
chs.iter().map(|c| *c).collect()
}

/// Return the underlying byte buffer, encoded as UTF-8.
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/libregex/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl<'a> Parser<'a> {
};
self.chari = closer;
let greed = try!(self.get_next_greedy());
let inner = str::from_chars(
let inner = String::from_chars(
self.chars.as_slice().slice(start + 1, closer));

// Parse the min and max values from the regex.
Expand Down Expand Up @@ -944,7 +944,7 @@ impl<'a> Parser<'a> {
}

fn slice(&self, start: uint, end: uint) -> String {
str::from_chars(self.chars.as_slice().slice(start, end)).to_string()
String::from_chars(self.chars.as_slice().slice(start, end))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libuuid/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ mod test {
let hs = uuid1.to_hyphenated_str();
let ss = uuid1.to_string();

let hsn = str::from_chars(hs.as_slice()
let hsn = String::from_chars(hs.as_slice()
.chars()
.filter(|&c| c != '-')
.collect::<Vec<char>>()
Expand Down
4 changes: 1 addition & 3 deletions src/test/run-pass/issue-3563-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ impl fmt::Show for AsciiArt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Convert each line into a string.
let lines = self.lines.iter()
.map(|line| {
str::from_chars(line.as_slice()).to_string()
})
.map(|line| String::from_chars(line.as_slice()))
.collect::<Vec<String>>();

// Concatenate the lines together using a new-line.
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/utf8_chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use std::str;
pub fn main() {
// Chars of 1, 2, 3, and 4 bytes
let chs: Vec<char> = vec!('e', 'é', '€', '\U00010000');
let s: String = str::from_chars(chs.as_slice()).to_string();
let s: String = String::from_chars(chs.as_slice()).to_string();
let schs: Vec<char> = s.as_slice().chars().collect();

assert!(s.len() == 10u);
assert!(s.as_slice().char_len() == 4u);
assert!(schs.len() == 4u);
assert!(str::from_chars(schs.as_slice()).to_string() == s);
assert!(String::from_chars(schs.as_slice()) == s);
assert!(s.as_slice().char_at(0u) == 'e');
assert!(s.as_slice().char_at(1u) == 'é');

Expand Down

0 comments on commit 20a6894

Please sign in to comment.