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:
commit
795bc2ccff
@ -733,14 +733,14 @@ fn to_owned(&self) -> Vec<T> {
|
|||||||
fn clone_into(&self, target: &mut Vec<T>) {
|
fn clone_into(&self, target: &mut Vec<T>) {
|
||||||
// drop anything in target that will not be overwritten
|
// drop anything in target that will not be overwritten
|
||||||
target.truncate(self.len());
|
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
|
// target.len <= self.len due to the truncate above, so the
|
||||||
// slice here is always in-bounds.
|
// slices here are always in-bounds.
|
||||||
target.extend_from_slice(&self[len..]);
|
let (init, tail) = self.split_at(target.len());
|
||||||
|
|
||||||
|
// reuse the contained values' allocations/resources.
|
||||||
|
target.clone_from_slice(init);
|
||||||
|
target.extend_from_slice(tail);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1329,6 +1329,12 @@ impl ToOwned for CStr {
|
|||||||
fn to_owned(&self) -> CString {
|
fn to_owned(&self) -> CString {
|
||||||
CString { inner: self.to_bytes_with_nul().into() }
|
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")]
|
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
||||||
@ -1510,6 +1516,17 @@ fn boxed_default() {
|
|||||||
assert_eq!(boxed.to_bytes_with_nul(), &[0]);
|
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]
|
#[test]
|
||||||
fn into_rc() {
|
fn into_rc() {
|
||||||
let orig: &[u8] = b"Hello, world!\0";
|
let orig: &[u8] = b"Hello, world!\0";
|
||||||
|
@ -1120,8 +1120,7 @@ fn to_owned(&self) -> OsString {
|
|||||||
self.to_os_string()
|
self.to_os_string()
|
||||||
}
|
}
|
||||||
fn clone_into(&self, target: &mut OsString) {
|
fn clone_into(&self, target: &mut OsString) {
|
||||||
target.clear();
|
self.inner.clone_into(&mut target.inner)
|
||||||
target.push(self);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,6 +159,10 @@ pub fn to_owned(&self) -> Buf {
|
|||||||
Buf { inner: buf }
|
Buf { inner: buf }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clone_into(&self, buf: &mut Buf) {
|
||||||
|
self.inner.clone_into(&mut buf.inner)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_box(&self) -> Box<Slice> {
|
pub fn into_box(&self) -> Box<Slice> {
|
||||||
unsafe { mem::transmute(self.inner.into_box()) }
|
unsafe { mem::transmute(self.inner.into_box()) }
|
||||||
|
@ -173,6 +173,10 @@ pub fn to_owned(&self) -> Buf {
|
|||||||
Buf { inner: self.inner.to_vec() }
|
Buf { inner: self.inner.to_vec() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clone_into(&self, buf: &mut Buf) {
|
||||||
|
self.inner.clone_into(&mut buf.inner)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_box(&self) -> Box<Slice> {
|
pub fn into_box(&self) -> Box<Slice> {
|
||||||
let boxed: Box<[u8]> = self.inner.into();
|
let boxed: Box<[u8]> = self.inner.into();
|
||||||
|
@ -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`.
|
/// Boxes this `Wtf8`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_box(&self) -> Box<Wtf8> {
|
pub fn into_box(&self) -> Box<Wtf8> {
|
||||||
|
Loading…
Reference in New Issue
Block a user