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::RawDataClone;
11
12impl<S: RawDataClone, D: Clone> Clone for ArrayBase<S, D> {
13 fn clone(&self) -> ArrayBase<S, D> {
14 // safe because `clone_with_ptr` promises to provide equivalent data and ptr
15 unsafe {
16 let (data, ptr) = self.data.clone_with_ptr(self.ptr);
17 ArrayBase {
18 data,
19 ptr,
20 dim: self.dim.clone(),
21 strides: self.strides.clone(),
22 }
23 }
24 }
25
26 /// `Array` implements `.clone_from()` to reuse an array's existing
27 /// allocation. Semantically equivalent to `*self = other.clone()`, but
28 /// potentially more efficient.
29 fn clone_from(&mut self, other: &Self) {
30 unsafe {
31 self.ptr = self.data.clone_from_with_ptr(&other.data, other.ptr);
32 self.dim.clone_from(&other.dim);
33 self.strides.clone_from(&other.strides);
34 }
35 }
36}
37
38impl<S: RawDataClone + Copy, D: Copy> Copy for ArrayBase<S, D> {}