Skip to content

Implement conversions from &Slice and &ArrayBase for CowArray #1038

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 2 commits into from
Nov 7, 2021
Merged
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
31 changes: 31 additions & 0 deletions src/impl_cow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,34 @@ 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))
}
}

impl<'a, A, S, D> From<&'a ArrayBase<S, D>> for CowArray<'a, A, D>
where
S: Data<Elem = A>,
D: Dimension,
{
/// Create a read-only clone-on-write view of the array.
fn from(array: &'a ArrayBase<S, D>) -> Self {
Self::from(array.view())
}
}