Skip to content

Commit c033d98

Browse files
committed
Auto merge of #25162 - seanmonstar:asref-bytes, r=alexcrichton
r? @aturon
2 parents d3958c6 + 8e491ef commit c033d98

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

src/libcollections/slice.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
#![stable(feature = "rust1", since = "1.0.0")]
8181

8282
use alloc::boxed::Box;
83-
use core::convert::AsRef;
8483
use core::clone::Clone;
8584
use core::cmp::Ordering::{self, Greater, Less};
8685
use core::cmp::{self, Ord, PartialEq};
@@ -1024,25 +1023,25 @@ pub trait SliceConcatExt<T: ?Sized> {
10241023
fn connect(&self, sep: &T) -> Self::Output;
10251024
}
10261025

1027-
impl<T: Clone, V: AsRef<[T]>> SliceConcatExt<T> for [V] {
1026+
impl<T: Clone, V: Borrow<[T]>> SliceConcatExt<T> for [V] {
10281027
type Output = Vec<T>;
10291028

10301029
fn concat(&self) -> Vec<T> {
1031-
let size = self.iter().fold(0, |acc, v| acc + v.as_ref().len());
1030+
let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
10321031
let mut result = Vec::with_capacity(size);
10331032
for v in self {
1034-
result.push_all(v.as_ref())
1033+
result.push_all(v.borrow())
10351034
}
10361035
result
10371036
}
10381037

10391038
fn connect(&self, sep: &T) -> Vec<T> {
1040-
let size = self.iter().fold(0, |acc, v| acc + v.as_ref().len());
1039+
let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
10411040
let mut result = Vec::with_capacity(size + self.len());
10421041
let mut first = true;
10431042
for v in self {
10441043
if first { first = false } else { result.push(sep.clone()) }
1045-
result.push_all(v.as_ref())
1044+
result.push_all(v.borrow())
10461045
}
10471046
result
10481047
}

src/libcollections/str.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use core::str::pattern::Pattern;
5959
use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
6060
use rustc_unicode::str::{UnicodeStr, Utf16Encoder};
6161

62-
use core::convert::AsRef;
6362
use vec_deque::VecDeque;
6463
use borrow::{Borrow, ToOwned};
6564
use string::String;
@@ -83,7 +82,7 @@ pub use core::str::pattern;
8382
Section: Creating a string
8483
*/
8584

86-
impl<S: AsRef<str>> SliceConcatExt<str> for [S] {
85+
impl<S: Borrow<str>> SliceConcatExt<str> for [S] {
8786
type Output = String;
8887

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

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

9897
for s in self {
99-
result.push_str(s.as_ref())
98+
result.push_str(s.borrow())
10099
}
101100

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

@@ -125,7 +124,7 @@ impl<S: AsRef<str>> SliceConcatExt<str> for [S] {
125124
} else {
126125
result.push_str(sep);
127126
}
128-
result.push_str(s.as_ref());
127+
result.push_str(s.borrow());
129128
}
130129
result
131130
}

src/libcollections/string.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,14 @@ impl AsRef<str> for String {
10571057
}
10581058
}
10591059

1060+
#[stable(feature = "rust1", since = "1.0.0")]
1061+
impl AsRef<[u8]> for String {
1062+
#[inline]
1063+
fn as_ref(&self) -> &[u8] {
1064+
self.as_bytes()
1065+
}
1066+
}
1067+
10601068
#[stable(feature = "rust1", since = "1.0.0")]
10611069
impl<'a> From<&'a str> for String {
10621070
#[cfg(not(test))]

src/libcore/str/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
2121
use char::CharExt;
2222
use clone::Clone;
2323
use cmp::{self, Eq};
24+
use convert::AsRef;
2425
use default::Default;
2526
use fmt;
2627
use iter::ExactSizeIterator;
@@ -1842,6 +1843,14 @@ impl StrExt for str {
18421843
fn parse<T: FromStr>(&self) -> Result<T, T::Err> { FromStr::from_str(self) }
18431844
}
18441845

1846+
#[stable(feature = "rust1", since = "1.0.0")]
1847+
impl AsRef<[u8]> for str {
1848+
#[inline]
1849+
fn as_ref(&self) -> &[u8] {
1850+
self.as_bytes()
1851+
}
1852+
}
1853+
18451854
/// Pluck a code point out of a UTF-8-like byte slice and return the
18461855
/// index of the next code point.
18471856
#[inline]

0 commit comments

Comments
 (0)