Skip to content

Commit

Permalink
Update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtribick committed Sep 2, 2020
1 parent 4bdc1ed commit 0db51eb
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 150 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 0.4.0

* Breaking change: update error handling to make it more future-proof.

## Version 0.3.1

* Support Default trait.
Expand Down
39 changes: 20 additions & 19 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use core::cmp::Eq;
use core::convert::{From, TryFrom};
use core::fmt;
use std::error;

use crate::base::{no_overlap, TwoFloat};

/// Error indicating invalid conversions to/from `TwoFloat`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ConversionError;
/// The error type for `TwoFloat` operations.
#[non_exhaustive]
#[derive(Debug)]
pub enum TwoFloatError {
/// Indicates invalid conversion to/from `TwoFloat`
ConversionError,
}

impl fmt::Display for ConversionError {
impl fmt::Display for TwoFloatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid TwoFloat conversion")
match self {
Self::ConversionError => write!(f, "invalid TwoFloat conversion"),
}
}
}

impl error::Error for ConversionError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}
impl error::Error for TwoFloatError {}

macro_rules! from_conversion {
(|$source_i:ident : TwoFloat| -> $dest:tt $code:block) => {
Expand Down Expand Up @@ -49,7 +50,7 @@ macro_rules! from_conversion {
from_conversion!(|value: TwoFloat| -> (f64, f64) { (value.hi, value.lo) });

impl TryFrom<(f64, f64)> for TwoFloat {
type Error = ConversionError;
type Error = TwoFloatError;

fn try_from(value: (f64, f64)) -> Result<Self, Self::Error> {
if no_overlap(value.0, value.1) {
Expand All @@ -58,15 +59,15 @@ impl TryFrom<(f64, f64)> for TwoFloat {
lo: value.1,
})
} else {
Err(Self::Error {})
Err(Self::Error::ConversionError {})
}
}
}

from_conversion!(|value: TwoFloat| -> [f64; 2] { [value.hi, value.lo] });

impl TryFrom<[f64; 2]> for TwoFloat {
type Error = ConversionError;
type Error = TwoFloatError;

fn try_from(value: [f64; 2]) -> Result<Self, Self::Error> {
if no_overlap(value[0], value[1]) {
Expand All @@ -75,7 +76,7 @@ impl TryFrom<[f64; 2]> for TwoFloat {
lo: value[1],
})
} else {
Err(Self::Error {})
Err(Self::Error::ConversionError {})
}
}
}
Expand Down Expand Up @@ -109,12 +110,12 @@ macro_rules! int_convert {
}
}

from_conversion!(|value: TwoFloat| -> Result<$type, ConversionError> {
from_conversion!(|value: TwoFloat| -> Result<$type, TwoFloatError> {
const LOWER_BOUND: f64 = $type::MIN as f64;
const UPPER_BOUND: f64 = $type::MAX as f64;
let truncated = value.trunc();
if truncated < LOWER_BOUND || truncated > UPPER_BOUND {
Err(ConversionError {})
Err(Self::Error::ConversionError {})
} else {
Ok(truncated.hi() as $type)
}
Expand Down Expand Up @@ -146,7 +147,7 @@ macro_rules! bigint_convert {
}
}

from_conversion!(|value: TwoFloat| -> Result<$type, ConversionError> {
from_conversion!(|value: TwoFloat| -> Result<$type, TwoFloatError> {
const LOWER_BOUND: TwoFloat = TwoFloat {
hi: $type::MIN as f64,
lo: 0.0,
Expand All @@ -159,7 +160,7 @@ macro_rules! bigint_convert {

let truncated = value.trunc();
if truncated < LOWER_BOUND || truncated > UPPER_BOUND {
Err(ConversionError {})
Err(Self::Error::ConversionError {})
} else if truncated.hi() == UPPER_BOUND.hi() {
Ok($type::MAX - (-truncated.lo() as $type) + 1)
} else if truncated.lo() >= 0.0 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ mod convert;
mod functions;

pub use base::{no_overlap, TwoFloat};
pub use convert::ConversionError;
pub use convert::TwoFloatError;
10 changes: 5 additions & 5 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use core::convert::TryFrom;
use rand::Rng;
use twofloat::{ConversionError, TwoFloat};
use twofloat::{TwoFloat, TwoFloatError};

pub const TEST_ITERS: usize = 100000;

Expand Down Expand Up @@ -52,7 +52,7 @@ pub fn get_twofloat(rng: F64Rand) -> TwoFloat {
}
}

pub fn try_get_twofloat_with_hi(rng: F64Rand, hi: f64) -> Result<TwoFloat, ConversionError> {
pub fn try_get_twofloat_with_hi(rng: F64Rand, hi: f64) -> Result<TwoFloat, TwoFloatError> {
if hi == 0.0 {
return Ok(TwoFloat::from(0.0));
}
Expand All @@ -64,18 +64,18 @@ pub fn try_get_twofloat_with_hi(rng: F64Rand, hi: f64) -> Result<TwoFloat, Conve
}
}

Err(ConversionError {})
Err(TwoFloatError::ConversionError {})
}

pub fn try_get_twofloat_with_lo(rng: F64Rand, lo: f64) -> Result<TwoFloat, ConversionError> {
pub fn try_get_twofloat_with_lo(rng: F64Rand, lo: f64) -> Result<TwoFloat, TwoFloatError> {
for _ in 0..10 {
let result = TwoFloat::try_from((rng(), lo));
if result.is_ok() {
return result;
}
}

Err(ConversionError {})
Err(TwoFloatError::ConversionError {})
}

pub fn get_valid_twofloat<F: Fn(f64, f64) -> bool>(rng: F64Rand, pred: F) -> TwoFloat {
Expand Down
Loading

0 comments on commit 0db51eb

Please sign in to comment.