From ff4e44a8ed808384684bb477348ad08d7af2ffb1 Mon Sep 17 00:00:00 2001 From: Jack Wrenn Date: Tue, 6 Nov 2018 19:46:28 -0500 Subject: [PATCH] Introduces a `from_vec` constructor for `MatrixMN`. Addresses #378. The implementation of `from_vec` simply wraps a call to `from_iterator_generic`. Because the standard library's implementation of `into_iter` is specialized for the `.into_iter().collect>()` case (see https://github.com/rust-lang/rust/issues/46084#issuecomment-345524508), this is equivalent to re-using the original Vec as storage. --- src/base/construction.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/base/construction.rs b/src/base/construction.rs index 52e99cd7..9c10fbe5 100644 --- a/src/base/construction.rs +++ b/src/base/construction.rs @@ -257,6 +257,13 @@ where DefaultAllocator: Allocator { Self::from_fn_generic(nrows, ncols, |_, _| distribution.sample(rng)) } + + /// Creates a matrix backed by a given vector. + #[inline] + #[cfg(feature = "std")] + pub fn from_vec(nrows: R, ncols: C, data: Vec) -> Self { + Self::from_iterator_generic(nrows, ncols, data) + } } impl MatrixN