Skip to content

Commit e746187

Browse files
committed
Fix the float truncation test.
num-traits (since 0.2.13) now returns 'inf' instead of an error when converting from an out-of-range f64 to f32. We now do the out of range check on the rlua side. Also update to num-traits 0.2.14 (current at time of writing) to be sure we're past that change there.
1 parent 13a66cf commit e746187

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ system-lua = ["pkg-config"]
3434

3535
[dependencies]
3636
libc = { version = "0.2" }
37-
num-traits = { version = "0.2.6" }
37+
num-traits = { version = "0.2.14" }
3838
bitflags = { version = "1.0.4" }
3939
bstr = {version = "0.2", features = ["std"], default_features = false }
4040

src/conversion.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,17 @@ macro_rules! lua_convert_float {
359359
message: Some("expected number or string coercible to number".to_string()),
360360
})
361361
.and_then(|n| {
362+
// We want out of range f32 to return an error instead
363+
// of inf.
364+
if std::mem::size_of::<$x>() < std::mem::size_of::<f64>() {
365+
if n.is_finite() && n.abs() > (<$x>::MAX as f64) {
366+
return Err(Error::FromLuaConversionError {
367+
from: ty,
368+
to: stringify!($x),
369+
message: Some("number out of range".to_string()),
370+
});
371+
}
372+
}
362373
cast(n).ok_or_else(|| Error::FromLuaConversionError {
363374
from: ty,
364375
to: stringify!($x),

tests/tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,7 @@ fn test_num_conversion() {
428428
lua.unpack::<f64>(lua.pack(f32::MAX).unwrap()).unwrap(),
429429
f32::MAX as f64
430430
);
431-
assert!(dbg!(lua.unpack::<f32>(lua.pack(f64::MAX).unwrap())).is_err());
432-
assert!(false);
431+
assert!(lua.unpack::<f32>(lua.pack(f64::MAX).unwrap()).is_err());
433432

434433
assert_eq!(
435434
lua.unpack::<i128>(lua.pack(1i128 << 64).unwrap()).unwrap(),

0 commit comments

Comments
 (0)