SubmatrixRaw

Struct SubmatrixRaw 

Source
pub struct SubmatrixRaw<V, T>
where V: AsPointerToSlice<T>,
{ /* private fields */ }
Available on crate feature 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 Vs, 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>,

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V, T> Copy for SubmatrixRaw<V, T>
where V: AsPointerToSlice<T>,

Source§

impl<V, T> Send for SubmatrixRaw<V, T>
where V: AsPointerToSlice<T> + Sync, T: Sync,

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.

Source§

impl<V, T> Sync for SubmatrixRaw<V, T>
where V: AsPointerToSlice<T> + Sync, T: Sync,

Auto Trait Implementations§

§

impl<V, T> Freeze for SubmatrixRaw<V, T>

§

impl<V, T> RefUnwindSafe for SubmatrixRaw<V, T>

§

impl<V, T> Unpin for SubmatrixRaw<V, T>

§

impl<V, T> UnwindSafe for SubmatrixRaw<V, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.