I’ve got this code:
let rarity_from_db: Decimal = row.get("rarity");
let rarity = rarity_from_db.to_f64().unwrap_or_else(|| {
error!(
"Could not convert Numeric rarity value to f64 for record with ID {id}"
);
0.0
});
It’s inside a From
trait implementation that converts data from a database Row
type of tokio_postgres to my custom struct type, and in this specific case from Decimal
to f64
. The issue I get is that there are rows which have crazy numbers like 0.000000000000000000000002337556151251862
and the conversion fails with process panic:
thread 'tokio-runtime-worker' panicked at 'attempt to multiply with overflow'
I’m not sure:
- why the
unwrap_or_else
isn’t catching error (the process still panics)? - how can I safely convert the decimal number?