Skip to content

Commit 4d38ac1

Browse files
authored
Rollup merge of #70201 - cuviper:clone_into, r=dtolnay
Small tweaks in ToOwned::clone_into - `<[T]>::clone_into` is slightly more optimized. - `CStr::clone_into` is new, letting it reuse its allocation. - `OsStr::clone_into` now forwards to the underlying slice/`Vec`.
2 parents 83d64cf + 28791f6 commit 4d38ac1

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

src/liballoc/slice.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,14 @@ impl<T: Clone> ToOwned for [T] {
735735
fn clone_into(&self, target: &mut Vec<T>) {
736736
// drop anything in target that will not be overwritten
737737
target.truncate(self.len());
738-
let len = target.len();
739-
740-
// reuse the contained values' allocations/resources.
741-
target.clone_from_slice(&self[..len]);
742738

743739
// target.len <= self.len due to the truncate above, so the
744-
// slice here is always in-bounds.
745-
target.extend_from_slice(&self[len..]);
740+
// slices here are always in-bounds.
741+
let (init, tail) = self.split_at(target.len());
742+
743+
// reuse the contained values' allocations/resources.
744+
target.clone_from_slice(init);
745+
target.extend_from_slice(tail);
746746
}
747747
}
748748

src/libstd/ffi/c_str.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,12 @@ impl ToOwned for CStr {
13291329
fn to_owned(&self) -> CString {
13301330
CString { inner: self.to_bytes_with_nul().into() }
13311331
}
1332+
1333+
fn clone_into(&self, target: &mut CString) {
1334+
let mut b = Vec::from(mem::take(&mut target.inner));
1335+
self.to_bytes_with_nul().clone_into(&mut b);
1336+
target.inner = b.into_boxed_slice();
1337+
}
13321338
}
13331339

13341340
#[stable(feature = "cstring_asref", since = "1.7.0")]
@@ -1510,6 +1516,17 @@ mod tests {
15101516
assert_eq!(boxed.to_bytes_with_nul(), &[0]);
15111517
}
15121518

1519+
#[test]
1520+
fn test_c_str_clone_into() {
1521+
let mut c_string = CString::new("bonjour").unwrap();
1522+
let c_ptr = c_string.as_ptr();
1523+
let c_str = CStr::from_bytes_with_nul(b"hello\0").unwrap();
1524+
c_str.clone_into(&mut c_string);
1525+
assert_eq!(c_str, c_string.as_c_str());
1526+
// It shouldn't have needed to move its allocation
1527+
assert_eq!(c_ptr, c_string.as_ptr());
1528+
}
1529+
15131530
#[test]
15141531
fn into_rc() {
15151532
let orig: &[u8] = b"Hello, world!\0";

src/libstd/ffi/os_str.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,7 @@ impl ToOwned for OsStr {
952952
self.to_os_string()
953953
}
954954
fn clone_into(&self, target: &mut OsString) {
955-
target.clear();
956-
target.push(self);
955+
self.inner.clone_into(&mut target.inner)
957956
}
958957
}
959958

src/libstd/sys/windows/os_str.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ impl Slice {
147147
Buf { inner: buf }
148148
}
149149

150+
pub fn clone_into(&self, buf: &mut Buf) {
151+
self.inner.clone_into(&mut buf.inner)
152+
}
153+
150154
#[inline]
151155
pub fn into_box(&self) -> Box<Slice> {
152156
unsafe { mem::transmute(self.inner.into_box()) }

src/libstd/sys_common/os_str_bytes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ impl Slice {
162162
Buf { inner: self.inner.to_vec() }
163163
}
164164

165+
pub fn clone_into(&self, buf: &mut Buf) {
166+
self.inner.clone_into(&mut buf.inner)
167+
}
168+
165169
#[inline]
166170
pub fn into_box(&self) -> Box<Slice> {
167171
let boxed: Box<[u8]> = self.inner.into();

src/libstd/sys_common/wtf8.rs

+4
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,10 @@ impl Wtf8 {
613613
}
614614
}
615615

616+
pub fn clone_into(&self, buf: &mut Wtf8Buf) {
617+
self.bytes.clone_into(&mut buf.bytes)
618+
}
619+
616620
/// Boxes this `Wtf8`.
617621
#[inline]
618622
pub fn into_box(&self) -> Box<Wtf8> {

0 commit comments

Comments
 (0)