@@ -440,49 +440,72 @@ safety_comment! {
440
440
unsafe_impl_for_power_set!( A , B , C , D , E , F , G , H , I , J , K , L -> M => Immutable for opt_extern_c_fn!( ...) ) ;
441
441
}
442
442
443
- macro_rules! impl_traits_for_atomics {
444
- ( $( $atomics: ident) ,* $( , ) ?) => {
445
- $(
446
- impl_for_transparent_wrapper!( => TryFromBytes for $atomics) ;
447
- impl_for_transparent_wrapper!( => FromZeros for $atomics) ;
448
- impl_for_transparent_wrapper!( => FromBytes for $atomics) ;
449
- impl_for_transparent_wrapper!( => IntoBytes for $atomics) ;
450
- ) *
451
- } ;
452
- }
443
+ #[ cfg( zerocopy_target_has_atomics) ]
444
+ mod atomics {
445
+ use super :: * ;
453
446
454
- #[ rustfmt:: skip]
455
- impl_traits_for_atomics ! (
456
- AtomicI16 , AtomicI32 , AtomicI8 , AtomicIsize ,
457
- AtomicU16 , AtomicU32 , AtomicU8 , AtomicUsize ,
458
- ) ;
447
+ macro_rules! impl_traits_for_atomics {
448
+ ( $( $atomics: ident) ,* $( , ) ?) => {
449
+ $(
450
+ impl_for_transparent_wrapper!( => TryFromBytes for $atomics) ;
451
+ impl_for_transparent_wrapper!( => FromZeros for $atomics) ;
452
+ impl_for_transparent_wrapper!( => FromBytes for $atomics) ;
453
+ impl_for_transparent_wrapper!( => IntoBytes for $atomics) ;
454
+ ) *
455
+ } ;
456
+ }
459
457
460
- impl_for_transparent_wrapper ! ( => TryFromBytes for AtomicBool ) ;
461
- impl_for_transparent_wrapper ! ( => FromZeros for AtomicBool ) ;
462
- impl_for_transparent_wrapper ! ( => IntoBytes for AtomicBool ) ;
458
+ #[ cfg( target_has_atomic = "8" ) ]
459
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "8" ) ) ) ]
460
+ mod atomic_8 {
461
+ use super :: * ;
462
+
463
+ impl_traits_for_atomics ! ( AtomicU8 , AtomicI8 ) ;
464
+
465
+ impl_for_transparent_wrapper ! ( => TryFromBytes for AtomicBool ) ;
466
+ impl_for_transparent_wrapper ! ( => FromZeros for AtomicBool ) ;
467
+ impl_for_transparent_wrapper ! ( => IntoBytes for AtomicBool ) ;
468
+
469
+ safety_comment ! {
470
+ /// SAFETY:
471
+ /// Per [1], `AtomicBool`, `AtomicU8`, and `AtomicI8` have the same
472
+ /// size as `bool`, `u8`, and `i8` respectively. Since a type's
473
+ /// alignment cannot be smaller than 1 [2], and since its alignment
474
+ /// cannot be greater than its size [3], the only possible value for
475
+ /// the alignment is 1. Thus, it is sound to implement `Unaligned`.
476
+ ///
477
+ /// [1] TODO(#896), TODO(https://github.com/rust-lang/rust/pull/121943):
478
+ /// Cite docs once they've landed.
479
+ ///
480
+ /// [2] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
481
+ ///
482
+ /// Alignment is measured in bytes, and must be at least 1.
483
+ ///
484
+ /// [3] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
485
+ ///
486
+ /// The size of a value is always a multiple of its alignment.
487
+ unsafe_impl!( AtomicBool : Unaligned ) ;
488
+ unsafe_impl!( AtomicU8 : Unaligned ) ;
489
+ unsafe_impl!( AtomicI8 : Unaligned ) ;
490
+ assert_unaligned!( AtomicBool , AtomicU8 , AtomicI8 ) ;
491
+ }
492
+ }
463
493
464
- safety_comment ! {
465
- /// SAFETY:
466
- /// Per [1], `AtomicBool`, `AtomicU8`, and `AtomicI8` have the same size as
467
- /// `bool`, `u8`, and `i8` respectively. Since a type's alignment cannot be
468
- /// smaller than 1 [2], and since its alignment cannot be greater than its
469
- /// size [3], the only possible value for the alignment is 1. Thus, it is
470
- /// sound to implement `Unaligned`.
471
- ///
472
- /// [1] TODO(#896), TODO(https://github.com/rust-lang/rust/pull/121943):
473
- /// Cite docs once they've landed.
474
- ///
475
- /// [2] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
476
- ///
477
- /// Alignment is measured in bytes, and must be at least 1.
478
- ///
479
- /// [3] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
480
- ///
481
- /// The size of a value is always a multiple of its alignment.
482
- unsafe_impl!( AtomicBool : Unaligned ) ;
483
- unsafe_impl!( AtomicU8 : Unaligned ) ;
484
- unsafe_impl!( AtomicI8 : Unaligned ) ;
485
- assert_unaligned!( AtomicBool , AtomicU8 , AtomicI8 ) ;
494
+ #[ cfg( target_has_atomic = "16" ) ]
495
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "16" ) ) ) ]
496
+ impl_traits_for_atomics ! ( AtomicU16 , AtomicI16 ) ;
497
+
498
+ #[ cfg( target_has_atomic = "32" ) ]
499
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "32" ) ) ) ]
500
+ impl_traits_for_atomics ! ( AtomicU32 , AtomicI32 ) ;
501
+
502
+ #[ cfg( target_has_atomic = "64" ) ]
503
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "64" ) ) ) ]
504
+ impl_traits_for_atomics ! ( AtomicU64 , AtomicI64 ) ;
505
+
506
+ #[ cfg( target_has_atomic = "ptr" ) ]
507
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "ptr" ) ) ) ]
508
+ impl_traits_for_atomics ! ( AtomicUsize , AtomicIsize ) ;
486
509
}
487
510
488
511
safety_comment ! {
0 commit comments