From 490b2e2d6a6647172a63b642b68ca8823affe70b Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Fri, 18 Jun 2021 12:24:27 -0400 Subject: [PATCH 1/2] Implement From<&'a Slice> for CowArray<'a, A, Ix1> --- src/impl_cow.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/impl_cow.rs b/src/impl_cow.rs index b880fd62c..ffd7b04f5 100644 --- a/src/impl_cow.rs +++ b/src/impl_cow.rs @@ -53,3 +53,23 @@ where } } } + +impl<'a, A, Slice: ?Sized> From<&'a Slice> for CowArray<'a, A, Ix1> +where + Slice: AsRef<[A]>, +{ + /// Create a one-dimensional clone-on-write view of the data in `slice`. + /// + /// **Panics** if the slice length is greater than [`isize::MAX`]. + /// + /// ``` + /// use ndarray::{array, CowArray}; + /// + /// let array = CowArray::from(&[1., 2., 3., 4.]); + /// assert!(array.is_view()); + /// assert_eq!(array, array![1., 2., 3., 4.]); + /// ``` + fn from(slice: &'a Slice) -> Self { + Self::from(ArrayView1::from(slice)) + } +} From e40a84f10028ffd4ed87ac04c443fc0d175a853d Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Fri, 18 Jun 2021 12:39:00 -0400 Subject: [PATCH 2/2] Implement From<&'a ArrayBase> for CowArray --- src/impl_cow.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/impl_cow.rs b/src/impl_cow.rs index ffd7b04f5..605ba87af 100644 --- a/src/impl_cow.rs +++ b/src/impl_cow.rs @@ -73,3 +73,14 @@ where Self::from(ArrayView1::from(slice)) } } + +impl<'a, A, S, D> From<&'a ArrayBase> for CowArray<'a, A, D> +where + S: Data, + D: Dimension, +{ + /// Create a read-only clone-on-write view of the array. + fn from(array: &'a ArrayBase) -> Self { + Self::from(array.view()) + } +}