Skip to content

Commit ccfedf0

Browse files
committed
FEAT: Add methods .cell_view() and .into_cell_view()
1 parent 79392bb commit ccfedf0

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/impl_methods.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9+
use std::cell::Cell;
910
use std::ptr as std_ptr;
1011
use std::slice;
1112

@@ -151,6 +152,20 @@ where
151152
unsafe { ArrayViewMut::new(self.ptr, self.dim.clone(), self.strides.clone()) }
152153
}
153154

155+
/// Return a shared view of the array with elements as if they were embedded in cells.
156+
///
157+
/// The cell view requires a mutable borrow of the array. Once borrowed the
158+
/// cell view itself can be copied and accessed without exclusivity.
159+
///
160+
/// The view acts "as if" the elements are temporarily in cells, and elements
161+
/// can be changed through shared references using the regular cell methods.
162+
pub fn cell_view(&mut self) -> ArrayView<'_, Cell<A>, D>
163+
where
164+
S: DataMut,
165+
{
166+
self.view_mut().into_cell_view()
167+
}
168+
154169
/// Return an uniquely owned copy of the array.
155170
///
156171
/// If the input array is contiguous and its strides are positive, then the

src/impl_views/conversions.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9+
use std::cell::Cell;
910
use std::slice;
1011

1112
use crate::imp_prelude::*;
@@ -117,6 +118,21 @@ where
117118
pub fn into_slice(self) -> Option<&'a mut [A]> {
118119
self.try_into_slice().ok()
119120
}
121+
122+
/// Return a shared view of the array with elements as if they were embedded in cells.
123+
///
124+
/// The cell view itself can be copied and accessed without exclusivity.
125+
///
126+
/// The view acts "as if" the elements are temporarily in cells, and elements
127+
/// can be changed through shared references using the regular cell methods.
128+
pub fn into_cell_view(self) -> ArrayView<'a, Cell<A>, D> {
129+
// safety: valid because
130+
// A and Cell<A> have the same representation
131+
// &'a mut T is interchangeable with &'a Cell<T> -- see method Cell::from_mut
132+
unsafe {
133+
self.into_raw_view_mut().cast::<Cell<A>>().deref_into_view()
134+
}
135+
}
120136
}
121137

122138
/// Private array view methods

tests/views.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use ndarray::prelude::*;
2+
use ndarray::Zip;
3+
4+
#[test]
5+
fn cell_view() {
6+
let mut a = Array::from_shape_fn((10, 5), |(i, j)| (i * j) as f32);
7+
let answer = &a + 1.;
8+
9+
{
10+
let cv1 = a.cell_view();
11+
let cv2 = cv1;
12+
13+
Zip::from(cv1).and(cv2).apply(|a, b| a.set(b.get() + 1.));
14+
}
15+
assert_eq!(a, answer);
16+
}

0 commit comments

Comments
 (0)