Skip to content
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

add AsRef<[u8]> for both str and String #25162

Merged
merged 3 commits into from
May 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
#![stable(feature = "rust1", since = "1.0.0")]

use alloc::boxed::Box;
use core::convert::AsRef;
use core::clone::Clone;
use core::cmp::Ordering::{self, Greater, Less};
use core::cmp::{self, Ord, PartialEq};
Expand Down Expand Up @@ -1024,25 +1023,25 @@ pub trait SliceConcatExt<T: ?Sized> {
fn connect(&self, sep: &T) -> Self::Output;
}

impl<T: Clone, V: AsRef<[T]>> SliceConcatExt<T> for [V] {
impl<T: Clone, V: Borrow<[T]>> SliceConcatExt<T> for [V] {
type Output = Vec<T>;

fn concat(&self) -> Vec<T> {
let size = self.iter().fold(0, |acc, v| acc + v.as_ref().len());
let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
let mut result = Vec::with_capacity(size);
for v in self {
result.push_all(v.as_ref())
result.push_all(v.borrow())
}
result
}

fn connect(&self, sep: &T) -> Vec<T> {
let size = self.iter().fold(0, |acc, v| acc + v.as_ref().len());
let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
let mut result = Vec::with_capacity(size + self.len());
let mut first = true;
for v in self {
if first { first = false } else { result.push(sep.clone()) }
result.push_all(v.as_ref())
result.push_all(v.borrow())
}
result
}
Expand Down
11 changes: 5 additions & 6 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ use core::str::pattern::Pattern;
use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
use rustc_unicode::str::{UnicodeStr, Utf16Encoder};

use core::convert::AsRef;
use vec_deque::VecDeque;
use borrow::{Borrow, ToOwned};
use string::String;
Expand All @@ -83,7 +82,7 @@ pub use core::str::pattern;
Section: Creating a string
*/

impl<S: AsRef<str>> SliceConcatExt<str> for [S] {
impl<S: Borrow<str>> SliceConcatExt<str> for [S] {
type Output = String;

fn concat(&self) -> String {
Expand All @@ -92,11 +91,11 @@ impl<S: AsRef<str>> SliceConcatExt<str> for [S] {
}

// `len` calculation may overflow but push_str will check boundaries
let len = self.iter().map(|s| s.as_ref().len()).sum();
let len = self.iter().map(|s| s.borrow().len()).sum();
let mut result = String::with_capacity(len);

for s in self {
result.push_str(s.as_ref())
result.push_str(s.borrow())
}

result
Expand All @@ -115,7 +114,7 @@ impl<S: AsRef<str>> SliceConcatExt<str> for [S] {
// this is wrong without the guarantee that `self` is non-empty
// `len` calculation may overflow but push_str but will check boundaries
let len = sep.len() * (self.len() - 1)
+ self.iter().map(|s| s.as_ref().len()).sum::<usize>();
+ self.iter().map(|s| s.borrow().len()).sum::<usize>();
let mut result = String::with_capacity(len);
let mut first = true;

Expand All @@ -125,7 +124,7 @@ impl<S: AsRef<str>> SliceConcatExt<str> for [S] {
} else {
result.push_str(sep);
}
result.push_str(s.as_ref());
result.push_str(s.borrow());
}
result
}
Expand Down
8 changes: 8 additions & 0 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,14 @@ impl AsRef<str> for String {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<[u8]> for String {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> From<&'a str> for String {
#[cfg(not(test))]
Expand Down
9 changes: 9 additions & 0 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
use char::CharExt;
use clone::Clone;
use cmp::{self, Eq};
use convert::AsRef;
use default::Default;
use fmt;
use iter::ExactSizeIterator;
Expand Down Expand Up @@ -1842,6 +1843,14 @@ impl StrExt for str {
fn parse<T: FromStr>(&self) -> Result<T, T::Err> { FromStr::from_str(self) }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<[u8]> for str {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

/// Pluck a code point out of a UTF-8-like byte slice and return the
/// index of the next code point.
#[inline]
Expand Down