From 87c7d6b0308c2b3d44c79c30499658267265384a Mon Sep 17 00:00:00 2001 From: david-perez Date: Thu, 3 Mar 2022 20:06:11 +0100 Subject: [PATCH] Panic when converting float to int --- rust-runtime/aws-smithy-types/src/lib.rs | 42 +++++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/rust-runtime/aws-smithy-types/src/lib.rs b/rust-runtime/aws-smithy-types/src/lib.rs index 3dc67cc221..5ba42c8784 100644 --- a/rust-runtime/aws-smithy-types/src/lib.rs +++ b/rust-runtime/aws-smithy-types/src/lib.rs @@ -100,19 +100,33 @@ macro_rules! to_num_fn { }; } +macro_rules! to_num_fn_not_from_float { + ($name:ident, $typ:ident) => { + /// Converts to a `$typ`. This conversion may be lossy. + pub fn $name(self) -> $typ { + match self { + Number::PosInt(val) => val as $typ, + Number::NegInt(val) => val as $typ, + Number::Float(_val) => panic!("Lossy conversion not allowed"), + // Number::Float(val) => val as $typ, + } + } + }; +} + impl Number { to_num_fn!(to_f32, f32); to_num_fn!(to_f64, f64); - to_num_fn!(to_i8, i8); - to_num_fn!(to_i16, i16); - to_num_fn!(to_i32, i32); - to_num_fn!(to_i64, i64); + to_num_fn_not_from_float!(to_i8, i8); + to_num_fn_not_from_float!(to_i16, i16); + to_num_fn_not_from_float!(to_i32, i32); + to_num_fn_not_from_float!(to_i64, i64); - to_num_fn!(to_u8, u8); - to_num_fn!(to_u16, u16); - to_num_fn!(to_u32, u32); - to_num_fn!(to_u64, u64); + to_num_fn_not_from_float!(to_u8, u8); + to_num_fn_not_from_float!(to_u16, u16); + to_num_fn_not_from_float!(to_u32, u32); + to_num_fn_not_from_float!(to_u64, u64); } /* ANCHOR_END: document */ @@ -260,3 +274,15 @@ pub mod error { impl std::error::Error for Error {} } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hello() { + let num = Number::Float(f64::NAN); + let int = num.to_i32(); + dbg!(&int); // With lossy conversion, NaN gets converted to 0. + } +}