Skip to content

Commit

Permalink
Switch to u64 types for value in cabi (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Oct 23, 2024
1 parent fc09128 commit bf9ef62
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 4 additions & 4 deletions minijinja-cabi/include/minijinja.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ typedef struct mj_value_iter mj_value_iter;
Opaque value type.
*/
typedef struct mj_value {
uintptr_t _opaque[3];
uint64_t _opaque[3];
} mj_value;

/*
Expand Down Expand Up @@ -414,7 +414,7 @@ MINIJINJA_API char *mj_value_to_str(struct mj_value value);
MINIJINJA_API struct mj_value_iter *mj_value_try_iter(struct mj_value value);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
} // extern "C"
#endif // __cplusplus

#endif /* _minijinja_h_included */
#endif /* _minijinja_h_included */
12 changes: 10 additions & 2 deletions minijinja-cabi/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ where
/// Opaque value type.
#[repr(C)]
pub struct mj_value {
_opaque: [usize; 3],
// Motivation on the size here: The size of `Value` is really not
// known and since cbindgen has no way to guarnatee us a matching
// size we have to be creative. The dominating type size wise is
// most likely going to be SmallStr which is a u8+[u8; 22] plus the
// enum discriminant (u8).
//
// We are going with u64 here for alignment reasons which is likely
// to be a good default across platforms.
_opaque: [u64; 3],
}

impl mj_value {
Expand All @@ -51,7 +59,7 @@ impl mj_value {
impl From<Value> for mj_value {
fn from(value: Value) -> Self {
mj_value {
_opaque: unsafe { transmute::<Value, [usize; 3]>(value) },
_opaque: unsafe { transmute::<Value, [u64; 3]>(value) },
}
}
}
Expand Down

0 comments on commit bf9ef62

Please sign in to comment.