Skip to content

Add back Array::from_vec and Array::from_iter #921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// except according to those terms.

use std::hash;
use std::isize;
use std::iter::FromIterator;
use std::iter::IntoIterator;
use std::mem;
Expand Down Expand Up @@ -135,13 +134,7 @@ where
/// let array = Array::from(vec![1., 2., 3., 4.]);
/// ```
fn from(v: Vec<A>) -> Self {
if mem::size_of::<A>() == 0 {
assert!(
v.len() <= isize::MAX as usize,
"Length must fit in `isize`.",
);
}
unsafe { Self::from_shape_vec_unchecked(v.len() as Ix, v) }
Self::from_vec(v)
}
}

Expand All @@ -165,7 +158,7 @@ where
where
I: IntoIterator<Item = A>,
{
Self::from(iterable.into_iter().collect::<Vec<A>>())
Self::from_iter(iterable)
}
}

Expand Down
25 changes: 22 additions & 3 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#[cfg(feature = "std")]
use num_traits::Float;
use num_traits::{One, Zero};
use std::mem;
use std::mem::MaybeUninit;
use alloc::vec;
use alloc::vec::Vec;
Expand Down Expand Up @@ -51,11 +52,29 @@ where
/// ```rust
/// use ndarray::Array;
///
/// let array = Array::from(vec![1., 2., 3., 4.]);
/// let array = Array::from_vec(vec![1., 2., 3., 4.]);
/// ```
#[deprecated(note = "use standard `from`", since = "0.13.0")]
pub fn from_vec(v: Vec<A>) -> Self {
Self::from(v)
if mem::size_of::<A>() == 0 {
assert!(
v.len() <= isize::MAX as usize,
"Length must fit in `isize`.",
);
}
unsafe { Self::from_shape_vec_unchecked(v.len() as Ix, v) }
}

/// Create a one-dimensional array from an iterator or iterable.
///
/// **Panics** if the length is greater than `isize::MAX`.
///
/// ```rust
/// use ndarray::Array;
///
/// let array = Array::from_iter(0..10);
/// ```
pub fn from_iter<I: IntoIterator<Item = A>>(iterable: I) -> Self {
Self::from_vec(iterable.into_iter().collect())
}

/// Create a one-dimensional array with `n` evenly spaced elements from
Expand Down