|
1 | 1 | //! Free functions to create `&[T]` and `&mut [T]`. |
2 | 2 |
|
3 | 3 | use crate::array; |
4 | | -use crate::mem::{align_of, size_of}; |
| 4 | +use crate::intrinsics; |
| 5 | +use crate::mem::{align_of, SizedTypeProperties}; |
5 | 6 | use crate::ops::Range; |
6 | 7 | use crate::ptr; |
7 | 8 | use crate::ub_checks; |
@@ -98,13 +99,14 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] |
98 | 99 | "slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`", |
99 | 100 | ( |
100 | 101 | data: *mut () = data as *mut (), |
101 | | - size: usize = size_of::<T>(), |
102 | 102 | align: usize = align_of::<T>(), |
103 | 103 | len: usize = len, |
| 104 | + max_len: usize = T::MAX_SLICE_LEN, |
104 | 105 | ) => |
105 | 106 | ub_checks::is_aligned_and_not_null(data, align) |
106 | | - && ub_checks::is_valid_allocation_size(size, len) |
| 107 | + && len <= max_len |
107 | 108 | ); |
| 109 | + intrinsics::assume(len <= T::MAX_SLICE_LEN); |
108 | 110 | &*ptr::slice_from_raw_parts(data, len) |
109 | 111 | } |
110 | 112 | } |
@@ -152,13 +154,14 @@ pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a m |
152 | 154 | "slice::from_raw_parts_mut requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`", |
153 | 155 | ( |
154 | 156 | data: *mut () = data as *mut (), |
155 | | - size: usize = size_of::<T>(), |
156 | 157 | align: usize = align_of::<T>(), |
157 | 158 | len: usize = len, |
| 159 | + max_len: usize = T::MAX_SLICE_LEN, |
158 | 160 | ) => |
159 | 161 | ub_checks::is_aligned_and_not_null(data, align) |
160 | | - && ub_checks::is_valid_allocation_size(size, len) |
| 162 | + && len <= max_len |
161 | 163 | ); |
| 164 | + intrinsics::assume(len <= T::MAX_SLICE_LEN); |
162 | 165 | &mut *ptr::slice_from_raw_parts_mut(data, len) |
163 | 166 | } |
164 | 167 | } |
|
0 commit comments