ndarray/impl_clone.rs
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
9use crate::imp_prelude::*;
10use crate::ArrayPartsSized;
11use crate::RawDataClone;
12
13impl<S: RawDataClone, D: Clone> Clone for ArrayBase<S, D>
14{
15 fn clone(&self) -> ArrayBase<S, D>
16 {
17 // safe because `clone_with_ptr` promises to provide equivalent data and ptr
18 unsafe {
19 let (data, ptr) = self.data.clone_with_ptr(self.parts.ptr);
20 ArrayBase {
21 data,
22 parts: ArrayPartsSized::new(ptr, self.parts.dim.clone(), self.parts.strides.clone()),
23 }
24 }
25 }
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 fn clone_from(&mut self, other: &Self)
31 {
32 unsafe {
33 self.parts.ptr = self.data.clone_from_with_ptr(&other.data, other.parts.ptr);
34 self.parts.dim.clone_from(&other.parts.dim);
35 self.parts.strides.clone_from(&other.parts.strides);
36 }
37 }
38}
39
40impl<S: RawDataClone + Copy, D: Copy> Copy for ArrayBase<S, D> {}