File tree 6 files changed +36
-8
lines changed
6 files changed +36
-8
lines changed Original file line number Diff line number Diff line change @@ -733,14 +733,14 @@ impl<T: Clone> ToOwned for [T] {
733
733
fn clone_into ( & self , target : & mut Vec < T > ) {
734
734
// drop anything in target that will not be overwritten
735
735
target. truncate ( self . len ( ) ) ;
736
- let len = target. len ( ) ;
737
-
738
- // reuse the contained values' allocations/resources.
739
- target. clone_from_slice ( & self [ ..len] ) ;
740
736
741
737
// target.len <= self.len due to the truncate above, so the
742
- // slice here is always in-bounds.
743
- target. extend_from_slice ( & self [ len..] ) ;
738
+ // slices here are always in-bounds.
739
+ let ( init, tail) = self . split_at ( target. len ( ) ) ;
740
+
741
+ // reuse the contained values' allocations/resources.
742
+ target. clone_from_slice ( init) ;
743
+ target. extend_from_slice ( tail) ;
744
744
}
745
745
}
746
746
Original file line number Diff line number Diff line change @@ -1329,6 +1329,12 @@ impl ToOwned for CStr {
1329
1329
fn to_owned ( & self ) -> CString {
1330
1330
CString { inner : self . to_bytes_with_nul ( ) . into ( ) }
1331
1331
}
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
+ }
1332
1338
}
1333
1339
1334
1340
#[ stable( feature = "cstring_asref" , since = "1.7.0" ) ]
@@ -1510,6 +1516,17 @@ mod tests {
1510
1516
assert_eq ! ( boxed. to_bytes_with_nul( ) , & [ 0 ] ) ;
1511
1517
}
1512
1518
1519
+ #[ test]
1520
+ fn test_c_str_clone_into ( ) {
1521
+ let mut c_string = CString :: new ( "lorem" ) . unwrap ( ) ;
1522
+ let c_ptr = c_string. as_ptr ( ) ;
1523
+ let c_str = CStr :: from_bytes_with_nul ( b"ipsum\0 " ) . unwrap ( ) ;
1524
+ c_str. clone_into ( & mut c_string) ;
1525
+ assert_eq ! ( c_str, c_string. as_c_str( ) ) ;
1526
+ // The exact same size shouldn't have needed to move its allocation
1527
+ assert_eq ! ( c_ptr, c_string. as_ptr( ) ) ;
1528
+ }
1529
+
1513
1530
#[ test]
1514
1531
fn into_rc ( ) {
1515
1532
let orig: & [ u8 ] = b"Hello, world!\0 " ;
Original file line number Diff line number Diff line change @@ -1120,8 +1120,7 @@ impl ToOwned for OsStr {
1120
1120
self . to_os_string ( )
1121
1121
}
1122
1122
fn clone_into ( & self , target : & mut OsString ) {
1123
- target. clear ( ) ;
1124
- target. push ( self ) ;
1123
+ self . inner . clone_into ( & mut target. inner )
1125
1124
}
1126
1125
}
1127
1126
Original file line number Diff line number Diff line change @@ -159,6 +159,10 @@ impl Slice {
159
159
Buf { inner : buf }
160
160
}
161
161
162
+ pub fn clone_into ( & self , buf : & mut Buf ) {
163
+ self . inner . clone_into ( & mut buf. inner )
164
+ }
165
+
162
166
#[ inline]
163
167
pub fn into_box ( & self ) -> Box < Slice > {
164
168
unsafe { mem:: transmute ( self . inner . into_box ( ) ) }
Original file line number Diff line number Diff line change @@ -173,6 +173,10 @@ impl Slice {
173
173
Buf { inner : self . inner . to_vec ( ) }
174
174
}
175
175
176
+ pub fn clone_into ( & self , buf : & mut Buf ) {
177
+ self . inner . clone_into ( & mut buf. inner )
178
+ }
179
+
176
180
#[ inline]
177
181
pub fn into_box ( & self ) -> Box < Slice > {
178
182
let boxed: Box < [ u8 ] > = self . inner . into ( ) ;
Original file line number Diff line number Diff line change @@ -613,6 +613,10 @@ impl Wtf8 {
613
613
}
614
614
}
615
615
616
+ pub fn clone_into ( & self , buf : & mut Wtf8Buf ) {
617
+ self . bytes . clone_into ( & mut buf. bytes )
618
+ }
619
+
616
620
/// Boxes this `Wtf8`.
617
621
#[ inline]
618
622
pub fn into_box ( & self ) -> Box < Wtf8 > {
You can’t perform that action at this time.
0 commit comments