@@ -2251,12 +2251,19 @@ assert_eq!(
2251
2251
```" ) ,
2252
2252
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
2253
2253
#[ rustc_const_stable( feature = "const_int_conversion" , since = "1.43.0" ) ]
2254
- #[ allow_internal_unstable( const_transmute) ]
2254
+ // SAFETY: const sound because integers are plain old datatypes so we can always
2255
+ // transmute them to arrays of bytes
2256
+ #[ allow_internal_unstable( const_fn_union) ]
2255
2257
#[ inline]
2256
2258
pub const fn to_ne_bytes( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
2259
+ #[ repr( C ) ]
2260
+ union Bytes {
2261
+ val: $SelfT,
2262
+ bytes: [ u8 ; mem:: size_of:: <$SelfT>( ) ] ,
2263
+ }
2257
2264
// SAFETY: integers are plain old datatypes so we can always transmute them to
2258
2265
// arrays of bytes
2259
- unsafe { mem :: transmute ( self ) }
2266
+ unsafe { Bytes { val : self } . bytes }
2260
2267
}
2261
2268
}
2262
2269
@@ -2362,11 +2369,18 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
2362
2369
```" ) ,
2363
2370
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
2364
2371
#[ rustc_const_stable( feature = "const_int_conversion" , since = "1.43.0" ) ]
2365
- #[ allow_internal_unstable( const_transmute) ]
2372
+ // SAFETY: const sound because integers are plain old datatypes so we can always
2373
+ // transmute to them
2374
+ #[ allow_internal_unstable( const_fn_union) ]
2366
2375
#[ inline]
2367
2376
pub const fn from_ne_bytes( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
2377
+ #[ repr( C ) ]
2378
+ union Bytes {
2379
+ val: $SelfT,
2380
+ bytes: [ u8 ; mem:: size_of:: <$SelfT>( ) ] ,
2381
+ }
2368
2382
// SAFETY: integers are plain old datatypes so we can always transmute to them
2369
- unsafe { mem :: transmute ( bytes) }
2383
+ unsafe { Bytes { bytes } . val }
2370
2384
}
2371
2385
}
2372
2386
}
@@ -4190,12 +4204,19 @@ assert_eq!(
4190
4204
```" ) ,
4191
4205
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
4192
4206
#[ rustc_const_stable( feature = "const_int_conversion" , since = "1.43.0" ) ]
4193
- #[ allow_internal_unstable( const_transmute) ]
4207
+ // SAFETY: const sound because integers are plain old datatypes so we can always
4208
+ // transmute them to arrays of bytes
4209
+ #[ allow_internal_unstable( const_fn_union) ]
4194
4210
#[ inline]
4195
4211
pub const fn to_ne_bytes( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
4212
+ #[ repr( C ) ]
4213
+ union Bytes {
4214
+ val: $SelfT,
4215
+ bytes: [ u8 ; mem:: size_of:: <$SelfT>( ) ] ,
4216
+ }
4196
4217
// SAFETY: integers are plain old datatypes so we can always transmute them to
4197
4218
// arrays of bytes
4198
- unsafe { mem :: transmute ( self ) }
4219
+ unsafe { Bytes { val : self } . bytes }
4199
4220
}
4200
4221
}
4201
4222
@@ -4301,11 +4322,18 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
4301
4322
```" ) ,
4302
4323
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
4303
4324
#[ rustc_const_stable( feature = "const_int_conversion" , since = "1.43.0" ) ]
4304
- #[ allow_internal_unstable( const_transmute) ]
4325
+ // SAFETY: const sound because integers are plain old datatypes so we can always
4326
+ // transmute to them
4327
+ #[ allow_internal_unstable( const_fn_union) ]
4305
4328
#[ inline]
4306
4329
pub const fn from_ne_bytes( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
4330
+ #[ repr( C ) ]
4331
+ union Bytes {
4332
+ val: $SelfT,
4333
+ bytes: [ u8 ; mem:: size_of:: <$SelfT>( ) ] ,
4334
+ }
4307
4335
// SAFETY: integers are plain old datatypes so we can always transmute to them
4308
- unsafe { mem :: transmute ( bytes) }
4336
+ unsafe { Bytes { bytes } . val }
4309
4337
}
4310
4338
}
4311
4339
}
0 commit comments