pub struct SubmatrixRaw<V, T>where
V: AsPointerToSlice<T>,{ /* private fields */ }
unstable-enable
only.Expand description
A submatrix that works on raw pointers, thus does not care about mutability and borrowing. It already takes care about bounds checking and indexing. Nevertheless, it is quite difficult to use this correctly, best not use it at all. I mainly made it public to allow doctests.
More concretely, when having a 2d-structure, given by a sequence of V
s, we
can consider a rectangular sub-block. This is encapsulated by SubmatrixRaw.
§Safety
The individual safety contracts are described at the corresponding functions.
However, in total be careful when actuall transforming the entry pointers
(given by SubmatrixRaw::entry_at
or SubmatrixRaw::row_at
) into references.
Since SubmatrixRaw
does not borrow-check (and is Copy
!), it is easy to create
aliasing pointers, that must not be converted into references.
§Example of illegal use
let mut data = [1, 2, 3];
// this is actuall safe and intended use
let mut matrix = unsafe { SubmatrixRaw::<AsFirstElement<i64>, i64>::new(std::mem::transmute(NonNull::new(data.as_mut_ptr()).unwrap()), 1, 3, 0, 3) };
// this is safe, but note that ptr1 and ptr2 alias...
let mut ptr1: NonNull<i64> = matrix.entry_at(0, 0);
let mut ptr2: NonNull<i64> = matrix.entry_at(0, 0);
// this is UB now!
let (ref1, ref2) = unsafe { (ptr1.as_mut(), ptr2.as_mut()) };
§Availability
This API is marked as unstable and is only available when the unstable-enable
crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time.
Implementations§
Source§impl<V, T> SubmatrixRaw<V, T>where
V: AsPointerToSlice<T>,
impl<V, T> SubmatrixRaw<V, T>where
V: AsPointerToSlice<T>,
Sourcepub unsafe fn new(
rows: NonNull<V>,
row_count: usize,
row_step: isize,
cols_start: usize,
col_count: usize,
) -> Self
pub unsafe fn new( rows: NonNull<V>, row_count: usize, row_step: isize, cols_start: usize, col_count: usize, ) -> Self
Create a new SubmatrixRaw object.
§Safety
We require that each pointer rows.offset(row_step * i)
for 0 <= i < row_count
points to a
valid object and can be dereferenced. Furthermore, if ptr
is the pointer returned by [
AsPointerToSlice::get_pointer()]
,
then ptr.offset(i + cols_start)
must point to a valid T
for 0 <= i < col_count
.
Furthermore, we require any two of these (for different i) to represent disjunct “slices”, i.e. if they
give pointers ptr1
and ptr2
(via AsPointerToSlice::get_pointer()
), then ptr1.offset(cols_start + k)
and
ptr2.offset(cols_start + l)
for 0 <= k, l < col_count
never alias.
§Availability
This API is marked as unstable and is only available when the unstable-enable
crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time.
Sourcepub fn restrict_rows(self, rows: Range<usize>) -> Self
pub fn restrict_rows(self, rows: Range<usize>) -> Self
§Availability
This API is marked as unstable and is only available when the unstable-enable
crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time.
Sourcepub fn restrict_cols(self, cols: Range<usize>) -> Self
pub fn restrict_cols(self, cols: Range<usize>) -> Self
§Availability
This API is marked as unstable and is only available when the unstable-enable
crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time.
Sourcepub fn row_at(&self, row: usize) -> NonNull<[T]>
pub fn row_at(&self, row: usize) -> NonNull<[T]>
Returns a pointer to the row
-th row of the matrix.
Be carefull about aliasing when making this into a reference!
§Availability
This API is marked as unstable and is only available when the unstable-enable
crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time.
Sourcepub fn entry_at(&self, row: usize, col: usize) -> NonNull<T>
pub fn entry_at(&self, row: usize, col: usize) -> NonNull<T>
Returns a pointer to the (row, col)
-th entry of the matrix.
Be carefull about aliasing when making this into a reference!
§Availability
This API is marked as unstable and is only available when the unstable-enable
crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time.
Trait Implementations§
Source§impl<V, T> Clone for SubmatrixRaw<V, T>where
V: AsPointerToSlice<T>,
impl<V, T> Clone for SubmatrixRaw<V, T>where
V: AsPointerToSlice<T>,
impl<V, T> Copy for SubmatrixRaw<V, T>where
V: AsPointerToSlice<T>,
impl<V, T> Send for SubmatrixRaw<V, T>
Requiring T: Sync
is the more conservative choice. If SubmatrixRaw
acts as a mutable reference, we would only require T: Send
, but we also
want SubmatrixRaw
to be usable as an immutable reference, thus it can be
shared between threads, which requires T: Sync
.
impl<V, T> Sync for SubmatrixRaw<V, T>
Auto Trait Implementations§
impl<V, T> Freeze for SubmatrixRaw<V, T>
impl<V, T> RefUnwindSafe for SubmatrixRaw<V, T>where
T: RefUnwindSafe,
V: RefUnwindSafe,
impl<V, T> Unpin for SubmatrixRaw<V, T>
impl<V, T> UnwindSafe for SubmatrixRaw<V, T>where
V: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more