ndarray/impl_cow.rs
1// Copyright 2019 ndarray developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::imp_prelude::*;
10
11/// Methods specific to `CowArray`.
12///
13/// ***See also all methods for [`ArrayBase`]***
14///
15/// [`ArrayBase`]: struct.ArrayBase.html
16impl<'a, A, D> CowArray<'a, A, D>
17where
18 D: Dimension,
19{
20 /// Returns `true` iff the array is the view (borrowed) variant.
21 pub fn is_view(&self) -> bool {
22 self.data.is_view()
23 }
24
25 /// Returns `true` iff the array is the owned variant.
26 pub fn is_owned(&self) -> bool {
27 self.data.is_owned()
28 }
29}
30
31impl<'a, A, D> From<ArrayView<'a, A, D>> for CowArray<'a, A, D>
32where
33 D: Dimension,
34{
35 fn from(view: ArrayView<'a, A, D>) -> CowArray<'a, A, D> {
36 // safe because equivalent data
37 unsafe {
38 ArrayBase::from_data_ptr(CowRepr::View(view.data), view.ptr)
39 .with_strides_dim(view.strides, view.dim)
40 }
41 }
42}
43
44impl<'a, A, D> From<Array<A, D>> for CowArray<'a, A, D>
45where
46 D: Dimension,
47{
48 fn from(array: Array<A, D>) -> CowArray<'a, A, D> {
49 // safe because equivalent data
50 unsafe {
51 ArrayBase::from_data_ptr(CowRepr::Owned(array.data), array.ptr)
52 .with_strides_dim(array.strides, array.dim)
53 }
54 }
55}