-
Notifications
You must be signed in to change notification settings - Fork 56
Fix #38 - support rust_decimal.
#42
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a1a5cc0
Support rust_decimal.
finnbear 424032f
Use macro.
finnbear b0aa34c
Round-trip -0.
finnbear ee25054
Test retention of trailing zero.
finnbear 2c6dffa
fmt.
finnbear 6ea4b0b
Undo mistake.
finnbear b3a3804
Undo mistake 2.
finnbear cbadccf
Undo mistake 3.
finnbear 8a598b3
Prepare optimization.
finnbear 154f0f9
Undo fmt.
finnbear 0772ea0
Bench.
finnbear f1f2654
Merge + WIP.
finnbear 700e75e
Optimize?
finnbear 7ea876c
Cleanup.
finnbear a652110
More tests.
finnbear 230f111
Move code.
finnbear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| use crate::{ | ||
| convert::{self, impl_convert, ConvertFrom}, | ||
| Decode, Encode, | ||
| }; | ||
| use bytemuck::CheckedBitPattern; | ||
| use rust_decimal::Decimal; | ||
| type DecimalConversion = (u32, u32, u32, Flags); | ||
|
|
||
| impl ConvertFrom<&Decimal> for DecimalConversion { | ||
| fn convert_from(value: &Decimal) -> Self { | ||
| let unpacked = value.unpack(); | ||
| ( | ||
| unpacked.lo, | ||
| unpacked.mid, | ||
| unpacked.hi, | ||
| Flags::new(unpacked.scale, unpacked.negative), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl ConvertFrom<DecimalConversion> for Decimal { | ||
| fn convert_from(value: DecimalConversion) -> Self { | ||
| let scale = value.3.scale(); | ||
| // Should make Decimal::from_parts faster, once it can be inlined, | ||
| // since it can skip division. | ||
| // Safety: impl CheckedBitPattern for Flags guarantees this. | ||
| unsafe { | ||
| if scale > 28 { | ||
| core::hint::unreachable_unchecked(); | ||
| } | ||
| } | ||
| let mut ret = Self::from_parts(value.0, value.1, value.2, false, scale); | ||
| ret.set_sign_negative(value.3.negative()); | ||
| ret | ||
| } | ||
| } | ||
|
|
||
| impl_convert!(Decimal, DecimalConversion); | ||
|
|
||
| impl ConvertFrom<&Flags> for u8 { | ||
| fn convert_from(flags: &Flags) -> Self { | ||
| flags.0 | ||
| } | ||
| } | ||
|
|
||
| impl Encode for Flags { | ||
| type Encoder = convert::ConvertIntoEncoder<u8>; | ||
| } | ||
|
|
||
| /// A u8 guaranteed to satisfy (flags >> 1) <= 28. Prevents Decimal::from_parts from misbehaving. | ||
| #[derive(Copy, Clone)] | ||
| #[repr(transparent)] | ||
| pub struct Flags(u8); | ||
|
|
||
| impl Flags { | ||
| #[inline(always)] | ||
| fn new(scale: u32, negative: bool) -> Self { | ||
| Self((scale as u8) << 1 | negative as u8) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn scale(&self) -> u32 { | ||
| (self.0 >> 1) as u32 | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn negative(&self) -> bool { | ||
| self.0 & 1 == 1 | ||
| } | ||
| } | ||
|
|
||
| // Safety: u8 and Flags have the same layout since Flags is #[repr(transparent)]. | ||
| unsafe impl CheckedBitPattern for Flags { | ||
| type Bits = u8; | ||
| #[inline(always)] | ||
| fn is_valid_bit_pattern(bits: &Self::Bits) -> bool { | ||
| (*bits >> 1) <= 28 | ||
| } | ||
| } | ||
|
|
||
| impl<'a> Decode<'a> for Flags { | ||
| type Decoder = crate::int::CheckedIntDecoder<'a, Flags, u8>; | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::{decode, encode}; | ||
| use rust_decimal::Decimal; | ||
| use std::str::FromStr; | ||
|
|
||
| #[test] | ||
| fn rust_decimal() { | ||
| let vs = [ | ||
| Decimal::from(0), | ||
| Decimal::from_f64_retain(-0f64).unwrap(), | ||
| Decimal::from(-1), | ||
| Decimal::from(1) / Decimal::from(2), | ||
| Decimal::from(1), | ||
| Decimal::from(999999999999999999u64), | ||
| Decimal::from_str("3.100").unwrap(), | ||
| ]; | ||
| for v in vs { | ||
| let d = decode::<Decimal>(&encode(&v)).unwrap(); | ||
| assert_eq!(d, v); | ||
| assert_eq!(d.is_sign_negative(), v.is_sign_negative()); | ||
| assert_eq!(d.scale(), v.scale()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.