use crate::matrices::views::{DataLayout, MatrixRef, NoInteriorMutability};
use crate::matrices::{Column, Row};
use std::marker::PhantomData;
#[derive(Clone, Debug)]
pub(crate) struct MatrixMap<T, U, S, F> {
source: S,
f: F,
_from: PhantomData<T>,
_to: PhantomData<U>,
}
impl<T, U, S, F> MatrixMap<T, U, S, F>
where
S: MatrixRef<T>,
F: Fn(&T) -> &U,
{
#[track_caller]
pub fn from(source: S, f: F) -> MatrixMap<T, U, S, F> {
MatrixMap {
source,
f,
_from: PhantomData,
_to: PhantomData,
}
}
#[allow(dead_code)]
pub fn source(self) -> S {
self.source
}
#[allow(dead_code)]
pub fn source_ref(&self) -> &S {
&self.source
}
#[allow(dead_code)]
pub fn source_ref_mut(&mut self) -> &mut S {
&mut self.source
}
}
unsafe impl<T, U, S, F> NoInteriorMutability for MatrixMap<T, U, S, F> where S: NoInteriorMutability {}
unsafe impl<T, U, S, F> MatrixRef<U> for MatrixMap<T, U, S, F>
where
S: MatrixRef<T>,
F: Fn(&T) -> &U,
{
fn try_get_reference(&self, row: Row, column: Column) -> Option<&U> {
Some((self.f)(self.source.try_get_reference(row, column)?))
}
fn view_rows(&self) -> Row {
self.source.view_rows()
}
fn view_columns(&self) -> Column {
self.source.view_columns()
}
unsafe fn get_reference_unchecked(&self, row: Row, column: Column) -> &U {
unsafe { (self.f)(self.source.get_reference_unchecked(row, column)) }
}
fn data_layout(&self) -> DataLayout {
self.source.data_layout()
}
}