Add a panic! when in debug mode
@@ -80,11 +80,15 @@ trait ToI64 {
impl ToI64 for usize {
fn to_i64(self) -> i64 {
if mem::size_of::<usize>() >= mem::size_of::<i64>() && self > i64::max_value() as usize {
+ if cfg!(debug_assertions) {
+ panic!("Tried to convert an out-of-range usize ({}) to i64", self);
+ } else {
error!(
"Tried to convert an out-of-range usize ({}) to i64; using i64::max_value()",
self
);
i64::max_value()
+ }
} else {
self as i64
}
@@ -93,12 +97,16 @@ impl ToI64 for usize {
impl ToI64 for u64 {
fn to_i64(self) -> i64 {
- if mem::size_of::<u64>() >= mem::size_of::<i64>() && self > i64::max_value() as u64 {
+ if self > i64::max_value() as u64 {
+ if cfg!(debug_assertions) {
+ panic!("Tried to convert an out-of-range u64 ({}) to i64", self);
+ } else {
error!(
"Tried to convert an out-of-range u64 ({}) to i64; using i64::max_value()",
self
);
i64::max_value()
+ }
} else {
self as i64
}
@@ -111,23 +119,49 @@ mod tests {
#[test]
fn conversion_of_usize_to_i64() {
- let too_big = usize::max_value();
let just_right: usize = 42;
let zero: usize = 0;
- assert_eq!(too_big.to_i64(), i64::max_value());
assert_eq!(just_right.to_i64(), 42);
assert_eq!(zero.to_i64(), 0);
}
+ #[test]
+ #[should_panic]
+ #[cfg(debug_assertions)]
+ fn conversion_of_too_big_usize_panics_in_debug_mode() {
+ let too_big = usize::max_value();
+ too_big.to_i64();
+ }
+
+ #[test]
+ #[cfg(not(debug_assertions))]
+ fn conversion_of_too_big_usize_caps_in_release_mode() {
+ let too_big = usize::max_value();
+ assert_eq!(too_big.to_i64(), i64::max_value());
+ }
+
#[test]
fn conversion_of_u64_to_i64() {
- let too_big = u64::max_value();
let just_right: u64 = 42;
let zero: u64 = 0;
- assert_eq!(too_big.to_i64(), i64::max_value());
assert_eq!(just_right.to_i64(), 42);
assert_eq!(zero.to_i64(), 0);
}
+
+ #[test]
+ #[should_panic]
+ #[cfg(debug_assertions)]
+ fn conversion_of_too_big_u64_panics_in_debug_mode() {
+ let too_big = u64::max_value();
+ too_big.to_i64();
+ }
+
+ #[test]
+ #[cfg(not(debug_assertions))]
+ fn conversion_of_too_big_u64_caps_in_release_mode() {
+ let too_big = u64::max_value();
+ assert_eq!(too_big.to_i64(), i64::max_value());
+ }
}
