Skip to content

Commit

Permalink
[wip] bound rows
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-quinones committed Sep 30, 2024
1 parent e25168b commit 3256c65
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 120 deletions.
37 changes: 37 additions & 0 deletions src/col/colmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,43 @@ impl<'a, E: Entity, R: Shape> ColMut<'a, E, R> {
#[inline(always)]
#[track_caller]
pub fn at_mut(self, row: R::Idx) -> Mut<'a, E> {
assert!(row < self.nrows());
unsafe {
E::faer_map(
self.ptr_inbounds_at_mut(row),
#[inline(always)]
|ptr| &mut *ptr,
)
}
}

/// Returns references to the element at the given index.
///
/// # Note
/// The values pointed to by the references are expected to be initialized, even if the
/// pointed-to value is not read, otherwise the behavior is undefined.
///
/// # Safety
/// The behavior is undefined if any of the following conditions are violated:
/// * `row` must be in `[0, self.nrows())`.
#[inline(always)]
#[track_caller]
pub unsafe fn at_unchecked(self, row: R::Idx) -> Ref<'a, E> {
self.into_const().at_unchecked(row)
}

/// Returns references to the element at the given index.
///
/// # Note
/// The values pointed to by the references are expected to be initialized, even if the
/// pointed-to value is not read, otherwise the behavior is undefined.
///
/// # Safety
/// The behavior is undefined if any of the following conditions are violated:
/// * `row` must be in `[0, self.nrows())`.
#[inline(always)]
#[track_caller]
pub unsafe fn at_mut_unchecked(self, row: R::Idx) -> Mut<'a, E> {
unsafe {
E::faer_map(
self.ptr_inbounds_at_mut(row),
Expand Down
18 changes: 12 additions & 6 deletions src/row/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
col::{VecImpl, VecOwnImpl},
mat::*,
utils::slice::*,
Conj,
Conj, Shape,
};
use coe::Coerce;
use core::{marker::PhantomData, ptr::NonNull};
Expand All @@ -25,28 +25,34 @@ pub trait RowIndex<ColRange>: crate::seal::Seal + Sized {

/// Trait for types that can be converted to a row view.
pub trait AsRowRef<E: Entity> {
type C: Shape;

/// Convert to a row view.
fn as_row_ref(&self) -> RowRef<'_, E>;
fn as_row_ref(&self) -> RowRef<'_, E, Self::C>;
}
/// Trait for types that can be converted to a mutable row view.
pub trait AsRowMut<E: Entity>: AsRowRef<E> {
/// Convert to a mutable row view.
fn as_row_mut(&mut self) -> RowMut<'_, E>;
fn as_row_mut(&mut self) -> RowMut<'_, E, Self::C>;
}

impl<E: Entity, T: AsRowRef<E>> AsRowRef<E> for &T {
fn as_row_ref(&self) -> RowRef<'_, E> {
type C = T::C;

fn as_row_ref(&self) -> RowRef<'_, E, Self::C> {
(**self).as_row_ref()
}
}
impl<E: Entity, T: AsRowRef<E>> AsRowRef<E> for &mut T {
fn as_row_ref(&self) -> RowRef<'_, E> {
type C = T::C;

fn as_row_ref(&self) -> RowRef<'_, E, Self::C> {
(**self).as_row_ref()
}
}

impl<E: Entity, T: AsRowMut<E>> AsRowMut<E> for &mut T {
fn as_row_mut(&mut self) -> RowMut<'_, E> {
fn as_row_mut(&mut self) -> RowMut<'_, E, Self::C> {
(**self).as_row_mut()
}
}
Expand Down
Loading

0 comments on commit 3256c65

Please sign in to comment.