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`.
This commit is contained in:
Dylan DPC 2020-04-07 14:46:53 +02:00 committed by GitHub
commit 795bc2ccff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 8 deletions

View File

@ -733,14 +733,14 @@ fn to_owned(&self) -> Vec<T> {
fn clone_into(&self, target: &mut Vec<T>) {
// drop anything in target that will not be overwritten
target.truncate(self.len());
let len = target.len();
// reuse the contained values' allocations/resources.
target.clone_from_slice(&self[..len]);
// target.len <= self.len due to the truncate above, so the
// slice here is always in-bounds.
target.extend_from_slice(&self[len..]);
// slices here are always in-bounds.
let (init, tail) = self.split_at(target.len());
// reuse the contained values' allocations/resources.
target.clone_from_slice(init);
target.extend_from_slice(tail);
}
}

View File

@ -1329,6 +1329,12 @@ impl ToOwned for CStr {
fn to_owned(&self) -> CString {
CString { inner: self.to_bytes_with_nul().into() }
}
fn clone_into(&self, target: &mut CString) {
let mut b = Vec::from(mem::take(&mut target.inner));
self.to_bytes_with_nul().clone_into(&mut b);
target.inner = b.into_boxed_slice();
}
}
#[stable(feature = "cstring_asref", since = "1.7.0")]
@ -1510,6 +1516,17 @@ fn boxed_default() {
assert_eq!(boxed.to_bytes_with_nul(), &[0]);
}
#[test]
fn test_c_str_clone_into() {
let mut c_string = CString::new("lorem").unwrap();
let c_ptr = c_string.as_ptr();
let c_str = CStr::from_bytes_with_nul(b"ipsum\0").unwrap();
c_str.clone_into(&mut c_string);
assert_eq!(c_str, c_string.as_c_str());
// The exact same size shouldn't have needed to move its allocation
assert_eq!(c_ptr, c_string.as_ptr());
}
#[test]
fn into_rc() {
let orig: &[u8] = b"Hello, world!\0";

View File

@ -1120,8 +1120,7 @@ fn to_owned(&self) -> OsString {
self.to_os_string()
}
fn clone_into(&self, target: &mut OsString) {
target.clear();
target.push(self);
self.inner.clone_into(&mut target.inner)
}
}

View File

@ -159,6 +159,10 @@ pub fn to_owned(&self) -> Buf {
Buf { inner: buf }
}
pub fn clone_into(&self, buf: &mut Buf) {
self.inner.clone_into(&mut buf.inner)
}
#[inline]
pub fn into_box(&self) -> Box<Slice> {
unsafe { mem::transmute(self.inner.into_box()) }

View File

@ -173,6 +173,10 @@ pub fn to_owned(&self) -> Buf {
Buf { inner: self.inner.to_vec() }
}
pub fn clone_into(&self, buf: &mut Buf) {
self.inner.clone_into(&mut buf.inner)
}
#[inline]
pub fn into_box(&self) -> Box<Slice> {
let boxed: Box<[u8]> = self.inner.into();

View File

@ -613,6 +613,10 @@ fn initial_trail_surrogate(&self) -> Option<u16> {
}
}
pub fn clone_into(&self, buf: &mut Wtf8Buf) {
self.bytes.clone_into(&mut buf.bytes)
}
/// Boxes this `Wtf8`.
#[inline]
pub fn into_box(&self) -> Box<Wtf8> {