@@ -1008,18 +1008,18 @@ pub fn copy<T: Copy>(x: &T) -> T {
1008
1008
* x
1009
1009
}
1010
1010
1011
- /// Interprets `src` as having type `&U `, and then reads `src` without moving
1011
+ /// Interprets `src` as having type `&Dst `, and then reads `src` without moving
1012
1012
/// the contained value.
1013
1013
///
1014
- /// This function will unsafely assume the pointer `src` is valid for [`size_of::<U >`][size_of]
1015
- /// bytes by transmuting `&T ` to `&U ` and then reading the `&U ` (except that this is done in a way
1016
- /// that is correct even when `&U ` has stricter alignment requirements than `&T `). It will also
1017
- /// unsafely create a copy of the contained value instead of moving out of `src`.
1014
+ /// This function will unsafely assume the pointer `src` is valid for [`size_of::<Dst >`][size_of]
1015
+ /// bytes by transmuting `&Src ` to `&Dst ` and then reading the `&Dst ` (except that this is done
1016
+ /// in a way that is correct even when `&Dst ` has stricter alignment requirements than `&Src `).
1017
+ /// It will also unsafely create a copy of the contained value instead of moving out of `src`.
1018
1018
///
1019
- /// It is not a compile-time error if `T ` and `U ` have different sizes, but it
1020
- /// is highly encouraged to only invoke this function where `T ` and `U ` have the
1021
- /// same size. This function triggers [undefined behavior][ub] if `U ` is larger than
1022
- /// `T `.
1019
+ /// It is not a compile-time error if `Src ` and `Dst ` have different sizes, but it
1020
+ /// is highly encouraged to only invoke this function where `Src ` and `Dst ` have the
1021
+ /// same size. This function triggers [undefined behavior][ub] if `Dst ` is larger than
1022
+ /// `Src `.
1023
1023
///
1024
1024
/// [ub]: ../../reference/behavior-considered-undefined.html
1025
1025
///
@@ -1052,19 +1052,22 @@ pub fn copy<T: Copy>(x: &T) -> T {
1052
1052
#[ must_use]
1053
1053
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1054
1054
#[ rustc_const_unstable( feature = "const_transmute_copy" , issue = "83165" ) ]
1055
- pub const unsafe fn transmute_copy < T , U > ( src : & T ) -> U {
1056
- assert ! ( size_of:: <T >( ) >= size_of:: <U >( ) , "cannot transmute_copy if U is larger than T" ) ;
1055
+ pub const unsafe fn transmute_copy < Src , Dst > ( src : & Src ) -> Dst {
1056
+ assert ! (
1057
+ size_of:: <Src >( ) >= size_of:: <Dst >( ) ,
1058
+ "cannot transmute_copy if Dst is larger than Src"
1059
+ ) ;
1057
1060
1058
- // If U has a higher alignment requirement, src might not be suitably aligned.
1059
- if align_of :: < U > ( ) > align_of :: < T > ( ) {
1061
+ // If Dst has a higher alignment requirement, src might not be suitably aligned.
1062
+ if align_of :: < Dst > ( ) > align_of :: < Src > ( ) {
1060
1063
// SAFETY: `src` is a reference which is guaranteed to be valid for reads.
1061
1064
// The caller must guarantee that the actual transmutation is safe.
1062
- unsafe { ptr:: read_unaligned ( src as * const T as * const U ) }
1065
+ unsafe { ptr:: read_unaligned ( src as * const Src as * const Dst ) }
1063
1066
} else {
1064
1067
// SAFETY: `src` is a reference which is guaranteed to be valid for reads.
1065
- // We just checked that `src as *const U ` was properly aligned.
1068
+ // We just checked that `src as *const Dst ` was properly aligned.
1066
1069
// The caller must guarantee that the actual transmutation is safe.
1067
- unsafe { ptr:: read ( src as * const T as * const U ) }
1070
+ unsafe { ptr:: read ( src as * const Src as * const Dst ) }
1068
1071
}
1069
1072
}
1070
1073
0 commit comments