@@ -311,8 +311,8 @@ impl CString {
311
311
#[ stable( feature = "cstr_memory" , since = "1.4.0" ) ]
312
312
pub unsafe fn from_raw ( ptr : * mut c_char ) -> CString {
313
313
let len = libc:: strlen ( ptr) + 1 ; // Including the NUL byte
314
- let slice = slice:: from_raw_parts ( ptr, len as usize ) ;
315
- CString { inner : mem :: transmute ( slice) }
314
+ let slice = slice:: from_raw_parts_mut ( ptr, len as usize ) ;
315
+ CString { inner : Box :: from_raw ( slice as * mut [ c_char ] as * mut [ u8 ] ) }
316
316
}
317
317
318
318
/// Transfers ownership of the string to a C caller.
@@ -480,7 +480,7 @@ impl CString {
480
480
/// ```
481
481
#[ stable( feature = "into_boxed_c_str" , since = "1.20.0" ) ]
482
482
pub fn into_boxed_c_str ( self ) -> Box < CStr > {
483
- unsafe { mem :: transmute ( self . into_inner ( ) ) }
483
+ unsafe { Box :: from_raw ( Box :: into_raw ( self . into_inner ( ) ) as * mut CStr ) }
484
484
}
485
485
486
486
// Bypass "move out of struct which implements [`Drop`] trait" restriction.
@@ -569,7 +569,7 @@ impl Borrow<CStr> for CString {
569
569
impl < ' a > From < & ' a CStr > for Box < CStr > {
570
570
fn from ( s : & ' a CStr ) -> Box < CStr > {
571
571
let boxed: Box < [ u8 ] > = Box :: from ( s. to_bytes_with_nul ( ) ) ;
572
- unsafe { mem :: transmute ( boxed) }
572
+ unsafe { Box :: from_raw ( Box :: into_raw ( boxed) as * mut CStr ) }
573
573
}
574
574
}
575
575
@@ -593,7 +593,7 @@ impl From<CString> for Box<CStr> {
593
593
impl Default for Box < CStr > {
594
594
fn default ( ) -> Box < CStr > {
595
595
let boxed: Box < [ u8 ] > = Box :: from ( [ 0 ] ) ;
596
- unsafe { mem :: transmute ( boxed) }
596
+ unsafe { Box :: from_raw ( Box :: into_raw ( boxed) as * mut CStr ) }
597
597
}
598
598
}
599
599
@@ -817,7 +817,7 @@ impl CStr {
817
817
#[ inline]
818
818
#[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
819
819
pub unsafe fn from_bytes_with_nul_unchecked ( bytes : & [ u8 ] ) -> & CStr {
820
- mem :: transmute ( bytes)
820
+ & * ( bytes as * const [ u8 ] as * const CStr )
821
821
}
822
822
823
823
/// Returns the inner pointer to this C string.
@@ -913,7 +913,7 @@ impl CStr {
913
913
#[ inline]
914
914
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
915
915
pub fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
916
- unsafe { mem :: transmute ( & self . inner ) }
916
+ unsafe { & * ( & self . inner as * const [ c_char ] as * const [ u8 ] ) }
917
917
}
918
918
919
919
/// Yields a [`&str`] slice if the `CStr` contains valid UTF-8.
@@ -1005,7 +1005,8 @@ impl CStr {
1005
1005
/// ```
1006
1006
#[ stable( feature = "into_boxed_c_str" , since = "1.20.0" ) ]
1007
1007
pub fn into_c_string ( self : Box < CStr > ) -> CString {
1008
- unsafe { mem:: transmute ( self ) }
1008
+ let raw = Box :: into_raw ( self ) as * mut [ u8 ] ;
1009
+ CString { inner : unsafe { Box :: from_raw ( raw) } }
1009
1010
}
1010
1011
}
1011
1012
0 commit comments