From aaa3641754543d6891c872e3be6667cfbec7b741 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 6 May 2015 15:53:34 -0700 Subject: [PATCH 1/3] core: impl AsRef<[u8]> for str --- src/libcore/str/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index c9bbcba31e9de..c10e1443cfcbe 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -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; @@ -1842,6 +1843,14 @@ impl StrExt for str { fn parse(&self) -> Result { 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] From 093ebd5a62d77678046b5e56420d46df37a97129 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 6 May 2015 15:53:53 -0700 Subject: [PATCH 2/3] collections: impl AsRef<[u8]> for String --- src/libcollections/string.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 3c668f7fe9bc6..52d72501b4a9b 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1057,6 +1057,14 @@ impl AsRef 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))] From 8e491ef0193f48fc000ff62c75544f8dcaa6548d Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 8 May 2015 17:13:35 -0700 Subject: [PATCH 3/3] collections: change bounds of SliceConcatExt implementations to use Borrow instead of AsRef --- src/libcollections/slice.rs | 11 +++++------ src/libcollections/str.rs | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 8eb7995c42218..b9e9800f7a0e7 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -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}; @@ -1024,25 +1023,25 @@ pub trait SliceConcatExt { fn connect(&self, sep: &T) -> Self::Output; } -impl> SliceConcatExt for [V] { +impl> SliceConcatExt for [V] { type Output = Vec; fn concat(&self) -> Vec { - 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 { - 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 } diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index da1b4dcddfc38..baef6ba6f01f3 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -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; @@ -83,7 +82,7 @@ pub use core::str::pattern; Section: Creating a string */ -impl> SliceConcatExt for [S] { +impl> SliceConcatExt for [S] { type Output = String; fn concat(&self) -> String { @@ -92,11 +91,11 @@ impl> SliceConcatExt 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 @@ -115,7 +114,7 @@ impl> SliceConcatExt 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::(); + + self.iter().map(|s| s.borrow().len()).sum::(); let mut result = String::with_capacity(len); let mut first = true; @@ -125,7 +124,7 @@ impl> SliceConcatExt for [S] { } else { result.push_str(sep); } - result.push_str(s.as_ref()); + result.push_str(s.borrow()); } result }