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