File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 17
17
#![ feature( const_nonnull_new) ]
18
18
#![ feature( const_num_from_num) ]
19
19
#![ feature( const_pointer_byte_offsets) ]
20
+ #![ feature( const_pointer_is_aligned) ]
20
21
#![ feature( const_ptr_as_ref) ]
21
22
#![ feature( const_ptr_read) ]
22
23
#![ feature( const_ptr_write) ]
80
81
#![ feature( never_type) ]
81
82
#![ feature( unwrap_infallible) ]
82
83
#![ feature( pointer_byte_offsets) ]
84
+ #![ feature( pointer_is_aligned) ]
83
85
#![ feature( portable_simd) ]
84
86
#![ feature( ptr_metadata) ]
85
87
#![ feature( once_cell) ]
Original file line number Diff line number Diff line change @@ -616,6 +616,41 @@ fn align_offset_with_provenance_const() {
616
616
}
617
617
}
618
618
619
+ #[ test]
620
+ fn is_aligned ( ) {
621
+ let data = 42 ;
622
+ let ptr: * const i32 = & data;
623
+ assert ! ( ptr. is_aligned( ) ) ;
624
+ assert ! ( ptr. is_aligned_to( 1 ) ) ;
625
+ assert ! ( ptr. is_aligned_to( 2 ) ) ;
626
+ assert ! ( ptr. is_aligned_to( 4 ) ) ;
627
+ assert ! ( ptr. wrapping_byte_add( 2 ) . is_aligned_to( 1 ) ) ;
628
+ assert ! ( ptr. wrapping_byte_add( 2 ) . is_aligned_to( 2 ) ) ;
629
+ assert ! ( !ptr. wrapping_byte_add( 2 ) . is_aligned_to( 4 ) ) ;
630
+
631
+ // At runtime either `ptr` or `ptr+1` is aligned to 8.
632
+ assert_ne ! ( ptr. is_aligned_to( 8 ) , ptr. wrapping_add( 1 ) . is_aligned_to( 8 ) ) ;
633
+ }
634
+
635
+ #[ test]
636
+ fn is_aligned_const ( ) {
637
+ const {
638
+ let data = 42 ;
639
+ let ptr: * const i32 = & data;
640
+ assert ! ( ptr. is_aligned( ) ) ;
641
+ assert ! ( ptr. is_aligned_to( 1 ) ) ;
642
+ assert ! ( ptr. is_aligned_to( 2 ) ) ;
643
+ assert ! ( ptr. is_aligned_to( 4 ) ) ;
644
+ assert ! ( ptr. wrapping_byte_add( 2 ) . is_aligned_to( 1 ) ) ;
645
+ assert ! ( ptr. wrapping_byte_add( 2 ) . is_aligned_to( 2 ) ) ;
646
+ assert ! ( !ptr. wrapping_byte_add( 2 ) . is_aligned_to( 4 ) ) ;
647
+
648
+ // At comptime neither `ptr` nor `ptr+1` is aligned to 8.
649
+ assert ! ( !ptr. is_aligned_to( 8 ) ) ;
650
+ assert ! ( !ptr. wrapping_add( 1 ) . is_aligned_to( 8 ) ) ;
651
+ }
652
+ }
653
+
619
654
#[ test]
620
655
fn offset_from ( ) {
621
656
let mut a = [ 0 ; 5 ] ;
You can’t perform that action at this time.
0 commit comments