|
7 | 7 | // except according to those terms. |
8 | 8 |
|
9 | 9 | use alloc::vec::Vec; |
10 | | -use std::hash; |
11 | | -use std::iter::FromIterator; |
12 | 10 | use std::iter::IntoIterator; |
13 | 11 | use std::mem; |
14 | 12 | use std::ops::{Index, IndexMut}; |
| 13 | +use std::{hash, mem::size_of}; |
| 14 | +use std::{iter::FromIterator, slice}; |
15 | 15 |
|
16 | | -use crate::imp_prelude::*; |
| 16 | +use crate::iter::{Iter, IterMut}; |
17 | 17 | use crate::NdIndex; |
18 | | -use crate::{ |
19 | | - aview_mut2, |
20 | | - iter::{Iter, IterMut}, |
21 | | -}; |
| 18 | +use crate::{dimension, imp_prelude::*}; |
22 | 19 |
|
23 | 20 | use crate::numeric_util; |
24 | 21 | use crate::{FoldWhile, Zip}; |
@@ -315,10 +312,26 @@ where |
315 | 312 | } |
316 | 313 |
|
317 | 314 | /// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array |
| 315 | +/// |
| 316 | +/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A |
| 317 | +/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes). |
318 | 318 | impl<'a, A, const N: usize> From<&'a [[A; N]]> for ArrayView<'a, A, Ix2> { |
319 | 319 | /// Create a two-dimensional read-only array view of the data in `slice` |
320 | | - fn from(slice: &'a [[A; N]]) -> Self { |
321 | | - aview2(slice) |
| 320 | + fn from(xs: &'a [[A; N]]) -> Self { |
| 321 | + let cols = N; |
| 322 | + let rows = xs.len(); |
| 323 | + let dim = Ix2(rows, cols); |
| 324 | + if size_of::<A>() == 0 { |
| 325 | + dimension::size_of_shape_checked(&dim) |
| 326 | + .expect("Product of non-zero axis lengths must not overflow isize."); |
| 327 | + } |
| 328 | + |
| 329 | + // `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in |
| 330 | + // `isize::MAX` |
| 331 | + unsafe { |
| 332 | + let data = slice::from_raw_parts(xs.as_ptr() as *const A, cols * rows); |
| 333 | + ArrayView::from_shape_ptr(dim, data.as_ptr()) |
| 334 | + } |
322 | 335 | } |
323 | 336 | } |
324 | 337 |
|
@@ -355,10 +368,26 @@ where |
355 | 368 | } |
356 | 369 |
|
357 | 370 | /// Implementation of ArrayViewMut2::from(&S) where S is a slice to a 2D array |
| 371 | +/// |
| 372 | +/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A |
| 373 | +/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes). |
358 | 374 | impl<'a, A, const N: usize> From<&'a mut [[A; N]]> for ArrayViewMut<'a, A, Ix2> { |
359 | 375 | /// Create a two-dimensional read-write array view of the data in `slice` |
360 | | - fn from(slice: &'a mut [[A; N]]) -> Self { |
361 | | - aview_mut2(slice) |
| 376 | + fn from(xs: &'a mut [[A; N]]) -> Self { |
| 377 | + let cols = N; |
| 378 | + let rows = xs.len(); |
| 379 | + let dim = Ix2(rows, cols); |
| 380 | + if size_of::<A>() == 0 { |
| 381 | + dimension::size_of_shape_checked(&dim) |
| 382 | + .expect("Product of non-zero axis lengths must not overflow isize."); |
| 383 | + } |
| 384 | + |
| 385 | + // `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in |
| 386 | + // `isize::MAX` |
| 387 | + unsafe { |
| 388 | + let data = slice::from_raw_parts_mut(xs.as_mut_ptr() as *mut A, cols * rows); |
| 389 | + ArrayViewMut::from_shape_ptr(dim, data.as_mut_ptr()) |
| 390 | + } |
362 | 391 | } |
363 | 392 | } |
364 | 393 |
|
|
0 commit comments