@@ -43,6 +43,10 @@ impl Alignment {
43
43
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
44
44
#[ inline]
45
45
#[ must_use]
46
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: requires(
47
+ mem:: align_of:: <T >( ) . is_power_of_two( ) ) ) ]
48
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: ensures(
49
+ |result| result. as_usize( ) . is_power_of_two( ) ) ) ]
46
50
pub const fn of < T > ( ) -> Self {
47
51
// This can't actually panic since type alignment is always a power of two.
48
52
const { Alignment :: new ( mem:: align_of :: < T > ( ) ) . unwrap ( ) }
@@ -54,6 +58,9 @@ impl Alignment {
54
58
/// Note that `0` is not a power of two, nor a valid alignment.
55
59
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
56
60
#[ inline]
61
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: ensures(
62
+ |result| align. is_power_of_two( ) == result. is_some( ) &&
63
+ ( result. is_none( ) || result. unwrap( ) . as_usize( ) == align) ) ) ]
57
64
pub const fn new ( align : usize ) -> Option < Self > {
58
65
if align. is_power_of_two ( ) {
59
66
// SAFETY: Just checked it only has one bit set
@@ -73,6 +80,9 @@ impl Alignment {
73
80
/// It must *not* be zero.
74
81
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
75
82
#[ inline]
83
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: requires( align > 0 && ( align & ( align - 1 ) ) == 0 ) ) ]
84
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: ensures(
85
+ |result| result. as_usize( ) == align && result. as_usize( ) . is_power_of_two( ) ) ) ]
76
86
pub const unsafe fn new_unchecked ( align : usize ) -> Self {
77
87
assert_unsafe_precondition ! (
78
88
check_language_ub,
@@ -88,13 +98,16 @@ impl Alignment {
88
98
/// Returns the alignment as a [`usize`].
89
99
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
90
100
#[ inline]
101
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: ensures( |result| result. is_power_of_two( ) ) ) ]
91
102
pub const fn as_usize ( self ) -> usize {
92
103
self . 0 as usize
93
104
}
94
105
95
106
/// Returns the alignment as a <code>[NonZero]<[usize]></code>.
96
107
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
97
108
#[ inline]
109
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: ensures(
110
+ |result| result. get( ) . is_power_of_two( ) && result. get( ) == self . as_usize( ) ) ) ]
98
111
pub const fn as_nonzero ( self ) -> NonZero < usize > {
99
112
// This transmutes directly to avoid the UbCheck in `NonZero::new_unchecked`
100
113
// since there's no way for the user to trip that check anyway -- the
@@ -120,6 +133,10 @@ impl Alignment {
120
133
/// ```
121
134
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
122
135
#[ inline]
136
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: requires( self . as_usize( ) . is_power_of_two( ) ) ) ]
137
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: ensures(
138
+ |result| ( * result as usize ) < mem:: size_of:: <usize >( ) * 8 &&
139
+ ( 1usize << * result) == self . as_usize( ) ) ) ]
123
140
pub const fn log2 ( self ) -> u32 {
124
141
self . as_nonzero ( ) . trailing_zeros ( )
125
142
}
@@ -149,6 +166,10 @@ impl Alignment {
149
166
/// ```
150
167
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
151
168
#[ inline]
169
+ #[ cfg_attr( not( bootstrap) , core:: contracts:: ensures(
170
+ |result| * result > 0 &&
171
+ * result == !( self . as_usize( ) -1 ) &&
172
+ self . as_usize( ) & * result == self . as_usize( ) ) ) ]
152
173
pub const fn mask ( self ) -> usize {
153
174
// SAFETY: The alignment is always nonzero, and therefore decrementing won't overflow.
154
175
!( unsafe { self . as_usize ( ) . unchecked_sub ( 1 ) } )
0 commit comments