Skip to content

Commit ecc1544

Browse files
committed
Enable cloning of shared borrows into NumPy arrays.
1 parent a1dd622 commit ecc1544

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/borrow.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ where
272272
}
273273
}
274274

275+
impl<'a, T, D> Clone for PyReadonlyArray<'a, T, D>
276+
where
277+
T: Element,
278+
D: Dimension,
279+
{
280+
fn clone(&self) -> Self {
281+
Self::try_new(self.0).unwrap()
282+
}
283+
}
284+
275285
impl<'a, T, D> Drop for PyReadonlyArray<'a, T, D> {
276286
fn drop(&mut self) {
277287
let address = base_address(self.0);

tests/borrow.rs

Lines changed: 13 additions & 0 deletions
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)