Skip to content

Commit c28372d

Browse files
authored
Rollup merge of #41390 - scottmcm:toowned-clone-into, r=alexcrichton
Override ToOwned::clone_into for Path and OsStr The only non-overridden one remaining is the CStr impl, which cannot be optimized as doing so would break CString's second invariant. Follow-up to 7ec27ae (PR #41009). r? @alexcrichton
2 parents 559a4b3 + 295bcdb commit c28372d

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/libstd/ffi/os_str.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,13 @@ impl Borrow<OsStr> for OsString {
677677
#[stable(feature = "rust1", since = "1.0.0")]
678678
impl ToOwned for OsStr {
679679
type Owned = OsString;
680-
fn to_owned(&self) -> OsString { self.to_os_string() }
680+
fn to_owned(&self) -> OsString {
681+
self.to_os_string()
682+
}
683+
fn clone_into(&self, target: &mut OsString) {
684+
target.clear();
685+
target.push(self);
686+
}
681687
}
682688

683689
#[stable(feature = "rust1", since = "1.0.0")]
@@ -863,4 +869,14 @@ mod tests {
863869
let boxed = <Box<OsStr>>::default();
864870
assert!(boxed.is_empty());
865871
}
872+
873+
#[test]
874+
fn test_os_str_clone_into() {
875+
let mut os_string = OsString::with_capacity(123);
876+
os_string.push("hello");
877+
let os_str = OsStr::new("bonjour");
878+
os_str.clone_into(&mut os_string);
879+
assert_eq!(os_str, os_string);
880+
assert!(os_string.capacity() >= 123);
881+
}
866882
}

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
#![feature(str_utf16)]
312312
#![feature(test, rustc_private)]
313313
#![feature(thread_local)]
314+
#![feature(toowned_clone_into)]
314315
#![feature(try_from)]
315316
#![feature(unboxed_closures)]
316317
#![feature(unicode)]

src/libstd/path.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,9 @@ impl ToOwned for Path {
14141414
fn to_owned(&self) -> PathBuf {
14151415
self.to_path_buf()
14161416
}
1417+
fn clone_into(&self, target: &mut PathBuf) {
1418+
self.inner.clone_into(&mut target.inner);
1419+
}
14171420
}
14181421

14191422
#[stable(feature = "rust1", since = "1.0.0")]
@@ -3859,4 +3862,13 @@ mod tests {
38593862
assert_eq!(&*boxed, &*path_buf);
38603863
assert_eq!(&*path_buf, path);
38613864
}
3865+
3866+
#[test]
3867+
fn test_clone_into() {
3868+
let mut path_buf = PathBuf::from("supercalifragilisticexpialidocious");
3869+
let path = Path::new("short");
3870+
path.clone_into(&mut path_buf);
3871+
assert_eq!(path, path_buf);
3872+
assert!(path_buf.into_os_string().capacity() >= 15);
3873+
}
38623874
}

0 commit comments

Comments
 (0)