p3_matrix/
horizontally_truncated.rs

1use core::iter::Take;
2use core::marker::PhantomData;
3
4use crate::Matrix;
5
6pub struct HorizontallyTruncated<T, Inner> {
7    inner: Inner,
8    truncated_width: usize,
9    _phantom: PhantomData<T>,
10}
11
12impl<T, Inner: Matrix<T>> HorizontallyTruncated<T, Inner>
13where
14    T: Send + Sync,
15{
16    pub fn new(inner: Inner, truncated_width: usize) -> Self {
17        assert!(truncated_width <= inner.width());
18        Self {
19            inner,
20            truncated_width,
21            _phantom: PhantomData,
22        }
23    }
24}
25
26impl<T, Inner> Matrix<T> for HorizontallyTruncated<T, Inner>
27where
28    T: Send + Sync,
29    Inner: Matrix<T>,
30{
31    #[inline(always)]
32    fn width(&self) -> usize {
33        self.truncated_width
34    }
35
36    #[inline(always)]
37    fn height(&self) -> usize {
38        self.inner.height()
39    }
40
41    #[inline(always)]
42    fn get(&self, r: usize, c: usize) -> T {
43        debug_assert!(c < self.truncated_width);
44        self.inner.get(c, r)
45    }
46
47    type Row<'a>
48        = Take<Inner::Row<'a>>
49    where
50        Self: 'a;
51
52    #[inline(always)]
53    fn row(&self, r: usize) -> Self::Row<'_> {
54        self.inner.row(r).take(self.truncated_width)
55    }
56}