Skip to content

Commit

Permalink
Rename and simplify store_list.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuhood committed Apr 26, 2018
1 parent 1acaeec commit bb59a27
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 42 deletions.
24 changes: 6 additions & 18 deletions src/python/pants/engine/native.py
Expand Up @@ -109,7 +109,7 @@
typedef Buffer (*extern_ptr_val_to_str)(ExternContext*, Value*);
typedef _Bool (*extern_ptr_satisfied_by)(ExternContext*, Value*, Value*);
typedef _Bool (*extern_ptr_satisfied_by_type)(ExternContext*, Value*, TypeId*);
typedef Value (*extern_ptr_store_list)(ExternContext*, Value**, uint64_t, _Bool);
typedef Value (*extern_ptr_store_tuple)(ExternContext*, Value*, uint64_t);
typedef Value (*extern_ptr_store_bytes)(ExternContext*, uint8_t*, uint64_t);
typedef Value (*extern_ptr_store_i32)(ExternContext*, int32_t);
typedef ValueBuffer (*extern_ptr_project_multi)(ExternContext*, Value*, uint8_t*, uint64_t);
Expand Down Expand Up @@ -153,7 +153,7 @@
extern_ptr_val_to_str,
extern_ptr_satisfied_by,
extern_ptr_satisfied_by_type,
extern_ptr_store_list,
extern_ptr_store_tuple,
extern_ptr_store_bytes,
extern_ptr_store_i32,
extern_ptr_project_ignoring_type,
Expand Down Expand Up @@ -244,7 +244,7 @@
Buffer extern_val_to_str(ExternContext*, Value*);
_Bool extern_satisfied_by(ExternContext*, Value*, Value*);
_Bool extern_satisfied_by_type(ExternContext*, Value*, TypeId*);
Value extern_store_list(ExternContext*, Value**, uint64_t, _Bool);
Value extern_store_tuple(ExternContext*, Value*, uint64_t);
Value extern_store_bytes(ExternContext*, uint8_t*, uint64_t);
Value extern_store_i32(ExternContext*, int32_t);
Value extern_project_ignoring_type(ExternContext*, Value*, uint8_t*, uint64_t);
Expand Down Expand Up @@ -396,22 +396,10 @@ def extern_satisfied_by_type(context_handle, constraint_val, cls_id):
return constraint.satisfied_by_type(c.from_id(cls_id.id_))

@ffi.def_extern()
def extern_store_list(context_handle, vals_ptr_ptr, vals_len, merge):
def extern_store_tuple(context_handle, vals_ptr, vals_len):
"""Given storage and an array of Values, return a new Value to represent the list."""
c = ffi.from_handle(context_handle)
vals = tuple(c.from_value(val) for val in ffi.unpack(vals_ptr_ptr, vals_len))
if merge:
# Expect each obj to represent a list, and do a de-duping merge.
merged_set = set()
def merged():
for outer_val in vals:
for inner_val in outer_val:
if inner_val in merged_set:
continue
merged_set.add(inner_val)
yield inner_val
vals = tuple(merged())
return c.to_value(vals)
return c.to_value(tuple(c.from_value(val) for val in ffi.unpack(vals_ptr, vals_len)))

@ffi.def_extern()
def extern_store_bytes(context_handle, bytes_ptr, bytes_len):
Expand Down Expand Up @@ -674,7 +662,7 @@ def init_externs():
self.ffi_lib.extern_val_to_str,
self.ffi_lib.extern_satisfied_by,
self.ffi_lib.extern_satisfied_by_type,
self.ffi_lib.extern_store_list,
self.ffi_lib.extern_store_tuple,
self.ffi_lib.extern_store_bytes,
self.ffi_lib.extern_store_i32,
self.ffi_lib.extern_project_ignoring_type,
Expand Down
21 changes: 5 additions & 16 deletions src/rust/engine/src/externs.rs
Expand Up @@ -54,16 +54,8 @@ pub fn satisfied_by_type(constraint: &TypeConstraint, cls: &TypeId) -> bool {
with_externs(|e| (e.satisfied_by_type)(e.context, interns.get(&constraint.0), cls))
}

pub fn store_list(values: Vec<&Value>, merge: bool) -> Value {
with_externs(|e| {
let values_clone: Vec<*const Value> = values.into_iter().map(|v| v as *const Value).collect();
(e.store_list)(
e.context,
values_clone.as_ptr(),
values_clone.len() as u64,
merge,
)
})
pub fn store_tuple(values: &[Value]) -> Value {
with_externs(|e| (e.store_tuple)(e.context, values.as_ptr(), values.len() as u64))
}

pub fn store_bytes(bytes: &[u8]) -> Value {
Expand Down Expand Up @@ -227,7 +219,7 @@ pub struct Externs {
pub drop_handles: DropHandlesExtern,
pub satisfied_by: SatisfiedByExtern,
pub satisfied_by_type: SatisfiedByTypeExtern,
pub store_list: StoreListExtern,
pub store_tuple: StoreTupleExtern,
pub store_bytes: StoreBytesExtern,
pub store_i32: StoreI32Extern,
pub project_ignoring_type: ProjectIgnoringTypeExtern,
Expand All @@ -245,10 +237,8 @@ unsafe impl Send for Externs {}

pub type LogExtern = extern "C" fn(*const ExternContext, u8, str_ptr: *const u8, str_len: u64);

// TODO: Type alias used to avoid rustfmt breaking itself by rendering a 101 character line.
pub type SatisfedBool = bool;
pub type SatisfiedByExtern =
extern "C" fn(*const ExternContext, *const Value, *const Value) -> SatisfedBool;
extern "C" fn(*const ExternContext, *const Value, *const Value) -> bool;

pub type SatisfiedByTypeExtern =
extern "C" fn(*const ExternContext, *const Value, *const TypeId) -> bool;
Expand All @@ -261,8 +251,7 @@ pub type CloneValExtern = extern "C" fn(*const ExternContext, *const Value) -> V

pub type DropHandlesExtern = extern "C" fn(*const ExternContext, *const Handle, u64);

pub type StoreListExtern =
extern "C" fn(*const ExternContext, *const *const Value, u64, bool) -> Value;
pub type StoreTupleExtern = extern "C" fn(*const ExternContext, *const Value, u64) -> Value;

pub type StoreBytesExtern = extern "C" fn(*const ExternContext, *const u8, u64) -> Value;

Expand Down
8 changes: 4 additions & 4 deletions src/rust/engine/src/lib.rs
Expand Up @@ -45,7 +45,7 @@ use externs::{Buffer, BufferBuffer, CallExtern, CloneValExtern, CreateExceptionE
DropHandlesExtern, EqualsExtern, EvalExtern, ExternContext, Externs,
GeneratorSendExtern, IdentifyExtern, LogExtern, ProjectIgnoringTypeExtern,
ProjectMultiExtern, PyResult, SatisfiedByExtern, SatisfiedByTypeExtern,
StoreBytesExtern, StoreI32Extern, StoreListExtern, TypeIdBuffer, TypeToStrExtern,
StoreBytesExtern, StoreI32Extern, StoreTupleExtern, TypeIdBuffer, TypeToStrExtern,
ValToStrExtern};
use rule_graph::{GraphMaker, RuleGraph};
use scheduler::{ExecutionRequest, RootResult, Scheduler};
Expand Down Expand Up @@ -134,7 +134,7 @@ pub extern "C" fn externs_set(
val_to_str: ValToStrExtern,
satisfied_by: SatisfiedByExtern,
satisfied_by_type: SatisfiedByTypeExtern,
store_list: StoreListExtern,
store_tuple: StoreTupleExtern,
store_bytes: StoreBytesExtern,
store_i32: StoreI32Extern,
project_ignoring_type: ProjectIgnoringTypeExtern,
Expand All @@ -157,7 +157,7 @@ pub extern "C" fn externs_set(
val_to_str,
satisfied_by,
satisfied_by_type,
store_list,
store_tuple,
store_bytes,
store_i32,
project_ignoring_type,
Expand Down Expand Up @@ -456,7 +456,7 @@ pub extern "C" fn execution_request_destroy(ptr: *mut ExecutionRequest) {
pub extern "C" fn validator_run(scheduler_ptr: *mut Scheduler) -> Value {
with_scheduler(scheduler_ptr, |scheduler| {
match scheduler.core.rule_graph.validate() {
Result::Ok(_) => externs::store_list(vec![], false),
Result::Ok(_) => externs::store_tuple(&[]),
Result::Err(msg) => externs::create_exception(&msg),
}
})
Expand Down
8 changes: 4 additions & 4 deletions src/rust/engine/src/nodes.rs
Expand Up @@ -492,7 +492,7 @@ impl SelectDependencies {
.then(move |dep_values_res| {
// Finally, store the resulting values.
match dep_values_res {
Ok(dep_values) => Ok(externs::store_list(dep_values.iter().collect(), false)),
Ok(dep_values) => Ok(externs::store_tuple(&dep_values)),
Err(failure) => Err(was_required(failure)),
}
})
Expand Down Expand Up @@ -736,7 +736,7 @@ impl Snapshot {
&[
externs::store_bytes(&(item.digest.0).to_hex().as_bytes()),
externs::store_i32(item.digest.1 as i32),
externs::store_list(path_stats.iter().collect(), false),
externs::store_tuple(&path_stats),
],
)
}
Expand Down Expand Up @@ -784,7 +784,7 @@ impl Snapshot {
.collect();
externs::unsafe_call(
&context.core.types.construct_files_content,
&[externs::store_list(entries.iter().collect(), false)],
&[externs::store_tuple(&entries)],
)
}
}
Expand Down Expand Up @@ -894,7 +894,7 @@ impl Task {
.map(|vs| future::Loop::Continue(vs.into_iter().next().unwrap()))
.to_boxed(),
externs::GeneratorResponse::GetMulti(gets) => Self::gen_get(&context, entry, gets)
.map(|vs| future::Loop::Continue(externs::store_list(vs.iter().collect(), false)))
.map(|vs| future::Loop::Continue(externs::store_tuple(&vs)))
.to_boxed(),
externs::GeneratorResponse::Break(val) => future::ok(future::Loop::Break(val)).to_boxed(),
}
Expand Down

0 comments on commit bb59a27

Please sign in to comment.