Skip to content

Commit

Permalink
Implement is_float using enif_get_double
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Feb 13, 2024
1 parent 940423c commit d79d918
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion rustler/src/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::c_double;

#[cfg(feature = "nif_version_2_15")]
use rustler_sys::ErlNifTermType;

Expand Down Expand Up @@ -56,7 +58,11 @@ pub fn get_type(term: Term) -> TermType {
} else if term.is_map() {
TermType::Map
} else if term.is_number() {
TermType::Float
if term.is_float() {
TermType::Float
} else {
TermType::Integer
}
} else if term.is_pid() {
TermType::Pid
} else if term.is_port() {
Expand Down Expand Up @@ -98,4 +104,15 @@ impl<'a> Term<'a> {
impl_check!(is_port);
impl_check!(is_ref);
impl_check!(is_tuple);

pub fn is_float(self) -> bool {
let mut val: c_double = 0.0;
unsafe {
rustler_sys::enif_get_double(self.get_env().as_c_arg(), self.as_c_arg(), &mut val) == 1
}
}

pub fn is_integer(self) -> bool {
self.is_number() && !self.is_float()
}
}

0 comments on commit d79d918

Please sign in to comment.