@@ -440,49 +440,91 @@ 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
- ) *
443
+ #[ cfg( zerocopy_target_has_atomics) ]
444
+ mod atomics {
445
+ use core:: sync:: atomic:: {
446
+ AtomicBool , AtomicI16 , AtomicI32 , AtomicI64 , AtomicI8 , AtomicIsize , AtomicPtr , AtomicU16 ,
447
+ AtomicU32 , AtomicU64 , AtomicU8 , AtomicUsize ,
451
448
} ;
452
- }
453
449
454
- #[ rustfmt:: skip]
455
- impl_traits_for_atomics ! (
456
- AtomicI16 , AtomicI32 , AtomicI8 , AtomicIsize ,
457
- AtomicU16 , AtomicU32 , AtomicU8 , AtomicUsize ,
458
- ) ;
450
+ use super :: * ;
459
451
460
- impl_for_transparent_wrapper ! ( => TryFromBytes for AtomicBool ) ;
461
- impl_for_transparent_wrapper ! ( => FromZeros for AtomicBool ) ;
462
- impl_for_transparent_wrapper ! ( => IntoBytes for AtomicBool ) ;
452
+ macro_rules! impl_traits_for_atomics {
453
+ ( $( $atomics: ident) ,* $( , ) ?) => {
454
+ $(
455
+ impl_known_layout!( $atomics) ;
456
+ impl_for_transparent_wrapper!( => TryFromBytes for $atomics) ;
457
+ impl_for_transparent_wrapper!( => FromZeros for $atomics) ;
458
+ impl_for_transparent_wrapper!( => FromBytes for $atomics) ;
459
+ impl_for_transparent_wrapper!( => IntoBytes for $atomics) ;
460
+ ) *
461
+ } ;
462
+ }
463
463
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 ) ;
464
+ #[ cfg( target_has_atomic = "8" ) ]
465
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "8" ) ) ) ]
466
+ mod atomic_8 {
467
+ use super :: * ;
468
+
469
+ impl_traits_for_atomics ! ( AtomicU8 , AtomicI8 ) ;
470
+
471
+ impl_known_layout ! ( AtomicBool ) ;
472
+
473
+ impl_for_transparent_wrapper ! ( => TryFromBytes for AtomicBool ) ;
474
+ impl_for_transparent_wrapper ! ( => FromZeros for AtomicBool ) ;
475
+ impl_for_transparent_wrapper ! ( => IntoBytes for AtomicBool ) ;
476
+
477
+ safety_comment ! {
478
+ /// SAFETY:
479
+ /// Per [1], `AtomicBool`, `AtomicU8`, and `AtomicI8` have the same
480
+ /// size as `bool`, `u8`, and `i8` respectively. Since a type's
481
+ /// alignment cannot be smaller than 1 [2], and since its alignment
482
+ /// cannot be greater than its size [3], the only possible value for
483
+ /// the alignment is 1. Thus, it is sound to implement `Unaligned`.
484
+ ///
485
+ /// [1] TODO(#896), TODO(https://github.com/rust-lang/rust/pull/121943):
486
+ /// Cite docs once they've landed.
487
+ ///
488
+ /// [2] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
489
+ ///
490
+ /// Alignment is measured in bytes, and must be at least 1.
491
+ ///
492
+ /// [3] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
493
+ ///
494
+ /// The size of a value is always a multiple of its alignment.
495
+ unsafe_impl!( AtomicBool : Unaligned ) ;
496
+ unsafe_impl!( AtomicU8 : Unaligned ) ;
497
+ unsafe_impl!( AtomicI8 : Unaligned ) ;
498
+ assert_unaligned!( AtomicBool , AtomicU8 , AtomicI8 ) ;
499
+ }
500
+ }
501
+
502
+ #[ cfg( target_has_atomic = "16" ) ]
503
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "16" ) ) ) ]
504
+ impl_traits_for_atomics ! ( AtomicU16 , AtomicI16 ) ;
505
+
506
+ #[ cfg( target_has_atomic = "32" ) ]
507
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "32" ) ) ) ]
508
+ impl_traits_for_atomics ! ( AtomicU32 , AtomicI32 ) ;
509
+
510
+ #[ cfg( target_has_atomic = "64" ) ]
511
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "64" ) ) ) ]
512
+ impl_traits_for_atomics ! ( AtomicU64 , AtomicI64 ) ;
513
+
514
+ #[ cfg( target_has_atomic = "ptr" ) ]
515
+ #[ cfg_attr( doc_cfg, doc( cfg( target_has_atomic = "ptr" ) ) ) ]
516
+ mod atomic_ptr {
517
+ use super :: * ;
518
+
519
+ impl_traits_for_atomics ! ( AtomicUsize , AtomicIsize ) ;
520
+
521
+ impl_known_layout ! ( T => AtomicPtr <T >) ;
522
+
523
+ // TODO(#170): Implement `FromBytes` and `IntoBytes` once we implement
524
+ // those traits for `*mut T`.
525
+ impl_for_transparent_wrapper ! ( T => TryFromBytes for AtomicPtr <T >) ;
526
+ impl_for_transparent_wrapper ! ( T => FromZeros for AtomicPtr <T >) ;
527
+ }
486
528
}
487
529
488
530
safety_comment ! {
0 commit comments