File tree 1 file changed +19
-4
lines changed
1 file changed +19
-4
lines changed Original file line number Diff line number Diff line change @@ -247,8 +247,7 @@ impl OwnedAsciiCast for String {
247
247
248
248
#[ inline]
249
249
unsafe fn into_ascii_nocheck ( self ) -> Vec < Ascii > {
250
- let v: Vec < u8 > = mem:: transmute ( self ) ;
251
- v. into_ascii_nocheck ( )
250
+ self . into_bytes ( ) . into_ascii_nocheck ( )
252
251
}
253
252
}
254
253
@@ -260,7 +259,14 @@ impl OwnedAsciiCast for Vec<u8> {
260
259
261
260
#[ inline]
262
261
unsafe fn into_ascii_nocheck ( self ) -> Vec < Ascii > {
263
- mem:: transmute ( self )
262
+ let v = Vec :: from_raw_parts ( self . len ( ) ,
263
+ self . capacity ( ) ,
264
+ mem:: transmute ( self . as_ptr ( ) ) ) ;
265
+
266
+ // We forget `self` to avoid freeing it at the end of the scope
267
+ // Otherwise, the returned `Vec` would point to freed memory
268
+ mem:: forget ( self ) ;
269
+ v
264
270
}
265
271
}
266
272
@@ -338,7 +344,16 @@ pub trait IntoBytes {
338
344
339
345
impl IntoBytes for Vec < Ascii > {
340
346
fn into_bytes ( self ) -> Vec < u8 > {
341
- unsafe { mem:: transmute ( self ) }
347
+ unsafe {
348
+ let v = Vec :: from_raw_parts ( self . len ( ) ,
349
+ self . capacity ( ) ,
350
+ mem:: transmute ( self . as_ptr ( ) ) ) ;
351
+
352
+ // We forget `self` to avoid freeing it at the end of the scope
353
+ // Otherwise, the returned `Vec` would point to freed memory
354
+ mem:: forget ( self ) ;
355
+ v
356
+ }
342
357
}
343
358
}
344
359
You can’t perform that action at this time.
0 commit comments