Skip to content

Commit

Permalink
Refactor aview2 and aview_mut2 implementations into From
Browse files Browse the repository at this point in the history
  • Loading branch information
YuhanLiin committed Apr 15, 2021
1 parent 7b1cc36 commit 999ec8c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
51 changes: 40 additions & 11 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
// except according to those terms.

use alloc::vec::Vec;
use std::hash;
use std::iter::FromIterator;
use std::iter::IntoIterator;
use std::mem;
use std::ops::{Index, IndexMut};
use std::{hash, mem::size_of};
use std::{iter::FromIterator, slice};

use crate::imp_prelude::*;
use crate::iter::{Iter, IterMut};
use crate::NdIndex;
use crate::{
aview_mut2,
iter::{Iter, IterMut},
};
use crate::{dimension, imp_prelude::*};

use crate::numeric_util;
use crate::{FoldWhile, Zip};
Expand Down Expand Up @@ -315,10 +312,26 @@ where
}

/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array
///
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
impl<'a, A, const N: usize> From<&'a [[A; N]]> for ArrayView<'a, A, Ix2> {
/// Create a two-dimensional read-only array view of the data in `slice`
fn from(slice: &'a [[A; N]]) -> Self {
aview2(slice)
fn from(xs: &'a [[A; N]]) -> Self {
let cols = N;
let rows = xs.len();
let dim = Ix2(rows, cols);
if size_of::<A>() == 0 {
dimension::size_of_shape_checked(&dim)
.expect("Product of non-zero axis lengths must not overflow isize.");
}

// `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in
// `isize::MAX`
unsafe {
let data = slice::from_raw_parts(xs.as_ptr() as *const A, cols * rows);
ArrayView::from_shape_ptr(dim, data.as_ptr())
}
}
}

Expand Down Expand Up @@ -355,10 +368,26 @@ where
}

/// Implementation of ArrayViewMut2::from(&S) where S is a slice to a 2D array
///
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
impl<'a, A, const N: usize> From<&'a mut [[A; N]]> for ArrayViewMut<'a, A, Ix2> {
/// Create a two-dimensional read-write array view of the data in `slice`
fn from(slice: &'a mut [[A; N]]) -> Self {
aview_mut2(slice)
fn from(xs: &'a mut [[A; N]]) -> Self {
let cols = N;
let rows = xs.len();
let dim = Ix2(rows, cols);
if size_of::<A>() == 0 {
dimension::size_of_shape_checked(&dim)
.expect("Product of non-zero axis lengths must not overflow isize.");
}

// `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in
// `isize::MAX`
unsafe {
let data = slice::from_raw_parts_mut(xs.as_mut_ptr() as *mut A, cols * rows);
ArrayViewMut::from_shape_ptr(dim, data.as_mut_ptr())
}
}
}

Expand Down
31 changes: 2 additions & 29 deletions src/free_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use alloc::slice;
use alloc::vec;
use alloc::vec::Vec;
use std::mem::{forget, size_of};
Expand Down Expand Up @@ -90,20 +89,7 @@ pub fn aview1<A>(xs: &[A]) -> ArrayView1<'_, A> {
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
pub fn aview2<A, const N: usize>(xs: &[[A; N]]) -> ArrayView2<'_, A> {
let cols = N;
let rows = xs.len();
let dim = Ix2(rows, cols);
if size_of::<A>() == 0 {
dimension::size_of_shape_checked(&dim)
.expect("Product of non-zero axis lengths must not overflow isize.");
}

// `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in
// `isize::MAX`
unsafe {
let data = slice::from_raw_parts(xs.as_ptr() as *const A, cols * rows);
ArrayView::from_shape_ptr(dim, data.as_ptr())
}
ArrayView2::from(xs)
}

/// Create a one-dimensional read-write array view with elements borrowing `xs`.
Expand Down Expand Up @@ -145,20 +131,7 @@ pub fn aview_mut1<A>(xs: &mut [A]) -> ArrayViewMut1<'_, A> {
/// assert_eq!(&data[..3], [[1., -1.], [1., -1.], [1., -1.]]);
/// ```
pub fn aview_mut2<A, const N: usize>(xs: &mut [[A; N]]) -> ArrayViewMut2<'_, A> {
let cols = N;
let rows = xs.len();
let dim = Ix2(rows, cols);
if size_of::<A>() == 0 {
dimension::size_of_shape_checked(&dim)
.expect("Product of non-zero axis lengths must not overflow isize.");
}

// `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in
// `isize::MAX`
unsafe {
let data = slice::from_raw_parts_mut(xs.as_mut_ptr() as *mut A, cols * rows);
ArrayViewMut::from_shape_ptr(dim, data.as_mut_ptr())
}
ArrayViewMut2::from(xs)
}

/// Create a two-dimensional array with elements from `xs`.
Expand Down

0 comments on commit 999ec8c

Please # to comment.