|
5 | 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
6 | 6 | // option. This file may not be copied, modified, or distributed
|
7 | 7 | // except according to those terms.
|
| 8 | +use crate::dimension::slices_intersect; |
8 | 9 | use crate::error::{ErrorKind, ShapeError};
|
9 |
| -use crate::Dimension; |
| 10 | +use crate::{ArrayViewMut, Dimension, RawArrayViewMut}; |
10 | 11 | use std::fmt;
|
11 | 12 | use std::marker::PhantomData;
|
12 | 13 | use std::ops::{Deref, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
|
@@ -629,3 +630,189 @@ macro_rules! s(
|
629 | 630 | &*&$crate::s![@parse ::std::marker::PhantomData::<$crate::Ix0>, [] $($t)*]
|
630 | 631 | };
|
631 | 632 | );
|
| 633 | + |
| 634 | +/// Slicing information describing multiple mutable, disjoint slices. |
| 635 | +/// |
| 636 | +/// It's unfortunate that we need `'out` and `A` to be parameters of the trait, |
| 637 | +/// but they're necessary until Rust supports generic associated types. |
| 638 | +/// |
| 639 | +/// # Safety |
| 640 | +/// |
| 641 | +/// Implementers of this trait must ensure that: |
| 642 | +/// |
| 643 | +/// * `.slice_and_deref()` panics or aborts if the slices would intersect, and |
| 644 | +/// |
| 645 | +/// * the `.intersects_self()`, `.intersects_indices()`, and |
| 646 | +/// `.intersects_other()` implementations are correct. |
| 647 | +pub unsafe trait MultiSlice<'out, A, D> |
| 648 | +where |
| 649 | + A: 'out, |
| 650 | + D: Dimension, |
| 651 | +{ |
| 652 | + /// The type of the slices created by `.slice_and_deref()`. |
| 653 | + type Output; |
| 654 | + |
| 655 | + /// Slice the raw view into multiple raw views, and dereference them. |
| 656 | + /// |
| 657 | + /// **Panics** if performing any individual slice panics or if the slices |
| 658 | + /// are not disjoint (i.e. if they intersect). |
| 659 | + /// |
| 660 | + /// # Safety |
| 661 | + /// |
| 662 | + /// The caller must ensure that it is safe to mutably dereference the view |
| 663 | + /// using the lifetime `'out`. |
| 664 | + unsafe fn slice_and_deref(&self, view: RawArrayViewMut<A, D>) -> Self::Output; |
| 665 | + |
| 666 | + /// Returns `true` if slicing an array of the specified `shape` with `self` |
| 667 | + /// would result in intersecting slices. |
| 668 | + /// |
| 669 | + /// If `self.intersects_self(&view.raw_dim())` is `true`, then |
| 670 | + /// `self.slice_and_deref(view)` must panic. |
| 671 | + fn intersects_self(&self, shape: &D) -> bool; |
| 672 | + |
| 673 | + /// Returns `true` if any slices created by slicing an array of the |
| 674 | + /// specified `shape` with `self` would intersect with the specified |
| 675 | + /// indices. |
| 676 | + /// |
| 677 | + /// Note that even if this returns `false`, `self.intersects_self(shape)` |
| 678 | + /// may still return `true`. (`.intersects_indices()` doesn't check for |
| 679 | + /// intersections within `self`; it only checks for intersections between |
| 680 | + /// `self` and `indices`.) |
| 681 | + fn intersects_indices(&self, shape: &D, indices: &D::SliceArg) -> bool; |
| 682 | + |
| 683 | + /// Returns `true` if any slices created by slicing an array of the |
| 684 | + /// specified `shape` with `self` would intersect any slices created by |
| 685 | + /// slicing the array with `other`. |
| 686 | + /// |
| 687 | + /// Note that even if this returns `false`, `self.intersects_self(shape)` |
| 688 | + /// or `other.intersects_self(shape)` may still return `true`. |
| 689 | + /// (`.intersects_other()` doesn't check for intersections within `self` or |
| 690 | + /// within `other`; it only checks for intersections between `self` and |
| 691 | + /// `other`.) |
| 692 | + fn intersects_other(&self, shape: &D, other: impl MultiSlice<'out, A, D>) -> bool; |
| 693 | +} |
| 694 | + |
| 695 | +unsafe impl<'out, A, D, Do> MultiSlice<'out, A, D> for SliceInfo<D::SliceArg, Do> |
| 696 | +where |
| 697 | + A: 'out, |
| 698 | + D: Dimension, |
| 699 | + Do: Dimension, |
| 700 | +{ |
| 701 | + type Output = ArrayViewMut<'out, A, Do>; |
| 702 | + |
| 703 | + unsafe fn slice_and_deref(&self, view: RawArrayViewMut<A, D>) -> Self::Output { |
| 704 | + view.slice_move(self).deref_into_view_mut() |
| 705 | + } |
| 706 | + |
| 707 | + fn intersects_self(&self, _shape: &D) -> bool { |
| 708 | + false |
| 709 | + } |
| 710 | + |
| 711 | + fn intersects_indices(&self, shape: &D, indices: &D::SliceArg) -> bool { |
| 712 | + slices_intersect(shape, &*self, indices) |
| 713 | + } |
| 714 | + |
| 715 | + fn intersects_other(&self, shape: &D, other: impl MultiSlice<'out, A, D>) -> bool { |
| 716 | + other.intersects_indices(shape, &*self) |
| 717 | + } |
| 718 | +} |
| 719 | + |
| 720 | +unsafe impl<'out, A, D> MultiSlice<'out, A, D> for () |
| 721 | +where |
| 722 | + A: 'out, |
| 723 | + D: Dimension, |
| 724 | +{ |
| 725 | + type Output = (); |
| 726 | + |
| 727 | + unsafe fn slice_and_deref(&self, _view: RawArrayViewMut<A, D>) -> Self::Output {} |
| 728 | + |
| 729 | + fn intersects_self(&self, _shape: &D) -> bool { |
| 730 | + false |
| 731 | + } |
| 732 | + |
| 733 | + fn intersects_indices(&self, _shape: &D, _indices: &D::SliceArg) -> bool { |
| 734 | + false |
| 735 | + } |
| 736 | + |
| 737 | + fn intersects_other(&self, _shape: &D, _other: impl MultiSlice<'out, A, D>) -> bool { |
| 738 | + false |
| 739 | + } |
| 740 | +} |
| 741 | + |
| 742 | +macro_rules! impl_multislice_tuple { |
| 743 | + ($($T:ident,)*) => { |
| 744 | + unsafe impl<'out, A, D, $($T,)*> MultiSlice<'out, A, D> for ($($T,)*) |
| 745 | + where |
| 746 | + A: 'out, |
| 747 | + D: Dimension, |
| 748 | + $($T: MultiSlice<'out, A, D>,)* |
| 749 | + { |
| 750 | + type Output = ($($T::Output,)*); |
| 751 | + |
| 752 | + unsafe fn slice_and_deref(&self, view: RawArrayViewMut<A, D>) -> Self::Output { |
| 753 | + assert!(!self.intersects_self(&view.raw_dim())); |
| 754 | + |
| 755 | + #[allow(non_snake_case)] |
| 756 | + let ($($T,)*) = self; |
| 757 | + ($($T.slice_and_deref(view.clone()),)*) |
| 758 | + } |
| 759 | + |
| 760 | + fn intersects_self(&self, shape: &D) -> bool { |
| 761 | + #[allow(non_snake_case)] |
| 762 | + let ($($T,)*) = self; |
| 763 | + impl_multislice_tuple!(@intersects_self shape, ($($T,)*)) |
| 764 | + } |
| 765 | + |
| 766 | + fn intersects_indices(&self, shape: &D, indices: &D::SliceArg) -> bool { |
| 767 | + #[allow(non_snake_case)] |
| 768 | + let ($($T,)*) = self; |
| 769 | + $($T.intersects_indices(shape, indices)) ||* |
| 770 | + } |
| 771 | + |
| 772 | + fn intersects_other(&self, shape: &D, other: impl MultiSlice<'out, A, D>) -> bool { |
| 773 | + #[allow(non_snake_case)] |
| 774 | + let ($($T,)*) = self; |
| 775 | + $($T.intersects_other(shape, &other)) ||* |
| 776 | + } |
| 777 | + } |
| 778 | + }; |
| 779 | + (@intersects_self $shape:expr, ($head:expr,)) => { |
| 780 | + $head.intersects_self($shape) |
| 781 | + }; |
| 782 | + (@intersects_self $shape:expr, ($head:expr, $($tail:expr,)*)) => { |
| 783 | + $head.intersects_self($shape) || |
| 784 | + $($head.intersects_other($shape, &$tail)) ||* || |
| 785 | + impl_multislice_tuple!(@intersects_self $shape, ($($tail,)*)) |
| 786 | + }; |
| 787 | +} |
| 788 | +impl_multislice_tuple!(T0,); |
| 789 | +impl_multislice_tuple!(T0, T1,); |
| 790 | +impl_multislice_tuple!(T0, T1, T2,); |
| 791 | +impl_multislice_tuple!(T0, T1, T2, T3,); |
| 792 | +impl_multislice_tuple!(T0, T1, T2, T3, T4,); |
| 793 | +impl_multislice_tuple!(T0, T1, T2, T3, T4, T5,); |
| 794 | + |
| 795 | +unsafe impl<'out, A, D, T> MultiSlice<'out, A, D> for &'_ T |
| 796 | +where |
| 797 | + A: 'out, |
| 798 | + D: Dimension, |
| 799 | + T: MultiSlice<'out, A, D>, |
| 800 | +{ |
| 801 | + type Output = T::Output; |
| 802 | + |
| 803 | + unsafe fn slice_and_deref(&self, view: RawArrayViewMut<A, D>) -> Self::Output { |
| 804 | + T::slice_and_deref(self, view) |
| 805 | + } |
| 806 | + |
| 807 | + fn intersects_self(&self, shape: &D) -> bool { |
| 808 | + T::intersects_self(self, shape) |
| 809 | + } |
| 810 | + |
| 811 | + fn intersects_indices(&self, shape: &D, indices: &D::SliceArg) -> bool { |
| 812 | + T::intersects_indices(self, shape, indices) |
| 813 | + } |
| 814 | + |
| 815 | + fn intersects_other(&self, shape: &D, other: impl MultiSlice<'out, A, D>) -> bool { |
| 816 | + T::intersects_other(self, shape, other) |
| 817 | + } |
| 818 | +} |
0 commit comments