|
6 | 6 | // option. This file may not be copied, modified, or distributed |
7 | 7 | // except according to those terms. |
8 | 8 |
|
9 | | -use std::hash; |
10 | | -use std::iter::FromIterator; |
| 9 | +use alloc::boxed::Box; |
| 10 | +use alloc::vec::Vec; |
11 | 11 | use std::iter::IntoIterator; |
12 | 12 | use std::mem; |
13 | 13 | use std::ops::{Index, IndexMut}; |
14 | | -use alloc::boxed::Box; |
15 | | -use alloc::vec::Vec; |
| 14 | +use std::{hash, mem::size_of}; |
| 15 | +use std::{iter::FromIterator, slice}; |
16 | 16 |
|
17 | 17 | use crate::imp_prelude::*; |
18 | | -use crate::iter::{Iter, IterMut}; |
19 | | -use crate::NdIndex; |
20 | | - |
21 | | -use crate::numeric_util; |
22 | | -use crate::{FoldWhile, Zip}; |
| 18 | +use crate::{ |
| 19 | + dimension, |
| 20 | + iter::{Iter, IterMut}, |
| 21 | + numeric_util, FoldWhile, NdIndex, Zip, |
| 22 | +}; |
23 | 23 |
|
24 | 24 | #[cold] |
25 | 25 | #[inline(never)] |
@@ -323,6 +323,30 @@ where |
323 | 323 | } |
324 | 324 | } |
325 | 325 |
|
| 326 | +/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array |
| 327 | +/// |
| 328 | +/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A |
| 329 | +/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes). |
| 330 | +impl<'a, A, const N: usize> From<&'a [[A; N]]> for ArrayView<'a, A, Ix2> { |
| 331 | + /// Create a two-dimensional read-only array view of the data in `slice` |
| 332 | + fn from(xs: &'a [[A; N]]) -> Self { |
| 333 | + let cols = N; |
| 334 | + let rows = xs.len(); |
| 335 | + let dim = Ix2(rows, cols); |
| 336 | + if size_of::<A>() == 0 { |
| 337 | + dimension::size_of_shape_checked(&dim) |
| 338 | + .expect("Product of non-zero axis lengths must not overflow isize."); |
| 339 | + } |
| 340 | + |
| 341 | + // `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in |
| 342 | + // `isize::MAX` |
| 343 | + unsafe { |
| 344 | + let data = slice::from_raw_parts(xs.as_ptr() as *const A, cols * rows); |
| 345 | + ArrayView::from_shape_ptr(dim, data.as_ptr()) |
| 346 | + } |
| 347 | + } |
| 348 | +} |
| 349 | + |
326 | 350 | /// Implementation of `ArrayView::from(&A)` where `A` is an array. |
327 | 351 | impl<'a, A, S, D> From<&'a ArrayBase<S, D>> for ArrayView<'a, A, D> |
328 | 352 | where |
@@ -355,6 +379,30 @@ where |
355 | 379 | } |
356 | 380 | } |
357 | 381 |
|
| 382 | +/// Implementation of ArrayViewMut2::from(&S) where S is a slice to a 2D array |
| 383 | +/// |
| 384 | +/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A |
| 385 | +/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes). |
| 386 | +impl<'a, A, const N: usize> From<&'a mut [[A; N]]> for ArrayViewMut<'a, A, Ix2> { |
| 387 | + /// Create a two-dimensional read-write array view of the data in `slice` |
| 388 | + fn from(xs: &'a mut [[A; N]]) -> Self { |
| 389 | + let cols = N; |
| 390 | + let rows = xs.len(); |
| 391 | + let dim = Ix2(rows, cols); |
| 392 | + if size_of::<A>() == 0 { |
| 393 | + dimension::size_of_shape_checked(&dim) |
| 394 | + .expect("Product of non-zero axis lengths must not overflow isize."); |
| 395 | + } |
| 396 | + |
| 397 | + // `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in |
| 398 | + // `isize::MAX` |
| 399 | + unsafe { |
| 400 | + let data = slice::from_raw_parts_mut(xs.as_mut_ptr() as *mut A, cols * rows); |
| 401 | + ArrayViewMut::from_shape_ptr(dim, data.as_mut_ptr()) |
| 402 | + } |
| 403 | + } |
| 404 | +} |
| 405 | + |
358 | 406 | /// Implementation of `ArrayViewMut::from(&mut A)` where `A` is an array. |
359 | 407 | impl<'a, A, S, D> From<&'a mut ArrayBase<S, D>> for ArrayViewMut<'a, A, D> |
360 | 408 | where |
|
0 commit comments