Skip to content

Commit b9b396c

Browse files
committed
auto merge of #18463 : japaric/rust/bytes2, r=alexcrichton
- The `BytesContainer::container_into_owned_bytes` method has been removed - Methods that used to take `BytesContainer` implementors by value, now take them by reference. In particular, this breaks some uses of Path: ``` rust Path::new("foo") // Still works path.join(another_path) -> path.join(&another_path) ``` [breaking-change] --- Re: `container_into_owned_bytes`, I've removed it because - Nothing in the whole repository uses it - Takes `self` by value, which is incompatible with unsized types (`str`) The alternative to removing this method is to split `BytesContainer` into `BytesContainer for Sized?` and `SizedBytesContainer: BytesContainer + Sized`, where the second trait only contains the `container_into_owned_bytes` method. I tried this alternative [in another branch](https://github.com/japaric/rust/commits/bytes) and it works, but it seemed better not to create a new trait for an unused method. Re: Breakage of `Path` methods We could use the idea that @alexcrichton proposed in #18457 (add blanket `impl BytesContainer for &T where T: BytesContainer` + keep taking `T: BytesContainer` by value in `Path` methods) to avoid breaking any code. r? @aturon cc #16918
2 parents 851799d + fe256f8 commit b9b396c

File tree

8 files changed

+36
-59
lines changed

8 files changed

+36
-59
lines changed

Diff for: src/compiletest/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ fn find_rust_src_root(config: &Config) -> Option<Path> {
627627
let path_postfix = Path::new("src/etc/lldb_batchmode.py");
628628

629629
while path.pop() {
630-
if path.join(path_postfix.clone()).is_file() {
630+
if path.join(&path_postfix).is_file() {
631631
return Some(path);
632632
}
633633
}

Diff for: src/librustc/driver/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub fn build_session_(sopts: config::Options,
231231
if path.is_absolute() {
232232
path.clone()
233233
} else {
234-
os::getcwd().join(path.clone())
234+
os::getcwd().join(&path)
235235
}
236236
);
237237

Diff for: src/libstd/io/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ mod tests {
918918
let prog = pwd_cmd().cwd(&parent_dir).spawn().unwrap();
919919

920920
let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
921-
let child_dir = Path::new(output.as_slice().trim().into_string());
921+
let child_dir = Path::new(output.as_slice().trim());
922922

923923
let parent_stat = parent_dir.stat().unwrap();
924924
let child_stat = child_dir.stat().unwrap();

Diff for: src/libstd/path/mod.rs

+30-25
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ println!("path exists: {}", path.exists());
6767

6868
#![experimental]
6969

70+
use core::kinds::Sized;
7071
use c_str::CString;
7172
use clone::Clone;
7273
use fmt;
@@ -626,7 +627,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
626627
/// ```
627628
#[inline]
628629
fn push_many<T: BytesContainer>(&mut self, paths: &[T]) {
629-
let t: Option<T> = None;
630+
let t: Option<&T> = None;
630631
if BytesContainer::is_str(t) {
631632
for p in paths.iter() {
632633
self.push(p.container_as_str().unwrap())
@@ -791,14 +792,9 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
791792
}
792793

793794
/// A trait that represents something bytes-like (e.g. a &[u8] or a &str)
794-
pub trait BytesContainer {
795+
pub trait BytesContainer for Sized? {
795796
/// Returns a &[u8] representing the receiver
796797
fn container_as_bytes<'a>(&'a self) -> &'a [u8];
797-
/// Consumes the receiver and converts it into Vec<u8>
798-
#[inline]
799-
fn container_into_owned_bytes(self) -> Vec<u8> {
800-
self.container_as_bytes().to_vec()
801-
}
802798
/// Returns the receiver interpreted as a utf-8 string, if possible
803799
#[inline]
804800
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
@@ -807,7 +803,7 @@ pub trait BytesContainer {
807803
/// Returns whether .container_as_str() is guaranteed to not fail
808804
// FIXME (#8888): Remove unused arg once ::<for T> works
809805
#[inline]
810-
fn is_str(_: Option<Self>) -> bool { false }
806+
fn is_str(_: Option<&Self>) -> bool { false }
811807
}
812808

813809
/// A trait that represents the unsafe operations on GenericPaths
@@ -859,48 +855,44 @@ impl<'a, P: GenericPath> Display<'a, P> {
859855
}
860856
}
861857

862-
impl<'a> BytesContainer for &'a str {
858+
impl BytesContainer for str {
863859
#[inline]
864-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
860+
fn container_as_bytes(&self) -> &[u8] {
865861
self.as_bytes()
866862
}
867863
#[inline]
868-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
869-
Some(*self)
864+
fn container_as_str(&self) -> Option<&str> {
865+
Some(self)
870866
}
871867
#[inline]
872-
fn is_str(_: Option<&'a str>) -> bool { true }
868+
fn is_str(_: Option<&str>) -> bool { true }
873869
}
874870

875871
impl BytesContainer for String {
876872
#[inline]
877-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
873+
fn container_as_bytes(&self) -> &[u8] {
878874
self.as_bytes()
879875
}
880876
#[inline]
881-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
877+
fn container_as_str(&self) -> Option<&str> {
882878
Some(self.as_slice())
883879
}
884880
#[inline]
885-
fn is_str(_: Option<String>) -> bool { true }
881+
fn is_str(_: Option<&String>) -> bool { true }
886882
}
887883

888-
impl<'a> BytesContainer for &'a [u8] {
884+
impl BytesContainer for [u8] {
889885
#[inline]
890-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
891-
*self
886+
fn container_as_bytes(&self) -> &[u8] {
887+
self
892888
}
893889
}
894890

895891
impl BytesContainer for Vec<u8> {
896892
#[inline]
897-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
893+
fn container_as_bytes(&self) -> &[u8] {
898894
self.as_slice()
899895
}
900-
#[inline]
901-
fn container_into_owned_bytes(self) -> Vec<u8> {
902-
self
903-
}
904896
}
905897

906898
impl BytesContainer for CString {
@@ -920,7 +912,20 @@ impl<'a> BytesContainer for str::MaybeOwned<'a> {
920912
Some(self.as_slice())
921913
}
922914
#[inline]
923-
fn is_str(_: Option<str::MaybeOwned>) -> bool { true }
915+
fn is_str(_: Option<&str::MaybeOwned>) -> bool { true }
916+
}
917+
918+
impl<'a, Sized? T: BytesContainer> BytesContainer for &'a T {
919+
#[inline]
920+
fn container_as_bytes(&self) -> &[u8] {
921+
(**self).container_as_bytes()
922+
}
923+
#[inline]
924+
fn container_as_str(&self) -> Option<&str> {
925+
(**self).container_as_str()
926+
}
927+
#[inline]
928+
fn is_str(_: Option<& &'a T>) -> bool { BytesContainer::is_str(None::<&T>) }
924929
}
925930

926931
#[inline(always)]

Diff for: src/libstd/path/posix.rs

-11
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,6 @@ impl BytesContainer for Path {
117117
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
118118
self.as_vec()
119119
}
120-
#[inline]
121-
fn container_into_owned_bytes(self) -> Vec<u8> {
122-
self.into_vec()
123-
}
124-
}
125-
126-
impl<'a> BytesContainer for &'a Path {
127-
#[inline]
128-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
129-
self.as_vec()
130-
}
131120
}
132121

133122
impl GenericPathUnsafe for Path {

Diff for: src/libstd/path/windows.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,6 @@ impl<S: hash::Writer> hash::Hash<S> for Path {
144144
}
145145

146146
impl BytesContainer for Path {
147-
#[inline]
148-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
149-
self.as_vec()
150-
}
151-
#[inline]
152-
fn container_into_owned_bytes(self) -> Vec<u8> {
153-
self.into_vec()
154-
}
155-
#[inline]
156-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
157-
self.as_str()
158-
}
159-
#[inline]
160-
fn is_str(_: Option<Path>) -> bool { true }
161-
}
162-
163-
impl<'a> BytesContainer for &'a Path {
164147
#[inline]
165148
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
166149
self.as_vec()
@@ -170,7 +153,7 @@ impl<'a> BytesContainer for &'a Path {
170153
self.as_str()
171154
}
172155
#[inline]
173-
fn is_str(_: Option<&'a Path>) -> bool { true }
156+
fn is_str(_: Option<&Path>) -> bool { true }
174157
}
175158

176159
impl GenericPathUnsafe for Path {

Diff for: src/libterm/terminfo/searcher.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn get_dbpath_for_term(term: &str) -> Option<Box<Path>> {
4141
if i == "" {
4242
dirs_to_search.push(Path::new("/usr/share/terminfo"));
4343
} else {
44-
dirs_to_search.push(Path::new(i.to_string()));
44+
dirs_to_search.push(Path::new(i));
4545
}
4646
},
4747
// Found nothing in TERMINFO_DIRS, use the default paths:

Diff for: src/test/run-pass/process-spawn-with-unicode-params.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() {
4848

4949
let child_filestem = Path::new(child_name);
5050
let child_filename = child_filestem.with_extension(my_ext);
51-
let child_path = cwd.join(child_filename.clone());
51+
let child_path = cwd.join(child_filename);
5252

5353
// make a separate directory for the child
5454
drop(fs::mkdir(&cwd, io::USER_RWX).is_ok());

0 commit comments

Comments
 (0)