Skip to content
Snippets Groups Projects
Commit eb622230 authored by Martin Larralde's avatar Martin Larralde
Browse files

Use marker trait for `DiscreteMatrix` element types

parent c93271a6
No related branches found
No related tags found
No related merge requests found
...@@ -29,6 +29,13 @@ type _DefaultAlignment = typenum::consts::U1; ...@@ -29,6 +29,13 @@ type _DefaultAlignment = typenum::consts::U1;
/// The default alignment used in dense matrices. /// The default alignment used in dense matrices.
pub type DefaultAlignment = _DefaultAlignment; pub type DefaultAlignment = _DefaultAlignment;
// --- MatrixElement -----------------------------------------------------------
/// A marker trait for types allowed as `DenseMatrix` elements.
pub trait MatrixElement: Default + Copy {}
impl<T> MatrixElement for T where T: Default + Copy {}
// --- DenseMatrix ------------------------------------------------------------- // --- DenseMatrix -------------------------------------------------------------
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
...@@ -48,8 +55,7 @@ impl MatrixCoordinates { ...@@ -48,8 +55,7 @@ impl MatrixCoordinates {
/// A memory-aligned dense matrix with a constant number of columns. /// A memory-aligned dense matrix with a constant number of columns.
#[derive(Eq)] #[derive(Eq)]
pub struct DenseMatrix<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo = DefaultAlignment> pub struct DenseMatrix<T: MatrixElement, C: Unsigned, A: Unsigned + PowerOfTwo = DefaultAlignment> {
{
data: Vec<T>, data: Vec<T>,
offset: usize, offset: usize,
rows: usize, rows: usize,
...@@ -57,7 +63,7 @@ pub struct DenseMatrix<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo ...@@ -57,7 +63,7 @@ pub struct DenseMatrix<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo
_alignment: std::marker::PhantomData<A>, _alignment: std::marker::PhantomData<A>,
} }
impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> DenseMatrix<T, C, A> { impl<T: MatrixElement, C: Unsigned, A: Unsigned + PowerOfTwo> DenseMatrix<T, C, A> {
/// Create a new matrix with the given number of rows. /// Create a new matrix with the given number of rows.
pub fn new(rows: usize) -> Self { pub fn new(rows: usize) -> Self {
let data = Vec::new(); let data = Vec::new();
...@@ -203,7 +209,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> DenseMatrix<T, C, ...@@ -203,7 +209,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> DenseMatrix<T, C,
} }
} }
impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Clone for DenseMatrix<T, C, A> { impl<T: MatrixElement, C: Unsigned, A: Unsigned + PowerOfTwo> Clone for DenseMatrix<T, C, A> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
let mut clone = unsafe { Self::uninitialized(self.rows) }; let mut clone = unsafe { Self::uninitialized(self.rows) };
let l = self.rows() * self.stride(); let l = self.rows() * self.stride();
...@@ -213,7 +219,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Clone for DenseMa ...@@ -213,7 +219,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Clone for DenseMa
} }
} }
impl<T: Default + Copy + Debug, C: Unsigned, A: Unsigned + PowerOfTwo> Debug impl<T: MatrixElement + Debug, C: Unsigned, A: Unsigned + PowerOfTwo> Debug
for DenseMatrix<T, C, A> for DenseMatrix<T, C, A>
{ {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
...@@ -221,7 +227,7 @@ impl<T: Default + Copy + Debug, C: Unsigned, A: Unsigned + PowerOfTwo> Debug ...@@ -221,7 +227,7 @@ impl<T: Default + Copy + Debug, C: Unsigned, A: Unsigned + PowerOfTwo> Debug
} }
} }
impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Index<usize> impl<T: MatrixElement, C: Unsigned, A: Unsigned + PowerOfTwo> Index<usize>
for DenseMatrix<T, C, A> for DenseMatrix<T, C, A>
{ {
type Output = [T]; type Output = [T];
...@@ -236,7 +242,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Index<usize> ...@@ -236,7 +242,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Index<usize>
} }
} }
impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IndexMut<usize> impl<T: MatrixElement, C: Unsigned, A: Unsigned + PowerOfTwo> IndexMut<usize>
for DenseMatrix<T, C, A> for DenseMatrix<T, C, A>
{ {
#[inline] #[inline]
...@@ -250,7 +256,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IndexMut<usize> ...@@ -250,7 +256,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IndexMut<usize>
} }
} }
impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Index<MatrixCoordinates> impl<T: MatrixElement, C: Unsigned, A: Unsigned + PowerOfTwo> Index<MatrixCoordinates>
for DenseMatrix<T, C, A> for DenseMatrix<T, C, A>
{ {
type Output = T; type Output = T;
...@@ -263,7 +269,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Index<MatrixCoord ...@@ -263,7 +269,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Index<MatrixCoord
} }
} }
impl<'a, T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IntoIterator impl<'a, T: MatrixElement, C: Unsigned, A: Unsigned + PowerOfTwo> IntoIterator
for &'a DenseMatrix<T, C, A> for &'a DenseMatrix<T, C, A>
{ {
type Item = &'a [T]; type Item = &'a [T];
...@@ -274,7 +280,7 @@ impl<'a, T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IntoIterator ...@@ -274,7 +280,7 @@ impl<'a, T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IntoIterator
} }
} }
impl<'a, T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IntoIterator impl<'a, T: MatrixElement, C: Unsigned, A: Unsigned + PowerOfTwo> IntoIterator
for &'a mut DenseMatrix<T, C, A> for &'a mut DenseMatrix<T, C, A>
{ {
type Item = &'a mut [T]; type Item = &'a mut [T];
...@@ -285,7 +291,7 @@ impl<'a, T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IntoIterator ...@@ -285,7 +291,7 @@ impl<'a, T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IntoIterator
} }
} }
impl<'a, T: Default + Copy + PartialEq, C: Unsigned, A: Unsigned + PowerOfTwo> PartialEq impl<'a, T: MatrixElement + PartialEq, C: Unsigned, A: Unsigned + PowerOfTwo> PartialEq
for DenseMatrix<T, C, A> for DenseMatrix<T, C, A>
{ {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
...@@ -313,7 +319,7 @@ impl<'a, T: Default + Copy + PartialEq, C: Unsigned, A: Unsigned + PowerOfTwo> P ...@@ -313,7 +319,7 @@ impl<'a, T: Default + Copy + PartialEq, C: Unsigned, A: Unsigned + PowerOfTwo> P
pub struct Iter<'a, T, C, A> pub struct Iter<'a, T, C, A>
where where
T: 'a + Default + Copy, T: 'a + MatrixElement,
C: Unsigned, C: Unsigned,
A: Unsigned + PowerOfTwo, A: Unsigned + PowerOfTwo,
{ {
...@@ -324,7 +330,7 @@ where ...@@ -324,7 +330,7 @@ where
impl<'a, T, C, A> Iter<'a, T, C, A> impl<'a, T, C, A> Iter<'a, T, C, A>
where where
T: 'a + Default + Copy, T: 'a + MatrixElement,
C: Unsigned, C: Unsigned,
A: Unsigned + PowerOfTwo, A: Unsigned + PowerOfTwo,
{ {
...@@ -350,7 +356,7 @@ where ...@@ -350,7 +356,7 @@ where
pub struct IterMut<'a, T, C, A> pub struct IterMut<'a, T, C, A>
where where
T: 'a + Default + Copy, T: 'a + MatrixElement,
C: Unsigned, C: Unsigned,
A: Unsigned + PowerOfTwo, A: Unsigned + PowerOfTwo,
{ {
...@@ -361,7 +367,7 @@ where ...@@ -361,7 +367,7 @@ where
impl<'a, T, C, A> IterMut<'a, T, C, A> impl<'a, T, C, A> IterMut<'a, T, C, A>
where where
T: 'a + Default + Copy, T: 'a + MatrixElement,
C: Unsigned, C: Unsigned,
A: Unsigned + PowerOfTwo, A: Unsigned + PowerOfTwo,
{ {
...@@ -389,7 +395,7 @@ macro_rules! iterator { ...@@ -389,7 +395,7 @@ macro_rules! iterator {
($t:ident, $T:ident, $($item:tt)*) => { ($t:ident, $T:ident, $($item:tt)*) => {
impl<'a, $T, C, A> Iterator for $t<'a, $T, C, A> impl<'a, $T, C, A> Iterator for $t<'a, $T, C, A>
where where
$T: Default + Copy, $T: MatrixElement,
C: Unsigned, C: Unsigned,
A: Unsigned + PowerOfTwo, A: Unsigned + PowerOfTwo,
{ {
...@@ -401,7 +407,7 @@ macro_rules! iterator { ...@@ -401,7 +407,7 @@ macro_rules! iterator {
impl<'a, $T, C, A> ExactSizeIterator for $t<'a, $T, C, A> impl<'a, $T, C, A> ExactSizeIterator for $t<'a, $T, C, A>
where where
$T: Default + Copy, $T: MatrixElement,
C: Unsigned, C: Unsigned,
A: Unsigned + PowerOfTwo, A: Unsigned + PowerOfTwo,
{ {
...@@ -413,14 +419,14 @@ macro_rules! iterator { ...@@ -413,14 +419,14 @@ macro_rules! iterator {
impl<'a, $T, C, A> FusedIterator for $t<'a, $T, C, A> impl<'a, $T, C, A> FusedIterator for $t<'a, $T, C, A>
where where
$T: Default + Copy, $T: MatrixElement,
C: Unsigned, C: Unsigned,
A: Unsigned + PowerOfTwo, A: Unsigned + PowerOfTwo,
{} {}
impl<'a, $T, C, A> DoubleEndedIterator for $t<'a, $T, C, A> impl<'a, $T, C, A> DoubleEndedIterator for $t<'a, $T, C, A>
where where
$T: Default + Copy, $T: MatrixElement,
C: Unsigned, C: Unsigned,
A: Unsigned + PowerOfTwo, A: Unsigned + PowerOfTwo,
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment