rel_ptr/
error.rs

1
2/**
3 * If an integer's range is too small to store an offset, then
4 * this error is generated
5 */
6#[derive(Debug)]
7pub struct IntegerDeltaError(pub(crate) IntegerDeltaErrorImpl);
8
9/// All types of errors, this is internal and so protected
10/// behind a wrapper struct
11#[derive(Debug)]
12pub(crate) enum IntegerDeltaErrorImpl {
13    /// Failed to convert isize to given integer type
14    Conversion(isize),
15    
16    /// Failed to subtract the two usizes (overflowed isize)
17    Sub(usize, usize),
18
19    /// Got a zero when a non-zero value was expected (for `NonZero*`)
20    InvalidNonZero
21}
22
23#[cfg(not(feature = "no_std"))]
24impl std::error::Error for IntegerDeltaError {}
25
26mod fmt {
27    use super::*;
28    use std::fmt;
29
30    impl fmt::Display for IntegerDeltaError {
31        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32            match self.0 {
33                IntegerDeltaErrorImpl::Conversion(del) => write!(
34                    f,
35                    "Offset could not be stored (offset of {} is too large)",
36                    del
37                ),
38                IntegerDeltaErrorImpl::Sub(a, b) => {
39                    write!(f, "Difference is beween {} and {} overflows `isize`", a, b)
40                },
41                
42                IntegerDeltaErrorImpl::InvalidNonZero => {
43                    write!(f, "Difference was zero when a `NonZero*` type was specified")
44                }
45            }
46        }
47    }
48}