Skip to content

Commit d00022a

Browse files
committed
Add RcArray::into_owned() -> Array
1 parent 0d54e8f commit d00022a

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/impl_owned_array.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
use std::rc::Rc;
3+
24
use imp_prelude::*;
35

46
impl<A, D> ArrayBase<Vec<A>, D>
@@ -13,3 +15,22 @@ impl<A, D> ArrayBase<Vec<A>, D>
1315
self.data
1416
}
1517
}
18+
19+
// RcArray
20+
impl<A, D> ArrayBase<Rc<Vec<A>>, D>
21+
where A: Clone,
22+
D: Dimension
23+
{
24+
/// Convert an `RcArray` into `Array`; cloning the array elements to unshare
25+
/// them if necessary.
26+
pub fn into_owned(mut self) -> Array<A, D> {
27+
<_>::ensure_unique(&mut self);
28+
let data = Rc::try_unwrap(self.data).ok().unwrap();
29+
ArrayBase {
30+
data: data,
31+
ptr: self.ptr,
32+
dim: self.dim,
33+
strides: self.strides,
34+
}
35+
}
36+
}

‎tests/array-construct.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ fn test_dimension_zero() {
1818
let a: Array3<f32> = Array3::from(vec![[[]], [[]], [[]]]);
1919
assert_eq!(vec![0.; 0], a.into_raw_vec());
2020
}
21+
22+
#[test]
23+
fn test_rc_into_owned() {
24+
let a = Array2::from_elem((5, 5), 1.).into_shared();
25+
let mut b = a.clone();
26+
b.fill(0.);
27+
let mut c = b.into_owned();
28+
c.fill(2.);
29+
// test that they are unshared
30+
assert!(!a.all_close(&c, 0.01));
31+
}

0 commit comments

Comments
 (0)