Skip to content

Commit 24c6786

Browse files
committed
Allow passing owned slice arg to slicing methods
This allows the `s![]` macro to return an owned type, without requiring an explicit `&` when calling slicing methods.
1 parent 9c072be commit 24c6786

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

src/dimension/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,8 @@ fn slice_min_max(axis_len: usize, slice: Slice) -> Option<(usize, usize)> {
599599
/// Returns `true` iff the slices intersect.
600600
pub fn slices_intersect<D: Dimension>(
601601
dim: &D,
602-
indices1: &impl SliceArg<D>,
603-
indices2: &impl SliceArg<D>,
602+
indices1: impl SliceArg<D>,
603+
indices2: impl SliceArg<D>,
604604
) -> bool {
605605
debug_assert_eq!(indices1.in_ndim(), indices2.in_ndim());
606606
for (&axis_len, &si1, &si2) in izip!(

src/impl_methods.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ where
338338
///
339339
/// **Panics** if an index is out of bounds or step size is zero.<br>
340340
/// (**Panics** if `D` is `IxDyn` and `info` does not match the number of array axes.)
341-
pub fn slice<I>(&self, info: &I) -> ArrayView<'_, A, I::OutDim>
341+
pub fn slice<I>(&self, info: I) -> ArrayView<'_, A, I::OutDim>
342342
where
343-
I: SliceArg<D> + ?Sized,
343+
I: SliceArg<D>,
344344
S: Data,
345345
{
346346
self.view().slice_move(info)
@@ -353,9 +353,9 @@ where
353353
///
354354
/// **Panics** if an index is out of bounds or step size is zero.<br>
355355
/// (**Panics** if `D` is `IxDyn` and `info` does not match the number of array axes.)
356-
pub fn slice_mut<I>(&mut self, info: &I) -> ArrayViewMut<'_, A, I::OutDim>
356+
pub fn slice_mut<I>(&mut self, info: I) -> ArrayViewMut<'_, A, I::OutDim>
357357
where
358-
I: SliceArg<D> + ?Sized,
358+
I: SliceArg<D>,
359359
S: DataMut,
360360
{
361361
self.view_mut().slice_move(info)
@@ -399,9 +399,9 @@ where
399399
///
400400
/// **Panics** if an index is out of bounds or step size is zero.<br>
401401
/// (**Panics** if `D` is `IxDyn` and `info` does not match the number of array axes.)
402-
pub fn slice_move<I>(mut self, info: &I) -> ArrayBase<S, I::OutDim>
402+
pub fn slice_move<I>(mut self, info: I) -> ArrayBase<S, I::OutDim>
403403
where
404-
I: SliceArg<D> + ?Sized,
404+
I: SliceArg<D>,
405405
{
406406
assert_eq!(
407407
info.in_ndim(),
@@ -468,9 +468,9 @@ where
468468
/// - if [`AxisSliceInfo::NewAxis`] is in `info`, e.g. if [`NewAxis`] was
469469
/// used in the [`s!`] macro
470470
/// - if `D` is `IxDyn` and `info` does not match the number of array axes
471-
pub fn slice_collapse<I>(&mut self, info: &I)
471+
pub fn slice_collapse<I>(&mut self, info: I)
472472
where
473-
I: SliceArg<D> + ?Sized,
473+
I: SliceArg<D>,
474474
{
475475
assert_eq!(
476476
info.in_ndim(),

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,8 @@ pub type Ixs = isize;
492492
///
493493
/// The slicing argument can be passed using the macro [`s![]`](macro.s!.html),
494494
/// which will be used in all examples. (The explicit form is an instance of
495-
/// [`&SliceInfo`]; see its docs for more information.)
496-
///
497-
/// [`&SliceInfo`]: struct.SliceInfo.html
495+
/// [`SliceInfo`] or another type which implements [`SliceArg`]; see their docs
496+
/// for more information.)
498497
///
499498
/// If a range is used, the axis is preserved. If an index is used, that index
500499
/// is selected and the axis is removed; this selects a subview. See
@@ -510,7 +509,7 @@ pub type Ixs = isize;
510509
/// [`NewAxis`]: struct.NewAxis.html
511510
///
512511
/// When slicing arrays with generic dimensionality, creating an instance of
513-
/// [`&SliceInfo`] to pass to the multi-axis slicing methods like [`.slice()`]
512+
/// [`SliceInfo`] to pass to the multi-axis slicing methods like [`.slice()`]
514513
/// is awkward. In these cases, it's usually more convenient to use
515514
/// [`.slice_each_axis()`]/[`.slice_each_axis_mut()`]/[`.slice_each_axis_inplace()`]
516515
/// or to create a view and then slice individual axes of the view using

src/slice.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct NewAxis;
7878
/// A slice (range with step), an index, or a new axis token.
7979
///
8080
/// See also the [`s![]`](macro.s!.html) macro for a convenient way to create a
81-
/// `&SliceInfo<[AxisSliceInfo; n], Din, Dout>`.
81+
/// `SliceInfo<[AxisSliceInfo; n], Din, Dout>`.
8282
///
8383
/// ## Examples
8484
///
@@ -721,9 +721,7 @@ impl_slicenextdim!((), NewAxis, Ix0, Ix1);
721721
///
722722
/// `s![]` takes a list of ranges/slices/indices/new-axes, separated by comma,
723723
/// with optional step sizes that are separated from the range by a semicolon.
724-
/// It is converted into a [`&SliceInfo`] instance.
725-
///
726-
/// [`&SliceInfo`]: struct.SliceInfo.html
724+
/// It is converted into a [`SliceInfo`] instance.
727725
///
728726
/// Each range/slice/index uses signed indices, where a negative value is
729727
/// counted from the end of the axis. Step sizes are also signed and may be
@@ -907,9 +905,7 @@ macro_rules! s(
907905
<$crate::AxisSliceInfo as ::std::convert::From<_>>::from($r).step_by($s as isize)
908906
};
909907
($($t:tt)*) => {
910-
// The extra `*&` is a workaround for this compiler bug:
911-
// https://github.com/rust-lang/rust/issues/23014
912-
&*&$crate::s![@parse
908+
$crate::s![@parse
913909
::std::marker::PhantomData::<$crate::Ix0>,
914910
::std::marker::PhantomData::<$crate::Ix0>,
915911
[]
@@ -951,7 +947,7 @@ where
951947
private_impl! {}
952948
}
953949

954-
impl<'a, A, D, I0> MultiSliceArg<'a, A, D> for (&I0,)
950+
impl<'a, A, D, I0> MultiSliceArg<'a, A, D> for (I0,)
955951
where
956952
A: 'a,
957953
D: Dimension,
@@ -960,7 +956,7 @@ where
960956
type Output = (ArrayViewMut<'a, A, I0::OutDim>,);
961957

962958
fn multi_slice_move(&self, view: ArrayViewMut<'a, A, D>) -> Self::Output {
963-
(view.slice_move(self.0),)
959+
(view.slice_move(&self.0),)
964960
}
965961

966962
private_impl! {}
@@ -971,7 +967,7 @@ macro_rules! impl_multislice_tuple {
971967
impl_multislice_tuple!(@def_impl ($($but_last,)* $last,), [$($but_last)*] $last);
972968
};
973969
(@def_impl ($($all:ident,)*), [$($but_last:ident)*] $last:ident) => {
974-
impl<'a, A, D, $($all,)*> MultiSliceArg<'a, A, D> for ($(&$all,)*)
970+
impl<'a, A, D, $($all,)*> MultiSliceArg<'a, A, D> for ($($all,)*)
975971
where
976972
A: 'a,
977973
D: Dimension,
@@ -981,7 +977,7 @@ macro_rules! impl_multislice_tuple {
981977

982978
fn multi_slice_move(&self, view: ArrayViewMut<'a, A, D>) -> Self::Output {
983979
#[allow(non_snake_case)]
984-
let &($($all,)*) = self;
980+
let ($($all,)*) = self;
985981

986982
let shape = view.raw_dim();
987983
assert!(!impl_multislice_tuple!(@intersects_self &shape, ($($all,)*)));

0 commit comments

Comments
 (0)