Skip to content

Commit 017ae1e

Browse files
committed
Add aliasing_view() and aliasing_view_mut()
1 parent 0c48623 commit 017ae1e

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

src/impl_views.rs

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,5 +533,108 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
533533
{
534534
iterators::new_outer_iter_mut(self)
535535
}
536-
}
537536

537+
/// Returns a new `ArrayView` referencing the same data.
538+
///
539+
/// This is unsafe because it produces a mutable borrow and an immutable
540+
/// borrow of the same data.
541+
///
542+
/// # Example
543+
///
544+
/// ```
545+
/// use ndarray::prelude::*;
546+
///
547+
/// let mut arr = arr1(&[0, 1, 2]);
548+
/// let mut v1 = arr.view_mut();
549+
/// let v2 = unsafe { v1.aliasing_view() };
550+
/// v1[0] = 5;
551+
/// assert_eq!(v2[0], 5);
552+
/// ```
553+
///
554+
/// Note that the underlying array is still mutably borrowed, so this won't
555+
/// compile:
556+
///
557+
/// ```compile_fail
558+
/// // cannot borrow `arr` as immutable because it is also borrowed as mutable
559+
///
560+
/// use ndarray::prelude::*;
561+
///
562+
/// let mut arr = arr1(&[0, 1, 2]);
563+
/// let v = unsafe { arr.view_mut().aliasing_view() };
564+
/// // --- mutable borrow occurs here
565+
/// assert_eq!(arr[0], 0);
566+
/// // ^^^ immutable borrow occurs here
567+
/// ```
568+
///
569+
/// For the same reason, the view cannot outlive the data in `arr`, so this
570+
/// won't compile:
571+
///
572+
/// ```compile_fail
573+
/// // `arr` does not live long enough
574+
///
575+
/// use ndarray::prelude::*;
576+
///
577+
/// let v = {
578+
/// let mut arr = arr1(&[0, 1, 2]);
579+
/// unsafe { arr.view_mut().aliasing_view() }
580+
/// // --- borrow occurs here
581+
/// };
582+
/// // `arr` dropped here while still borrowed
583+
/// ```
584+
#[doc(hidden)]
585+
pub unsafe fn aliasing_view(&self) -> ArrayView<'a, A, D> {
586+
ArrayView::new_(self.ptr, self.dim.clone(), self.strides.clone())
587+
}
588+
589+
/// Returns a new `ArrayViewMut` referencing the same data.
590+
///
591+
/// This is unsafe because it produces two mutable borrows of the same
592+
/// data.
593+
///
594+
/// # Example
595+
///
596+
/// ```
597+
/// use ndarray::prelude::*;
598+
///
599+
/// let mut arr = arr1(&[0, 1, 2]);
600+
/// let mut v1 = arr.view_mut();
601+
/// let v2 = unsafe { v1.aliasing_view_mut() };
602+
/// v1[0] = 5;
603+
/// assert_eq!(v2[0], 5);
604+
/// ```
605+
///
606+
/// Note that the underlying array is still mutably borrowed, so this won't
607+
/// compile:
608+
///
609+
/// ```compile_fail
610+
/// // cannot borrow `arr` as immutable because it is also borrowed as mutable
611+
///
612+
/// use ndarray::prelude::*;
613+
///
614+
/// let mut arr = arr1(&[0, 1, 2]);
615+
/// let v = unsafe { arr.view_mut().aliasing_view_mut() };
616+
/// // --- mutable borrow occurs here
617+
/// assert_eq!(arr[0], 0);
618+
/// // ^^^ immutable borrow occurs here
619+
/// ```
620+
///
621+
/// For the same reason, the view cannot outlive the data in `arr`, so this
622+
/// won't compile:
623+
///
624+
/// ```compile_fail
625+
/// // `arr` does not live long enough
626+
///
627+
/// use ndarray::prelude::*;
628+
///
629+
/// let v = {
630+
/// let mut arr = arr1(&[0, 1, 2]);
631+
/// unsafe { arr.view_mut().aliasing_view_mut() }
632+
/// // --- borrow occurs here
633+
/// };
634+
/// // `arr` dropped here while still borrowed
635+
/// ```
636+
#[doc(hidden)]
637+
pub unsafe fn aliasing_view_mut(&self) -> ArrayViewMut<'a, A, D> {
638+
ArrayViewMut::new_(self.ptr, self.dim.clone(), self.strides.clone())
639+
}
640+
}

0 commit comments

Comments
 (0)