@@ -184,14 +184,30 @@ where
184
184
{
185
185
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
186
186
#[ inline]
187
- pub fn as_slice ( & self ) -> & [ T ] {
188
- self . 0 . as_ref ( )
187
+ pub const fn as_slice ( & self ) -> & [ T ] {
188
+ // SAFETY: `[T]` is layout-identical to `Array<T, U>`, which is a `repr(transparent)`
189
+ // newtype for `[T; N]`.
190
+ unsafe { slice:: from_raw_parts ( self . as_ptr ( ) , U :: USIZE ) }
189
191
}
190
192
191
193
/// Returns a mutable slice containing the entire array. Equivalent to `&mut s[..]`.
192
194
#[ inline]
193
- pub fn as_mut_slice ( & mut self ) -> & mut [ T ] {
194
- self . 0 . as_mut ( )
195
+ pub const fn as_mut_slice ( & mut self ) -> & mut [ T ] {
196
+ // SAFETY: `[T]` is layout-identical to `Array<T, U>`, which is a `repr(transparent)`
197
+ // newtype for `[T; N]`.
198
+ unsafe { slice:: from_raw_parts_mut ( self . as_mut_ptr ( ) , U :: USIZE ) }
199
+ }
200
+
201
+ /// Returns a pointer to the start of the array.
202
+ #[ allow( trivial_casts) ]
203
+ pub const fn as_ptr ( & self ) -> * const T {
204
+ self as * const Self as * const T
205
+ }
206
+
207
+ /// Returns a mutable pointer to the start of the array.
208
+ #[ allow( trivial_casts) ]
209
+ pub const fn as_mut_ptr ( & mut self ) -> * mut T {
210
+ self as * mut Self as * mut T
195
211
}
196
212
197
213
/// Returns an iterator over the array.
@@ -393,7 +409,7 @@ where
393
409
/// let empty_slice_of_arrays: &Array<Array<u32, U10>, U0> = &Array([]);
394
410
/// assert!(empty_slice_of_arrays.as_flattened().is_empty());
395
411
/// ```
396
- pub fn as_flattened ( & self ) -> & [ T ] {
412
+ pub const fn as_flattened ( & self ) -> & [ T ] {
397
413
Array :: slice_as_flattened ( self . as_slice ( ) )
398
414
}
399
415
@@ -422,7 +438,7 @@ where
422
438
/// add_5_to_all(array.as_flattened_mut());
423
439
/// assert_eq!(array, Array([Array([6, 7, 8]), Array([9, 10, 11]), Array([12, 13, 14])]));
424
440
/// ```
425
- pub fn as_flattened_mut ( & mut self ) -> & mut [ T ] {
441
+ pub const fn as_flattened_mut ( & mut self ) -> & mut [ T ] {
426
442
Array :: slice_as_flattened_mut ( self . as_mut_slice ( ) )
427
443
}
428
444
}
0 commit comments