Skip to content

Commit

Permalink
Merge pull request lcm-proj#19 from adeschamps/17-fix-hashes
Browse files Browse the repository at this point in the history
Fix hashes and `use` errors

Closes lcm-proj#17 and lcm-proj#18
  • Loading branch information
neachdainn authored Jun 10, 2017
2 parents 7953ae4 + 3cd8750 commit 3924bae
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions lcmgen/emit_rust.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,30 @@ static void emit_struct_def(lcmgen_t *lcmgen, FILE *f, lcm_struct_t *lcm_struct)
// Include non-primitive types
for (unsigned int mind = 0; mind < g_ptr_array_size(lcm_struct->members); mind++) {
lcm_member_t *lm = (lcm_member_t *)g_ptr_array_index(lcm_struct->members, mind);
if (!lcm_is_primitive_type(lm->type->lctypename) &&
if (!lcm_is_primitive_type(lm->type->lctypename) &&
strcmp(lm->type->lctypename, lcm_struct->structname->lctypename)) {
char *mapped_tn = map_type_name(lm->type);
char *other_pn = dots_to_double_colons(lm->type->package);
emit(0, "use %s::%s;", other_pn, mapped_tn);
free(other_pn);
free(mapped_tn);

// This is naive, but it is highly unlikely any of these lists will
// ever get very large.
int skip = 0;
for (unsigned int prev = 0; prev < mind; prev++) {
lcm_member_t * pm = (lcm_member_t *)g_ptr_array_index(lcm_struct->members, prev);
if (!lcm_is_primitive_type(pm->type->lctypename) &&
strcmp(pm->type->lctypename, lm->type->lctypename) == 0) {
// We've already emitted a "use" statement for this type
skip = 1;
break;
}
}

if(!skip) {
// This is the first time "use"ing this type
char *mapped_tn = map_type_name(lm->type);
char *other_pn = dots_to_double_colons(lm->type->package);
emit(0, "use %s::%s;", other_pn, mapped_tn);
free(other_pn);
free(mapped_tn);
}
}
}

Expand Down Expand Up @@ -387,7 +404,20 @@ static void emit_constants(lcmgen_t *lcmgen, FILE *f, lcm_struct_t *lcm_struct)

static void emit_impl_message_hash(FILE *f, lcm_struct_t *lcm_struct) {
emit(1, "fn hash() -> u64 {");
emit(2, "let hash = 0x%016"PRIx64";", lcm_struct->hash);
emit(2, "let hash = {");
emit(3, "0x%016"PRIx64"u64", lcm_struct->hash);

for (unsigned int mind = 0; mind < g_ptr_array_size(lcm_struct->members); mind++) {
lcm_member_t *lm = (lcm_member_t *)g_ptr_array_index(lcm_struct->members, mind);
const char *tn = lm->type->lctypename;
if (!lcm_is_primitive_type(tn) && strcmp(tn, lcm_struct->structname->lctypename)) {
char *mapped_tn = map_type_name(lm->type);
emit(3, ".wrapping_add(%s::hash())", mapped_tn);
free(mapped_tn);
}
}

emit(2, "};");
emit(2, "(hash << 1) + ((hash >> 63) & 1)");
emit(1, "}");
emit(0, "");
Expand Down

0 comments on commit 3924bae

Please sign in to comment.