addition: TryInto i64
This commit is contained in:
14
src/lib.rs
14
src/lib.rs
@@ -10,6 +10,7 @@ pub enum FractionError {
|
||||
DivisionByZero,
|
||||
ZeroDenominator,
|
||||
Overflow,
|
||||
InvalidInteger,
|
||||
}
|
||||
|
||||
impl fmt::Display for FractionError {
|
||||
@@ -18,6 +19,7 @@ impl fmt::Display for FractionError {
|
||||
FractionError::DivisionByZero => write!(f, "Division by zero"),
|
||||
FractionError::ZeroDenominator => write!(f, "Denominator can't be zero"),
|
||||
FractionError::Overflow => write!(f, "Numeric overflow"),
|
||||
FractionError::InvalidInteger => write!(f, "Can't convert to integer"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,6 +205,18 @@ impl TryFrom<(i64, i64)> for Fraction {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryInto<i64> for Fraction {
|
||||
type Error = FractionError;
|
||||
|
||||
fn try_into(self) -> Result<i64, Self::Error> {
|
||||
if self.den != 1 {
|
||||
return Err(FractionError::InvalidInteger);
|
||||
}
|
||||
|
||||
Ok(self.num)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user