3333 ///
3434 /// Unsafe because caller is responsible for ensuring all of the following:
3535 ///
36- /// * `ptr` must be non-null and aligned , and it must be safe to
37- /// [`.offset()`] `ptr` by zero.
36+ /// * `ptr` must be non-null, and it must be safe to [`.offset()`] `ptr` by
37+ /// zero.
3838 ///
3939 /// * It must be safe to [`.offset()`] the pointer repeatedly along all
4040 /// axes and calculate the `count`s for the `.offset()` calls without
7070 let strides = shape. strides ;
7171 if cfg ! ( debug_assertions) {
7272 assert ! ( !ptr. is_null( ) , "The pointer must be non-null." ) ;
73- assert ! ( is_aligned( ptr) , "The pointer must be aligned." ) ;
7473 dimension:: max_abs_offset_check_overflow :: < A , _ > ( & dim, & strides) . unwrap ( ) ;
7574 }
7675 RawArrayView :: new_ ( ptr, dim, strides)
8079 ///
8180 /// **Warning** from a safety standpoint, this is equivalent to
8281 /// dereferencing a raw pointer for every element in the array. You must
83- /// ensure that all of the data is valid and choose the correct lifetime.
82+ /// ensure that all of the data is valid, ensure that the pointer is
83+ /// aligned, and choose the correct lifetime.
8484 #[ inline]
8585 pub unsafe fn deref_into_view < ' a > ( self ) -> ArrayView < ' a , A , D > {
86+ debug_assert ! (
87+ is_aligned( self . ptr. as_ptr( ) ) ,
88+ "The pointer must be aligned."
89+ ) ;
8690 ArrayView :: new ( self . ptr , self . dim , self . strides )
8791 }
8892
@@ -130,12 +134,6 @@ where
130134 "size mismatch in raw view cast"
131135 ) ;
132136 let ptr = self . ptr . cast :: < B > ( ) ;
133- debug_assert ! (
134- is_aligned( ptr. as_ptr( ) ) ,
135- "alignment mismatch in raw view cast"
136- ) ;
137- /* Alignment checked with debug assertion: alignment could be dynamically correct,
138- * and we don't have a check that compiles out for that. */
139137 unsafe { RawArrayView :: new ( ptr, self . dim , self . strides ) }
140138 }
141139}
@@ -167,8 +165,8 @@ where
167165 ///
168166 /// Unsafe because caller is responsible for ensuring all of the following:
169167 ///
170- /// * `ptr` must be non-null and aligned , and it must be safe to
171- /// [`.offset()`] `ptr` by zero.
168+ /// * `ptr` must be non-null, and it must be safe to [`.offset()`] `ptr` by
169+ /// zero.
172170 ///
173171 /// * It must be safe to [`.offset()`] the pointer repeatedly along all
174172 /// axes and calculate the `count`s for the `.offset()` calls without
@@ -204,7 +202,6 @@ where
204202 let strides = shape. strides ;
205203 if cfg ! ( debug_assertions) {
206204 assert ! ( !ptr. is_null( ) , "The pointer must be non-null." ) ;
207- assert ! ( is_aligned( ptr) , "The pointer must be aligned." ) ;
208205 dimension:: max_abs_offset_check_overflow :: < A , _ > ( & dim, & strides) . unwrap ( ) ;
209206 }
210207 RawArrayViewMut :: new_ ( ptr, dim, strides)
@@ -220,19 +217,29 @@ where
220217 ///
221218 /// **Warning** from a safety standpoint, this is equivalent to
222219 /// dereferencing a raw pointer for every element in the array. You must
223- /// ensure that all of the data is valid and choose the correct lifetime.
220+ /// ensure that all of the data is valid, ensure that the pointer is
221+ /// aligned, and choose the correct lifetime.
224222 #[ inline]
225223 pub unsafe fn deref_into_view < ' a > ( self ) -> ArrayView < ' a , A , D > {
224+ debug_assert ! (
225+ is_aligned( self . ptr. as_ptr( ) ) ,
226+ "The pointer must be aligned."
227+ ) ;
226228 ArrayView :: new ( self . ptr , self . dim , self . strides )
227229 }
228230
229231 /// Converts to a mutable view of the array.
230232 ///
231233 /// **Warning** from a safety standpoint, this is equivalent to
232234 /// dereferencing a raw pointer for every element in the array. You must
233- /// ensure that all of the data is valid and choose the correct lifetime.
235+ /// ensure that all of the data is valid, ensure that the pointer is
236+ /// aligned, and choose the correct lifetime.
234237 #[ inline]
235238 pub unsafe fn deref_into_view_mut < ' a > ( self ) -> ArrayViewMut < ' a , A , D > {
239+ debug_assert ! (
240+ is_aligned( self . ptr. as_ptr( ) ) ,
241+ "The pointer must be aligned."
242+ ) ;
236243 ArrayViewMut :: new ( self . ptr , self . dim , self . strides )
237244 }
238245
@@ -267,12 +274,6 @@ where
267274 "size mismatch in raw view cast"
268275 ) ;
269276 let ptr = self . ptr . cast :: < B > ( ) ;
270- debug_assert ! (
271- is_aligned( ptr. as_ptr( ) ) ,
272- "alignment mismatch in raw view cast"
273- ) ;
274- /* Alignment checked with debug assertion: alignment could be dynamically correct,
275- * and we don't have a check that compiles out for that. */
276277 unsafe { RawArrayViewMut :: new ( ptr, self . dim , self . strides ) }
277278 }
278279}
0 commit comments