nd_slice/
errors.rs

1//! This modules contains all the errors associated with this crate
2
3use std::fmt;
4
5/// Errors involving a shape mismatch
6#[derive(Debug)]
7pub struct ShapeError<'s, T, const N: usize> {
8    slice: &'s [T],
9    shape: [usize; N] // INFO might have to change this to a const generic later
10}
11
12impl<'s, T, const N: usize> fmt::Display for ShapeError<'s, T, N> {
13    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14        write!(
15            f, 
16            "Constructing an NdSlice of shape: {:?} would need a slice of length {}, but a slice of length {} was provided",
17            self.shape,
18            self.shape.iter().fold(1, |acc, &x| acc * x),
19            self.slice.len()
20        )
21    }
22}
23
24impl<'s, T, const N: usize> ShapeError<'s, T, N> {
25    /// Create a new `ShapeError` given the length of the slice and the expected shape.
26    pub fn new(slice: &'s [T], shape: [usize; N]) -> Self {
27        Self {
28            slice,
29            shape
30        }
31    }
32}