Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing floating point numbers from rust to python #7259

Merged
merged 2 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/python/pants/engine/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ def extern_store_i64(self, context_handle, i64):
c = self._ffi.from_handle(context_handle)
return c.to_value(i64)

@_extern_decl('Handle', ['ExternContext*', 'double'])
def extern_store_f64(self, context_handle, f64):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extern_store_double? (not a blocking comment though)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "double" bit is only how C would describe this... and it's not particularly descriptive. It looks likeC/C++ don't have type declarations equivalent to uint64_t for floating point... which is a bummer: https://stackoverflow.com/questions/2524737/fixed-size-floating-point-types

But I think the method name is right, and more descriptive.

"""Given a context and double, return a new Handle to represent the double."""
c = self._ffi.from_handle(context_handle)
return c.to_value(f64)

@_extern_decl('Handle', ['ExternContext*', '_Bool'])
def extern_store_bool(self, context_handle, b):
"""Given a context and _Bool, return a new Handle to represent the _Bool."""
Expand Down Expand Up @@ -634,6 +640,7 @@ def init_externs():
self.ffi_lib.extern_store_bytes,
self.ffi_lib.extern_store_utf8,
self.ffi_lib.extern_store_i64,
self.ffi_lib.extern_store_f64,
self.ffi_lib.extern_store_bool,
self.ffi_lib.extern_project_ignoring_type,
self.ffi_lib.extern_project_multi,
Expand Down
8 changes: 8 additions & 0 deletions src/rust/engine/src/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ pub fn store_i64(val: i64) -> Value {
with_externs(|e| (e.store_i64)(e.context, val).into())
}

#[allow(dead_code)]
pub fn store_f64(val: f64) -> Value {
with_externs(|e| (e.store_f64)(e.context, val).into())
}

#[allow(dead_code)]
pub fn store_bool(val: bool) -> Value {
with_externs(|e| (e.store_bool)(e.context, val).into())
Expand Down Expand Up @@ -343,6 +348,7 @@ pub struct Externs {
pub store_bytes: StoreBytesExtern,
pub store_utf8: StoreUtf8Extern,
pub store_i64: StoreI64Extern,
pub store_f64: StoreF64Extern,
pub store_bool: StoreBoolExtern,
pub project_ignoring_type: ProjectIgnoringTypeExtern,
pub project_multi: ProjectMultiExtern,
Expand Down Expand Up @@ -382,6 +388,8 @@ pub type StoreUtf8Extern = extern "C" fn(*const ExternContext, *const u8, u64) -

pub type StoreI64Extern = extern "C" fn(*const ExternContext, i64) -> Handle;

pub type StoreF64Extern = extern "C" fn(*const ExternContext, f64) -> Handle;

pub type StoreBoolExtern = extern "C" fn(*const ExternContext, bool) -> Handle;

///
Expand Down
6 changes: 4 additions & 2 deletions src/rust/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ use crate::externs::{
Buffer, BufferBuffer, CallExtern, CloneValExtern, CreateExceptionExtern, DropHandlesExtern,
EqualsExtern, EvalExtern, ExternContext, Externs, GeneratorSendExtern, HandleBuffer,
IdentifyExtern, LogExtern, ProjectIgnoringTypeExtern, ProjectMultiExtern, PyResult,
SatisfiedByExtern, SatisfiedByTypeExtern, StoreBoolExtern, StoreBytesExtern, StoreI64Extern,
StoreTupleExtern, StoreUtf8Extern, TypeIdBuffer, TypeToStrExtern, ValToStrExtern,
SatisfiedByExtern, SatisfiedByTypeExtern, StoreBoolExtern, StoreBytesExtern, StoreF64Extern,
StoreI64Extern, StoreTupleExtern, StoreUtf8Extern, TypeIdBuffer, TypeToStrExtern, ValToStrExtern,
};
use crate::handles::Handle;
use crate::rule_graph::{GraphMaker, RuleGraph};
Expand Down Expand Up @@ -119,6 +119,7 @@ pub extern "C" fn externs_set(
store_bytes: StoreBytesExtern,
store_utf8: StoreUtf8Extern,
store_i64: StoreI64Extern,
store_f64: StoreF64Extern,
store_bool: StoreBoolExtern,
project_ignoring_type: ProjectIgnoringTypeExtern,
project_multi: ProjectMultiExtern,
Expand Down Expand Up @@ -146,6 +147,7 @@ pub extern "C" fn externs_set(
store_bytes,
store_utf8,
store_i64,
store_f64,
store_bool,
project_ignoring_type,
project_multi,
Expand Down