Debian Codegraph @

src/types/money.rs

raw symbols SCIP
1 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::{
2 decoderust-analyzer cargo sqlx-core 0.8.3 decode/::Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#,
3 encoderust-analyzer cargo sqlx-core 0.8.3 encode/::{Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#, IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#},
4 errorrust-analyzer cargo sqlx-postgres 0.8.3 error/::BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#,
5 typesrust-analyzer cargo sqlx-postgres 0.8.3 types/::Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#,
6 {PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#, PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#, PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#, PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#, PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#},
7 };
8 use byteorderrust-analyzer cargo byteorder 1.5.0 crate/::{BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#, ByteOrderrust-analyzer cargo byteorder 1.5.0 ByteOrder#};
9 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
10 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/,
11 opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#, AddAssignrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#, Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#, SubAssignrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/SubAssign#},
12 };
13
14 /// The PostgreSQL [`MONEY`] type stores a currency amount with a fixed fractional
15 /// precision. The fractional precision is determined by the database's
16 /// `lc_monetary` setting.
17 ///
18 /// Data is read and written as 64-bit signed integers, and conversion into a
19 /// decimal should be done using the right precision.
20 ///
21 /// Reading `MONEY` value in text format is not supported and will cause an error.
22 ///
23 /// ### `locale_frac_digits`
24 /// This parameter corresponds to the number of digits after the decimal separator.
25 ///
26 /// This value must match what Postgres is expecting for the locale set in the database
27 /// or else the decimal value you see on the client side will not match the `money` value
28 /// on the server side.
29 ///
30 /// **For _most_ locales, this value is `2`.**
31 ///
32 /// If you're not sure what locale your database is set to or how many decimal digits it specifies,
33 /// you can execute `SHOW lc_monetary;` to get the locale name, and then look it up in this list
34 /// (you can ignore the `.utf8` prefix):
35 /// <https://lh.2xlibre.net/values/frac_digits/>
36 ///
37 /// If that link is dead and you're on a POSIX-compliant system (Unix, FreeBSD) you can also execute:
38 ///
39 /// ```sh
40 /// $ LC_MONETARY=<value returned by `SHOW lc_monetary`> locale -k frac_digits
41 /// ```
42 ///
43 /// And the value you want is `N` in `frac_digits=N`. If you have shell access to the database
44 /// server you should execute it there as available locales may differ between machines.
45 ///
46 /// Note that if `frac_digits` for the locale is outside the range `[0, 10]`, Postgres assumes
47 /// it's a sentinel value and defaults to 2:
48 /// <https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/cash.c#L114-L123>
49 ///
50 /// [`MONEY`]: https://www.postgresql.org/docs/current/datatype-money.html
51 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
52 pub struct PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(
53 /// The raw integer value sent over the wire; for locales with `frac_digits=2` (i.e. most
54 /// of them), this will be the value in whole cents.
55 ///
56 /// E.g. for `select '$123.45'::money` with a locale of `en_US` (`frac_digits=2`),
57 /// this will be `12345`.
58 ///
59 /// If the currency of your locale does not have fractional units, e.g. Yen, then this will
60 /// just be the units of the currency.
61 ///
62 /// See the type-level docs for an explanation of `locale_frac_units`.
63 pub i64,
64 );
65
66 impl PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
67 /// Convert the money value into a [`BigDecimal`] using `locale_frac_digits`.
68 ///
69 /// See the type-level docs for an explanation of `locale_frac_digits`.
70 ///
71 /// [`BigDecimal`]: bigdecimal::BigDecimal
72 #[cfg(feature = "bigdecimal")]
73 pub fn to_bigdecimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]to_bigdecimal().(self, locale_frac_digits: i64) -> bigdecimalrust-analyzer cargo bigdecimal 0.4.10 crate/::BigDecimalrust-analyzer cargo bigdecimal 0.4.10 BigDecimal# {
74 let digits = num_bigintrust-analyzer cargo num-bigint 0.4.8 crate/::BigIntrust-analyzer cargo num-bigint 0.4.8 bigint/BigInt#::fromrust-analyzer cargo num-bigint 0.4.8 bigint/convert/impl#[BigInt][`From<i64>`]from().(self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.);
75
76 bigdecimalrust-analyzer cargo bigdecimal 0.4.10 crate/::BigDecimalrust-analyzer cargo bigdecimal 0.4.10 BigDecimal#::newrust-analyzer cargo bigdecimal 0.4.10 impl#[BigDecimal]new().(digits, locale_frac_digits)
77 }
78
79 /// Convert the money value into a [`Decimal`] using `locale_frac_digits`.
80 ///
81 /// See the type-level docs for an explanation of `locale_frac_digits`.
82 ///
83 /// [`Decimal`]: rust_decimal::Decimal
84 #[cfg(feature = "rust_decimal")]
85 pub fn to_decimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]to_decimal().(self, locale_frac_digits: u32) -> rust_decimalrust-analyzer cargo rust_decimal 1.42.1 crate/::Decimalrust-analyzer cargo rust_decimal 1.42.1 decimal/Decimal# {
86 rust_decimalrust-analyzer cargo rust_decimal 1.42.1 crate/::Decimalrust-analyzer cargo rust_decimal 1.42.1 decimal/Decimal#::newrust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]new().(self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0., locale_frac_digits)
87 }
88
89 /// Convert a [`Decimal`] value into money using `locale_frac_digits`.
90 ///
91 /// See the type-level docs for an explanation of `locale_frac_digits`.
92 ///
93 /// Note that `Decimal` has 96 bits of precision, but `PgMoney` only has 63 plus the sign bit.
94 /// If the value is larger than 63 bits it will be truncated.
95 ///
96 /// [`Decimal`]: rust_decimal::Decimal
97 #[cfg(feature = "rust_decimal")]
98 pub fn from_decimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]from_decimal().(mut decimal: rust_decimalrust-analyzer cargo rust_decimal 1.42.1 crate/::Decimalrust-analyzer cargo rust_decimal 1.42.1 decimal/Decimal#, locale_frac_digits: u32) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney] {
99 // this is all we need to convert to our expected locale's `frac_digits`
100 decimal.rescalerust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]rescale().(locale_frac_digits);
101
102 /// a mask to bitwise-AND with an `i64` to zero the sign bit
103 const SIGN_MASKrust-analyzer cargo sqlx-postgres 0.8.3 types/money/SIGN_MASK.: i64 = i64::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]MAX.;
104
105 let is_negative = decimal.is_sign_negativerust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]is_sign_negative().();
106 let serialized = decimal.serializerust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]serialize().();
107
108 // interpret bytes `4..12` as an i64, ignoring the sign bit
109 // this is where truncation occurs
110 let value = i64::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]from_le_bytes().(
111 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().<&[u8; 8]>::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`&'a [T; N]`][`TryFrom<&'a [T]>`]try_from().(&serialized[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().4..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#12]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().)
112 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("BUG: slice of serialized should be 8 bytes"),
113 ) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i64][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i64][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i64][`BitAnd<Self>`]bitand().SIGN_MASKrust-analyzer cargo sqlx-postgres 0.8.3 types/money/SIGN_MASK.; // zero out the sign bit
114
115 // negate if necessary
116 Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney](if is_negative { -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().value } else { value })
117 }
118
119 /// Convert a [`BigDecimal`](bigdecimal::BigDecimal) value into money using the correct precision
120 /// defined in the PostgreSQL settings. The default precision is two.
121 #[cfg(feature = "bigdecimal")]
122 pub fn from_bigdecimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]from_bigdecimal().(
123 decimal: bigdecimalrust-analyzer cargo bigdecimal 0.4.10 crate/::BigDecimalrust-analyzer cargo bigdecimal 0.4.10 BigDecimal#,
124 locale_frac_digits: u32,
125 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney], BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
126 use bigdecimalrust-analyzer cargo bigdecimal 0.4.10 crate/::ToPrimitiverust-analyzer cargo num-traits 0.2.19 cast/ToPrimitive#;
127
128 let multiplier = bigdecimalrust-analyzer cargo bigdecimal 0.4.10 crate/::BigDecimalrust-analyzer cargo bigdecimal 0.4.10 BigDecimal#::newrust-analyzer cargo bigdecimal 0.4.10 impl#[BigDecimal]new().(
129 num_bigintrust-analyzer cargo num-bigint 0.4.8 crate/::BigIntrust-analyzer cargo num-bigint 0.4.8 bigint/BigInt#::fromrust-analyzer cargo num-bigint 0.4.8 bigint/convert/impl#[BigInt][`From<i128>`]from().(10i128.powrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i128]pow().(locale_frac_digits)),
130 0,
131 );
132
133 let cents = decimal rust-analyzer cargo bigdecimal 0.4.10 impl_ops_mul/impl#[BigDecimal][`Mul<Self>`]mul().*rust-analyzer cargo bigdecimal 0.4.10 impl_ops_mul/impl#[BigDecimal][`Mul<Self>`]mul(). rust-analyzer cargo bigdecimal 0.4.10 impl_ops_mul/impl#[BigDecimal][`Mul<Self>`]mul().multiplier;
134
135 let money = cents.to_i64rust-analyzer cargo bigdecimal 0.4.10 impl_num/impl#[BigDecimal][ToPrimitive]to_i64().().ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| {
136 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
137 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#,
138 "Provided BigDecimal could not convert to i64: overflow.",
139 )
140 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
141
142 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney](money))
143 }
144 }
145
146 impl Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
147 fn type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Type<Postgres>`]type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# {
148 PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#::MONEYrust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]MONEY.
149 }
150 }
151
152 impl PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType# for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
153 fn array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][PgHasArrayType]array_type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# {
154 PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#::MONEY_ARRAYrust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]MONEY_ARRAY.
155 }
156 }
157
158 impl<T> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<T> for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#
159 where
160 T: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<i64>,
161 {
162 fn fromrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`From<T>`]from().(num: T) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`From<T>`] {
163 Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`From<T>`](num.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().())
164 }
165 }
166
167 impl Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'_, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
168 fn encode_by_refrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Encode<'_, Postgres>`]encode_by_ref().(&self, buf: &mut PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
169 buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0..to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]to_be_bytes().());
170
171 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#::Norust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#No#)
172 }
173 }
174
175 impl Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'_, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
176 fn decoderust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Decode<'_, Postgres>`]decode().(value: PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#<'_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Decode<'_, Postgres>`], BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
177 match value.formatrust-analyzer cargo sqlx-postgres 0.8.3 value/impl#[`PgValueRef<'r>`]format().() {
178 PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#::Binaryrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#Binary# => {
179 let cents = BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#::read_i64rust-analyzer cargo byteorder 1.5.0 ByteOrder#read_i64().(value.as_bytesrust-analyzer cargo sqlx-postgres 0.8.3 value/impl#[`PgValueRef<'r>`]as_bytes().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
180
181 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(cents))
182 }
183 PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#::Textrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#Text# => {
184 let error = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
185 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#,
186 "Reading a `MONEY` value in text format is not supported.",
187 );
188
189 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(error))
190 }
191 }
192 }
193 }
194
195 impl Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#> for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
196 type Outputrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Add<Self>`]Output# = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#;
197
198 /// Adds two monetary values.
199 ///
200 /// # Panics
201 /// Panics if overflowing the `i64::MAX`.
202 fn addrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Add<Self>`]add().(self, rhs: PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Add<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output# {
203 self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.
204 .checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]checked_add().(rhs.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.)
205 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#)
206 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("overflow adding money amounts")
207 }
208 }
209
210 impl AddAssignrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#<PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#> for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
211 /// An assigning add for two monetary values.
212 ///
213 /// # Panics
214 /// Panics if overflowing the `i64::MAX`.
215 fn add_assignrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`AddAssign<Self>`]add_assign().(&mut self, rhs: PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#) {
216 self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0. = self
217 .0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.
218 .checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]checked_add().(rhs.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.)
219 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("overflow adding money amounts")
220 }
221 }
222
223 impl Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#> for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
224 type Outputrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Sub<Self>`]Output# = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#;
225
226 /// Subtracts two monetary values.
227 ///
228 /// # Panics
229 /// Panics if underflowing the `i64::MIN`.
230 fn subrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Sub<Self>`]sub().(self, rhs: PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Sub<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output# {
231 self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.
232 .checked_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]checked_sub().(rhs.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.)
233 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#)
234 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("overflow subtracting money amounts")
235 }
236 }
237
238 impl SubAssignrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/SubAssign#<PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#> for PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney# {
239 /// An assigning subtract for two monetary values.
240 ///
241 /// # Panics
242 /// Panics if underflowing the `i64::MIN`.
243 fn sub_assignrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`SubAssign<Self>`]sub_assign().(&mut self, rhs: PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#) {
244 self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0. = self
245 .0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.
246 .checked_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]checked_sub().(rhs.0rust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#0.)
247 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("overflow subtracting money amounts")
248 }
249 }
250
251 #[cfg(test)]
252 mod testsrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/ {
253 use superrust-analyzer cargo sqlx-postgres 0.8.3 types/money/::PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#;
254
255 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
256 fn adding_worksrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/adding_works().() {
257 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(3), PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(1) +rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Add<Self>`]add(). PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(2))
258 }
259
260 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
261 fn add_assign_worksrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/add_assign_works().() {
262 let mut money = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(1);
263 money rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`AddAssign<Self>`]add_assign(). rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`AddAssign<Self>`]add_assign().PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(2);
264
265 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(3), money);
266 }
267
268 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
269 fn subtracting_worksrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/subtracting_works().() {
270 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(4), PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(5) -rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Sub<Self>`]sub(). PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(1))
271 }
272
273 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
274 fn sub_assign_worksrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/sub_assign_works().() {
275 let mut money = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(1);
276 money rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`SubAssign<Self>`]sub_assign().PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(2);
277
278 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().1), money);
279 }
280
281 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
282 fn default_valuerust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/default_value().() {
283 let money = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#::defaultrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][Default]default().();
284
285 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(money, PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(0));
286 }
287
288 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
289 #[should_panic]
290 fn add_overflow_panicsrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/add_overflow_panics().() {
291 let _ = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(i64::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]MAX.) rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Add<Self>`]add().+rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Add<Self>`]add(). rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Add<Self>`]add().PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(1);
292 }
293
294 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
295 #[should_panic]
296 fn add_assign_overflow_panicsrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/add_assign_overflow_panics().() {
297 let mut money = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(i64::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]MAX.);
298 money rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`AddAssign<Self>`]add_assign(). rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`AddAssign<Self>`]add_assign().PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(1);
299 }
300
301 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
302 #[should_panic]
303 fn sub_overflow_panicsrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/sub_overflow_panics().() {
304 let _ = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(i64::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]MIN.) rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Sub<Self>`]sub().-rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Sub<Self>`]sub(). rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`Sub<Self>`]sub().PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(1);
305 }
306
307 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
308 #[should_panic]
309 fn sub_assign_overflow_panicsrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/sub_assign_overflow_panics().() {
310 let mut money = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(i64::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]MIN.);
311 money rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney][`SubAssign<Self>`]sub_assign().PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(1);
312 }
313
314 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
315 #[cfg(feature = "bigdecimal")]
316 fn conversion_to_bigdecimal_worksrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/conversion_to_bigdecimal_works().() {
317 let money = PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(12345);
318
319 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
320 bigdecimalrust-analyzer cargo bigdecimal 0.4.10 crate/::BigDecimalrust-analyzer cargo bigdecimal 0.4.10 BigDecimal#::newrust-analyzer cargo bigdecimal 0.4.10 impl#[BigDecimal]new().(num_bigintrust-analyzer cargo num-bigint 0.4.8 crate/::BigIntrust-analyzer cargo num-bigint 0.4.8 bigint/BigInt#::fromrust-analyzer cargo num-bigint 0.4.8 bigint/convert/impl#[BigInt][`From<i32>`]from().(12345), 2),
321 money.to_bigdecimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]to_bigdecimal().(2)
322 );
323 }
324
325 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
326 #[cfg(feature = "rust_decimal")]
327 fn conversion_to_decimal_worksrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/conversion_to_decimal_works().() {
328 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
329 rust_decimalrust-analyzer cargo rust_decimal 1.42.1 crate/::Decimalrust-analyzer cargo rust_decimal 1.42.1 decimal/Decimal#::newrust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]new().(12345, 2),
330 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(12345).to_decimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]to_decimal().(2)
331 );
332 }
333
334 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
335 #[cfg(feature = "rust_decimal")]
336 fn conversion_from_decimal_worksrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/conversion_from_decimal_works().() {
337 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
338 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(12345),
339 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#::from_decimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]from_decimal().(rust_decimalrust-analyzer cargo rust_decimal 1.42.1 crate/::Decimalrust-analyzer cargo rust_decimal 1.42.1 decimal/Decimal#::newrust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]new().(12345, 2), 2)
340 );
341
342 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
343 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(12345),
344 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#::from_decimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]from_decimal().(rust_decimalrust-analyzer cargo rust_decimal 1.42.1 crate/::Decimalrust-analyzer cargo rust_decimal 1.42.1 decimal/Decimal#::newrust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]new().(123450, 3), 2)
345 );
346
347 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
348 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().12345),
349 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#::from_decimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]from_decimal().(rust_decimalrust-analyzer cargo rust_decimal 1.42.1 crate/::Decimalrust-analyzer cargo rust_decimal 1.42.1 decimal/Decimal#::newrust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]new().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().123450, 3), 2)
350 );
351
352 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
353 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().12300),
354 PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#::from_decimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]from_decimal().(rust_decimalrust-analyzer cargo rust_decimal 1.42.1 crate/::Decimalrust-analyzer cargo rust_decimal 1.42.1 decimal/Decimal#::newrust-analyzer cargo rust_decimal 1.42.1 decimal/impl#[Decimal]new().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().123, 0), 2)
355 );
356 }
357
358 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
359 #[cfg(feature = "bigdecimal")]
360 fn conversion_from_bigdecimal_worksrust-analyzer cargo sqlx-postgres 0.8.3 types/money/tests/conversion_from_bigdecimal_works().() {
361 let dec = bigdecimalrust-analyzer cargo bigdecimal 0.4.10 crate/::BigDecimalrust-analyzer cargo bigdecimal 0.4.10 BigDecimal#::newrust-analyzer cargo bigdecimal 0.4.10 impl#[BigDecimal]new().(num_bigintrust-analyzer cargo num-bigint 0.4.8 crate/::BigIntrust-analyzer cargo num-bigint 0.4.8 bigint/BigInt#::fromrust-analyzer cargo num-bigint 0.4.8 bigint/convert/impl#[BigInt][`From<i32>`]from().(12345), 2);
362
363 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#(12345), PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#::from_bigdecimalrust-analyzer cargo sqlx-postgres 0.8.3 types/money/impl#[PgMoney]from_bigdecimal().(dec, 2).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
364 }
365 }