Skip to content

Commit b63f11c

Browse files
committed
Enable cloning of shared borrows into NumPy arrays.
1 parent 865f7b0 commit b63f11c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/borrow.rs

+10
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ where
275275
}
276276
}
277277

278+
impl<'a, T, D> Clone for PyReadonlyArray<'a, T, D>
279+
where
280+
T: Element,
281+
D: Dimension,
282+
{
283+
fn clone(&self) -> Self {
284+
Self::try_new(self.0).unwrap()
285+
}
286+
}
287+
278288
impl<'a, T, D> Drop for PyReadonlyArray<'a, T, D> {
279289
fn drop(&mut self) {
280290
let address = base_address(self.0);

tests/borrow.rs

+13
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ fn borrows_span_threads() {
115115
});
116116
}
117117

118+
#[test]
119+
fn shared_borrows_can_be_cloned() {
120+
Python::with_gil(|py| {
121+
let array = PyArray::<f64, _>::zeros(py, (1, 2, 3), false);
122+
123+
let shared1 = array.readonly();
124+
let shared2 = shared1.clone();
125+
126+
assert_eq!(shared2.shape(), [1, 2, 3]);
127+
assert_eq!(shared1.shape(), [1, 2, 3]);
128+
});
129+
}
130+
118131
#[test]
119132
#[should_panic(expected = "AlreadyBorrowed")]
120133
fn overlapping_views_conflict() {

0 commit comments

Comments
 (0)