/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ndarray-0.17.2/src/impl_clone.rs
Line | Count | Source |
1 | | // Copyright 2014-2016 bluss and 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 | | |
9 | | use crate::imp_prelude::*; |
10 | | use crate::ArrayPartsSized; |
11 | | use crate::RawDataClone; |
12 | | |
13 | | impl<S: RawDataClone, D: Clone> Clone for ArrayBase<S, D> |
14 | | { |
15 | 0 | fn clone(&self) -> ArrayBase<S, D> |
16 | | { |
17 | | // safe because `clone_with_ptr` promises to provide equivalent data and ptr |
18 | | unsafe { |
19 | 0 | let (data, ptr) = self.data.clone_with_ptr(self.parts.ptr); |
20 | 0 | ArrayBase { |
21 | 0 | data, |
22 | 0 | parts: ArrayPartsSized::new(ptr, self.parts.dim.clone(), self.parts.strides.clone()), |
23 | 0 | } |
24 | | } |
25 | 0 | } |
26 | | |
27 | | /// `Array` implements `.clone_from()` to reuse an array's existing |
28 | | /// allocation. Semantically equivalent to `*self = other.clone()`, but |
29 | | /// potentially more efficient. |
30 | 0 | fn clone_from(&mut self, other: &Self) |
31 | | { |
32 | 0 | unsafe { |
33 | 0 | self.parts.ptr = self.data.clone_from_with_ptr(&other.data, other.parts.ptr); |
34 | 0 | self.parts.dim.clone_from(&other.parts.dim); |
35 | 0 | self.parts.strides.clone_from(&other.parts.strides); |
36 | 0 | } |
37 | 0 | } |
38 | | } |
39 | | |
40 | | impl<S: RawDataClone + Copy, D: Copy> Copy for ArrayBase<S, D> {} |