Skip to content

Commit

Permalink
Removing unsafe with reusing existing f64 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Virviil committed Mar 30, 2021
1 parent 223dfce commit 5505698
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions rustler/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,14 @@ impl<'a> Decoder<'a> for bool {

impl Encoder for f32 {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
#[allow(clippy::cast_lossless)]
unsafe {
Term::new(
env,
rustler_sys::enif_make_double(env.as_c_arg(), *self as f64),
)
}
f64::from(*self).encode(env)
}
}
impl<'a> Decoder<'a> for f32 {
fn decode(term: Term) -> NifResult<f32> {
#![allow(unused_unsafe)]
let mut res: f64 = Default::default();
if unsafe {
rustler_sys::enif_get_double(term.get_env().as_c_arg(), term.as_c_arg(), &mut res)
} == 0
{
return Err(Error::BadArg);
}
let res: f64 = term.decode()?;
let res = res as f32;
// Values bigger then f32 are coerced as infinity
// Values bigger than f32 are coerced as infinity
if res.is_finite() {
Ok(res)
} else {
Expand Down

0 comments on commit 5505698

Please sign in to comment.