Skip to content

Commit cbed837

Browse files
committed
Makes as_ref more explicit through methods on ArrayBase
1 parent 0a3cad3 commit cbed837

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

src/alias_asref.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<A, S: RawData<Elem = A>, D: Dimension> ArrayBase<S, D>
3333
pub fn get_ptr<I>(&self, index: I) -> Option<*const A>
3434
where I: NdIndex<D>
3535
{
36-
<Self as AsRef<RawRef<_, _>>>::as_ref(self).get_ptr(index)
36+
self.as_raw_ref().get_ptr(index)
3737
}
3838

3939
/// Return a raw pointer to the element at `index`, or return `None`
@@ -58,7 +58,7 @@ impl<A, S: RawData<Elem = A>, D: Dimension> ArrayBase<S, D>
5858
S: RawDataMut<Elem = A>,
5959
I: NdIndex<D>,
6060
{
61-
<Self as AsMut<RawRef<_, _>>>::as_mut(self).get_mut_ptr(index)
61+
self.as_raw_ref_mut().get_mut_ptr(index)
6262
}
6363

6464
/// Return a pointer to the first element in the array.
@@ -73,14 +73,14 @@ impl<A, S: RawData<Elem = A>, D: Dimension> ArrayBase<S, D>
7373
#[inline(always)]
7474
pub fn as_ptr(&self) -> *const A
7575
{
76-
<Self as AsRef<RawRef<_, _>>>::as_ref(self).as_ptr()
76+
self.as_raw_ref().as_ptr()
7777
}
7878

7979
/// Return a raw view of the array.
8080
#[inline]
8181
pub fn raw_view(&self) -> RawArrayView<S::Elem, D>
8282
{
83-
<Self as AsRef<RawRef<_, _>>>::as_ref(self).raw_view()
83+
self.as_raw_ref().raw_view()
8484
}
8585
}
8686

@@ -111,7 +111,7 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
111111
pub fn slice_collapse<I>(&mut self, info: I)
112112
where I: SliceArg<D>
113113
{
114-
self.as_mut().slice_collapse(info);
114+
self.as_layout_ref_mut().slice_collapse(info);
115115
}
116116

117117
/// Slice the array in place along the specified axis.
@@ -121,7 +121,7 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
121121
#[track_caller]
122122
pub fn slice_axis_inplace(&mut self, axis: Axis, indices: Slice)
123123
{
124-
self.as_mut().slice_axis_inplace(axis, indices);
124+
self.as_layout_ref_mut().slice_axis_inplace(axis, indices);
125125
}
126126

127127
/// Slice the array in place, with a closure specifying the slice for each
@@ -135,7 +135,7 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
135135
pub fn slice_each_axis_inplace<F>(&mut self, f: F)
136136
where F: FnMut(AxisDescription) -> Slice
137137
{
138-
self.as_mut().slice_each_axis_inplace(f);
138+
self.as_layout_ref_mut().slice_each_axis_inplace(f);
139139
}
140140

141141
/// Selects `index` along the axis, collapsing the axis into length one.
@@ -144,7 +144,7 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
144144
#[track_caller]
145145
pub fn collapse_axis(&mut self, axis: Axis, index: usize)
146146
{
147-
self.as_mut().collapse_axis(axis, index);
147+
self.as_layout_ref_mut().collapse_axis(axis, index);
148148
}
149149

150150
/// Return `true` if the array data is laid out in contiguous “C order” in
@@ -154,19 +154,19 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
154154
/// contiguous in memory, it has custom strides, etc.
155155
pub fn is_standard_layout(&self) -> bool
156156
{
157-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).is_standard_layout()
157+
self.as_layout_ref().is_standard_layout()
158158
}
159159

160160
/// Return true if the array is known to be contiguous.
161161
pub(crate) fn is_contiguous(&self) -> bool
162162
{
163-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).is_contiguous()
163+
self.as_layout_ref().is_contiguous()
164164
}
165165

166166
/// Return an iterator over the length and stride of each axis.
167167
pub fn axes(&self) -> Axes<'_, D>
168168
{
169-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).axes()
169+
self.as_layout_ref().axes()
170170
}
171171

172172
/*
@@ -180,7 +180,7 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
180180
/// preferring axes with len > 1.
181181
pub fn max_stride_axis(&self) -> Axis
182182
{
183-
LayoutRef::max_stride_axis(self.as_ref())
183+
self.as_layout_ref().max_stride_axis()
184184
}
185185

186186
/// Reverse the stride of `axis`.
@@ -189,7 +189,7 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
189189
#[track_caller]
190190
pub fn invert_axis(&mut self, axis: Axis)
191191
{
192-
self.as_mut().invert_axis(axis);
192+
self.as_layout_ref_mut().invert_axis(axis);
193193
}
194194

195195
/// Swap axes `ax` and `bx`.
@@ -211,7 +211,7 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
211211
#[track_caller]
212212
pub fn swap_axes(&mut self, ax: usize, bx: usize)
213213
{
214-
self.as_mut().swap_axes(ax, bx);
214+
self.as_layout_ref_mut().swap_axes(ax, bx);
215215
}
216216

217217
/// If possible, merge in the axis `take` to `into`.
@@ -252,13 +252,13 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
252252
#[track_caller]
253253
pub fn merge_axes(&mut self, take: Axis, into: Axis) -> bool
254254
{
255-
self.as_mut().merge_axes(take, into)
255+
self.as_layout_ref_mut().merge_axes(take, into)
256256
}
257257

258258
/// Return the total number of elements in the array.
259259
pub fn len(&self) -> usize
260260
{
261-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).len()
261+
self.as_layout_ref().len()
262262
}
263263

264264
/// Return the length of `axis`.
@@ -270,27 +270,27 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
270270
#[track_caller]
271271
pub fn len_of(&self, axis: Axis) -> usize
272272
{
273-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).len_of(axis)
273+
self.as_layout_ref().len_of(axis)
274274
}
275275

276276
/// Return whether the array has any elements
277277
pub fn is_empty(&self) -> bool
278278
{
279-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).is_empty()
279+
self.as_layout_ref().is_empty()
280280
}
281281

282282
/// Return the number of dimensions (axes) in the array
283283
pub fn ndim(&self) -> usize
284284
{
285-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).ndim()
285+
self.as_layout_ref().ndim()
286286
}
287287

288288
/// Return the shape of the array in its “pattern” form,
289289
/// an integer in the one-dimensional case, tuple in the n-dimensional cases
290290
/// and so on.
291291
pub fn dim(&self) -> D::Pattern
292292
{
293-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).dim()
293+
self.as_layout_ref().dim()
294294
}
295295

296296
/// Return the shape of the array as it's stored in the array.
@@ -309,7 +309,7 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
309309
/// ```
310310
pub fn raw_dim(&self) -> D
311311
{
312-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).raw_dim()
312+
self.as_layout_ref().raw_dim()
313313
}
314314

315315
/// Return the shape of the array as a slice.
@@ -338,13 +338,13 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
338338
/// ```
339339
pub fn shape(&self) -> &[usize]
340340
{
341-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).shape()
341+
self.as_layout_ref().shape()
342342
}
343343

344344
/// Return the strides of the array as a slice.
345345
pub fn strides(&self) -> &[isize]
346346
{
347-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).strides()
347+
self.as_layout_ref().strides()
348348
}
349349

350350
/// Return the stride of `axis`.
@@ -356,6 +356,6 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
356356
#[track_caller]
357357
pub fn stride_of(&self, axis: Axis) -> isize
358358
{
359-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).stride_of(axis)
359+
self.as_layout_ref().stride_of(axis)
360360
}
361361
}

src/impl_2d.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<S: RawData> ArrayBase<S, Ix2>
170170
/// ```
171171
pub fn nrows(&self) -> usize
172172
{
173-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).nrows()
173+
self.as_layout_ref().nrows()
174174
}
175175

176176
/// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
@@ -192,7 +192,7 @@ impl<S: RawData> ArrayBase<S, Ix2>
192192
/// ```
193193
pub fn ncols(&self) -> usize
194194
{
195-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).ncols()
195+
self.as_layout_ref().ncols()
196196
}
197197

198198
/// Return true if the array is square, false otherwise.
@@ -212,6 +212,6 @@ impl<S: RawData> ArrayBase<S, Ix2>
212212
/// ```
213213
pub fn is_square(&self) -> bool
214214
{
215-
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).is_square()
215+
self.as_layout_ref().is_square()
216216
}
217217
}

src/impl_owned_array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ where D: Dimension
747747
sort_axes_in_default_order_tandem(&mut tail_view, &mut array);
748748
debug_assert!(tail_view.is_standard_layout(),
749749
"not std layout dim: {:?}, strides: {:?}",
750-
tail_view.shape(), LayoutRef::strides(tail_view.as_ref()));
750+
tail_view.shape(), tail_view.strides());
751751
}
752752

753753
// Keep track of currently filled length of `self.data` and update it

src/impl_ref_types.rs

+30
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,33 @@ where
338338
target.zip_mut_with(self, |tgt, src| tgt.clone_from(src));
339339
}
340340
}
341+
342+
/// Shortcuts for the various as_ref calls
343+
impl<A, S, D> ArrayBase<S, D>
344+
where S: RawData<Elem = A>
345+
{
346+
/// Cheaply convert a reference to the array to an &LayoutRef
347+
pub fn as_layout_ref(&self) -> &LayoutRef<A, D>
348+
{
349+
self.as_ref()
350+
}
351+
352+
/// Cheaply and mutably convert a reference to the array to an &LayoutRef
353+
pub fn as_layout_ref_mut(&mut self) -> &mut LayoutRef<A, D>
354+
{
355+
self.as_mut()
356+
}
357+
358+
/// Cheaply convert a reference to the array to an &RawRef
359+
pub fn as_raw_ref(&self) -> &RawRef<A, D>
360+
{
361+
self.as_ref()
362+
}
363+
364+
/// Cheaply and mutably convert a reference to the array to an &RawRef
365+
pub fn as_raw_ref_mut(&mut self) -> &mut RawRef<A, D>
366+
where S: RawDataMut<Elem = A>
367+
{
368+
self.as_mut()
369+
}
370+
}

tests/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2821,7 +2821,7 @@ fn test_split_complex_invert_axis()
28212821
}
28222822

28232823
#[test]
2824-
fn test_slice_assing()
2824+
fn test_slice_assign()
28252825
{
28262826
let mut a = array![0, 1, 2, 3, 4];
28272827
*a.slice_mut(s![1..3]) += 1;

0 commit comments

Comments
 (0)