Skip to content

Commit 4326b25

Browse files
committed
Make SliceInfo be Sized, and remove pointer casts
The original reason for `SliceInfo` being `?Sized` and these conversions was to work around the limitations of the slicing methods taking a type involving `Dimension::SliceArg`. Now that the slicing methods take `I: SliceArg<D>` as the parameter type, these conversions are no longer necessary, and `SliceInfo` doesn't need to be `?Sized`.
1 parent 24c6786 commit 4326b25

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

src/slice.rs

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ unsafe impl SliceArg<IxDyn> for [AxisSliceInfo] {
406406

407407
/// Represents all of the necessary information to perform a slice.
408408
///
409-
/// The type `T` is typically `[AxisSliceInfo; n]`, `[AxisSliceInfo]`, or
409+
/// The type `T` is typically `[AxisSliceInfo; n]`, `&[AxisSliceInfo]`, or
410410
/// `Vec<AxisSliceInfo>`. The type `Din` is the dimension of the array to be
411411
/// sliced, and `Dout` is the output dimension after calling [`.slice()`]. Note
412412
/// that if `Din` is a fixed dimension type (`Ix0`, `Ix1`, `Ix2`, etc.), the
@@ -415,14 +415,13 @@ unsafe impl SliceArg<IxDyn> for [AxisSliceInfo] {
415415
///
416416
/// [`.slice()`]: struct.ArrayBase.html#method.slice
417417
#[derive(Debug)]
418-
#[repr(transparent)]
419-
pub struct SliceInfo<T: ?Sized, Din: Dimension, Dout: Dimension> {
418+
pub struct SliceInfo<T, Din: Dimension, Dout: Dimension> {
420419
in_dim: PhantomData<Din>,
421420
out_dim: PhantomData<Dout>,
422421
indices: T,
423422
}
424423

425-
impl<T: ?Sized, Din, Dout> Deref for SliceInfo<T, Din, Dout>
424+
impl<T, Din, Dout> Deref for SliceInfo<T, Din, Dout>
426425
where
427426
Din: Dimension,
428427
Dout: Dimension,
@@ -482,14 +481,7 @@ where
482481
indices,
483482
}
484483
}
485-
}
486484

487-
impl<T, Din, Dout> SliceInfo<T, Din, Dout>
488-
where
489-
T: AsRef<[AxisSliceInfo]>,
490-
Din: Dimension,
491-
Dout: Dimension,
492-
{
493485
/// Returns a new `SliceInfo` instance.
494486
///
495487
/// Errors if `Din` or `Dout` is not consistent with `indices`.
@@ -508,14 +500,7 @@ where
508500
indices,
509501
})
510502
}
511-
}
512503

513-
impl<T: ?Sized, Din, Dout> SliceInfo<T, Din, Dout>
514-
where
515-
T: AsRef<[AxisSliceInfo]>,
516-
Din: Dimension,
517-
Dout: Dimension,
518-
{
519504
/// Returns the number of dimensions of the input array for
520505
/// [`.slice()`](struct.ArrayBase.html#method.slice).
521506
///
@@ -546,7 +531,7 @@ where
546531
}
547532
}
548533

549-
impl<'a, Din, Dout> TryFrom<&'a [AxisSliceInfo]> for &'a SliceInfo<[AxisSliceInfo], Din, Dout>
534+
impl<'a, Din, Dout> TryFrom<&'a [AxisSliceInfo]> for SliceInfo<&'a [AxisSliceInfo], Din, Dout>
550535
where
551536
Din: Dimension,
552537
Dout: Dimension,
@@ -555,16 +540,11 @@ where
555540

556541
fn try_from(
557542
indices: &'a [AxisSliceInfo],
558-
) -> Result<&'a SliceInfo<[AxisSliceInfo], Din, Dout>, ShapeError> {
559-
check_dims_for_sliceinfo::<Din, Dout>(indices)?;
543+
) -> Result<SliceInfo<&'a [AxisSliceInfo], Din, Dout>, ShapeError> {
560544
unsafe {
561-
// This is okay because we've already checked the correctness of
562-
// `Din` and `Dout`, and the only non-zero-sized member of
563-
// `SliceInfo` is `indices`, so `&SliceInfo<[AxisSliceInfo], Din,
564-
// Dout>` should have the same bitwise representation as
565-
// `&[AxisSliceInfo]`.
566-
Ok(&*(indices as *const [AxisSliceInfo]
567-
as *const SliceInfo<[AxisSliceInfo], Din, Dout>))
545+
// This is okay because `&[AxisSliceInfo]` always returns the same
546+
// value for `.as_ref()`.
547+
Self::new(indices)
568548
}
569549
}
570550
}
@@ -630,20 +610,18 @@ where
630610
}
631611
}
632612

633-
impl<T, Din, Dout> AsRef<SliceInfo<[AxisSliceInfo], Din, Dout>> for SliceInfo<T, Din, Dout>
613+
impl<'a, T, Din, Dout> From<&'a SliceInfo<T, Din, Dout>>
614+
for SliceInfo<&'a [AxisSliceInfo], Din, Dout>
634615
where
635616
T: AsRef<[AxisSliceInfo]>,
636617
Din: Dimension,
637618
Dout: Dimension,
638619
{
639-
fn as_ref(&self) -> &SliceInfo<[AxisSliceInfo], Din, Dout> {
640-
unsafe {
641-
// This is okay because the only non-zero-sized member of
642-
// `SliceInfo` is `indices`, so `&SliceInfo<[AxisSliceInfo], Din, Dout>`
643-
// should have the same bitwise representation as
644-
// `&[AxisSliceInfo]`.
645-
&*(self.indices.as_ref() as *const [AxisSliceInfo]
646-
as *const SliceInfo<[AxisSliceInfo], Din, Dout>)
620+
fn from(info: &'a SliceInfo<T, Din, Dout>) -> SliceInfo<&'a [AxisSliceInfo], Din, Dout> {
621+
SliceInfo {
622+
in_dim: info.in_dim,
623+
out_dim: info.out_dim,
624+
indices: info.indices.as_ref(),
647625
}
648626
}
649627
}

0 commit comments

Comments
 (0)