1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Common overflow error types.

#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![forbid(missing_docs, missing_debug_implementations)]
#![cfg_attr(test, deny(warnings))]
#![feature(never_type)]

#[cfg(any(test, feature = "std"))]
extern crate core;
use core::fmt;

extern crate failure;
use failure::Fail;

// TODO: impl Hash
/// A discrete error with a known description.
pub trait Error: Fail + Copy + Eq {
    /// Describes the error using a static string.
    fn describe(self) -> &'static str;
}
impl Error for ! {
    fn describe(self) -> &'static str {
        self
    }
}

/// Division could not be performed due to division by zero.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct DivByZero;
impl Fail for DivByZero {}
impl Error for DivByZero {
    #[inline]
    fn describe(self) -> &'static str {
        "division by zero"
    }
}
impl fmt::Display for DivByZero {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.describe())
    }
}

/// Division could not be performed; either [`DivByZero`] or [`WouldOverflow`].
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum CannotDiv {
    /// [`DivByZero`]
    ByZero,

    /// [`WouldOverflow`]
    WouldOverflow,
}
impl Fail for CannotDiv {
    fn cause(&self) -> Option<&Fail> {
        match *self {
            CannotDiv::ByZero => Some(&DivByZero),
            CannotDiv::WouldOverflow => Some(&WouldOverflow),
        }
    }
}
impl Error for CannotDiv {
    #[inline]
    fn describe(self) -> &'static str {
        match self {
            CannotDiv::ByZero => DivByZero.describe(),
            CannotDiv::WouldOverflow => WouldOverflow.describe(),
        }
    }
}
impl From<DivByZero> for CannotDiv {
    #[inline]
    fn from(_: DivByZero) -> CannotDiv {
        CannotDiv::ByZero
    }
}
impl From<WouldOverflow> for CannotDiv {
    #[inline]
    fn from(_: WouldOverflow) -> CannotDiv {
        CannotDiv::WouldOverflow
    }
}
impl fmt::Display for CannotDiv {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.describe())
    }
}

/// Operation was performed and overflowed or underflowed.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Overflowed;
impl Fail for Overflowed {}
impl Error for Overflowed {
    #[inline]
    fn describe(self) -> &'static str {
        "operation overflowed"
    }
}
impl fmt::Display for Overflowed {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.describe())
    }
}

/// Operation was not performed, as it would result in overflow or underflow.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct WouldOverflow;
impl Fail for WouldOverflow {}
impl Error for WouldOverflow {
    fn describe(self) -> &'static str {
        "operation was not performed, as it would overflow"
    }
}
impl fmt::Display for WouldOverflow {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.describe())
    }
}