Skip to content

Commit

Permalink
Add c-repr feature
Browse files Browse the repository at this point in the history
This adds the c-repr feature which forces `Decimal` to be #[repr(C)].

Closes paupino#359.
  • Loading branch information
jean-airoldie committed Apr 30, 2021
1 parent 322e3d1 commit 9b5f427
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ serde-str = ["serde"]
serde-arbitrary-precision = ["serde", "serde_json/arbitrary_precision"]
std = ["arrayvec/std"]
tokio-pg = ["db-tokio-postgres"] # Backwards compatability
c-repr = [] # Force Decimal to be repr(C)

[workspace]
members = [".", "./macros"]
Expand Down
1 change: 1 addition & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct UnpackedDecimal {
/// between 0 and 28 inclusive.
#[derive(Clone, Copy)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, AsExpression), sql_type = "Numeric")]
#[cfg_attr(feature = "c-repr", repr(C))]
pub struct Decimal {
// Bits 0-15: unused
// Bits 16-23: Contains "e", a value between 0-28 that indicates the scale
Expand Down
6 changes: 6 additions & 0 deletions tests/decimal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ macro_rules! either {
};
}

#[test]
#[cfg(feature = "c-repr")]
fn layout_is_correct() {
assert_eq!(std::mem::size_of::<Decimal>(), std::mem::size_of::<u128>());
}

#[test]
fn it_can_extract_the_mantissa() {
let tests = [
Expand Down

0 comments on commit 9b5f427

Please sign in to comment.