Skip to content

Override ToOwned::clone_into for Path and OsStr #41390

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

Merged
merged 1 commit into from
Apr 20, 2017
Merged
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
18 changes: 17 additions & 1 deletion src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
@@ -677,7 +677,13 @@ impl Borrow<OsStr> for OsString {
#[stable(feature = "rust1", since = "1.0.0")]
impl ToOwned for OsStr {
type Owned = OsString;
fn to_owned(&self) -> OsString { self.to_os_string() }
fn to_owned(&self) -> OsString {
self.to_os_string()
}
fn clone_into(&self, target: &mut OsString) {
target.clear();
target.push(self);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
@@ -863,4 +869,14 @@ mod tests {
let boxed = <Box<OsStr>>::default();
assert!(boxed.is_empty());
}

#[test]
fn test_os_str_clone_into() {
let mut os_string = OsString::with_capacity(123);
os_string.push("hello");
let os_str = OsStr::new("bonjour");
os_str.clone_into(&mut os_string);
assert_eq!(os_str, os_string);
assert!(os_string.capacity() >= 123);
}
}
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
@@ -311,6 +311,7 @@
#![feature(str_utf16)]
#![feature(test, rustc_private)]
#![feature(thread_local)]
#![feature(toowned_clone_into)]
#![feature(try_from)]
#![feature(unboxed_closures)]
#![feature(unicode)]
12 changes: 12 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
@@ -1322,6 +1322,9 @@ impl ToOwned for Path {
fn to_owned(&self) -> PathBuf {
self.to_path_buf()
}
fn clone_into(&self, target: &mut PathBuf) {
self.inner.clone_into(&mut target.inner);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
@@ -3722,4 +3725,13 @@ mod tests {
assert_eq!(&*boxed, &*path_buf);
assert_eq!(&*path_buf, path);
}

#[test]
fn test_clone_into() {
let mut path_buf = PathBuf::from("supercalifragilisticexpialidocious");
let path = Path::new("short");
path.clone_into(&mut path_buf);
assert_eq!(path, path_buf);
assert!(path_buf.into_os_string().capacity() >= 15);
}
}