Skip to content

Commit 4aa8f20

Browse files
committed
update decimal.rs
1 parent 67de9e9 commit 4aa8f20

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
members = ["rbdc-pool-fast","rbdc-sqlite","rbdc-mysql","rbdc-pg","rbdc-mssql", "example"]
33
[package]
44
name = "rbdc"
5-
version = "4.5.41"
5+
version = "4.5.42"
66
edition = "2021"
77
description = "The Rust SQL Toolkit and ORM Library. An async, pure Rust SQL crate featuring compile-time Dynamic SQL"
88
readme = "Readme.md"

src/types/decimal.rs

+91-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
use crate::Error;
24
use bigdecimal::{BigDecimal, FromPrimitive};
35
use rbs::Value;
@@ -15,25 +17,43 @@ impl Decimal {
1517
pub fn new(arg: &str) -> Result<Self, Error> {
1618
Decimal::from_str(arg)
1719
}
18-
20+
#[inline]
1921
pub fn from_f64(arg: f64) -> Option<Decimal> {
2022
match BigDecimal::from_f64(arg) {
21-
None => { None }
22-
Some(v) => {
23-
Some(Decimal::from(v))
24-
}
23+
None => None,
24+
Some(v) => Some(Decimal::from(v)),
2525
}
2626
}
27-
27+
28+
#[inline]
2829
pub fn from_f32(arg: f32) -> Option<Decimal> {
2930
match BigDecimal::from_f32(arg) {
30-
None => { None }
31-
Some(v) => {
32-
Some(Decimal::from(v))
33-
}
31+
None => None,
32+
Some(v) => Some(Decimal::from(v)),
3433
}
3534
}
3635

36+
#[inline]
37+
fn from_i64(n: i64) -> Option<Self> {
38+
Some(Decimal::from(n))
39+
}
40+
41+
#[inline]
42+
fn from_u64(n: u64) -> Option<Self> {
43+
Some(Decimal::from(n))
44+
}
45+
46+
#[inline]
47+
fn from_i128(n: i128) -> Option<Self> {
48+
Some(Decimal::from(n))
49+
}
50+
51+
#[inline]
52+
fn from_u128(n: u128) -> Option<Self> {
53+
Some(Decimal::from(n))
54+
}
55+
56+
3757
///Return a new Decimal object equivalent to self,
3858
/// with internal scaling set to the number specified.
3959
/// If the new_scale is lower than the current value (indicating a larger power of 10),
@@ -94,7 +114,6 @@ impl From<Decimal> for Value {
94114
}
95115
}
96116

97-
98117
impl From<i32> for Decimal {
99118
fn from(arg: i32) -> Self {
100119
Self::from(BigDecimal::from(arg))
@@ -119,15 +138,49 @@ impl From<u64> for Decimal {
119138
}
120139
}
121140

141+
impl From<i128> for Decimal {
142+
fn from(arg: i128) -> Self {
143+
Self::from(BigDecimal::from(arg))
144+
}
145+
}
146+
147+
148+
impl From<u128> for Decimal {
149+
fn from(arg: u128) -> Self {
150+
Self::from(BigDecimal::from(arg))
151+
}
152+
}
153+
154+
impl TryFrom<f32> for Decimal {
155+
type Error = Error;
156+
157+
fn try_from(value: f32) -> Result<Self, Error> {
158+
Ok(Self(
159+
BigDecimal::try_from(value).map_err(|e| Error::from(e.to_string()))?,
160+
))
161+
}
162+
}
163+
164+
impl TryFrom<f64> for Decimal {
165+
type Error = Error;
166+
167+
fn try_from(value: f64) -> Result<Self, Error> {
168+
Ok(Self(
169+
BigDecimal::try_from(value).map_err(|e| Error::from(e.to_string()))?,
170+
))
171+
}
172+
}
173+
122174
impl FromStr for Decimal {
123175
type Err = Error;
124176

125177
fn from_str(s: &str) -> Result<Self, Self::Err> {
126-
Ok(Decimal(BigDecimal::from_str(&s).map_err(|e| Error::from(e.to_string()))?))
178+
Ok(Decimal(
179+
BigDecimal::from_str(&s).map_err(|e| Error::from(e.to_string()))?,
180+
))
127181
}
128182
}
129183

130-
131184
impl Deref for Decimal {
132185
type Target = BigDecimal;
133186

@@ -284,20 +337,42 @@ mod test {
284337
let v1 = Decimal::new("1.123456").unwrap();
285338
let v = v1.with_scale(2);
286339
println!("{}", v.to_string());
287-
assert_eq!(v.to_string(),"1.12");
340+
assert_eq!(v.to_string(), "1.12");
288341
}
289342

290343
#[test]
291344
fn test_with_prec() {
292345
let v1 = Decimal::new("1.123456").unwrap();
293346
let v = v1.with_prec(2);
294347
println!("{}", v.to_string());
295-
assert_eq!(v.to_string(),"1.1");
348+
assert_eq!(v.to_string(), "1.1");
296349
}
297350

298351
#[test]
299352
fn test_parse() {
300353
let v1 = "1.123456".parse::<Decimal>().unwrap();
301-
assert_eq!(v1.to_string(),"1.123456");
354+
assert_eq!(v1.to_string(), "1.123456");
355+
}
356+
357+
#[test]
358+
fn test_from() {
359+
let v = Decimal::from_i64(1);
360+
assert_eq!(v, Some(Decimal::from(1)));
361+
let v = Decimal::from_u64(1);
362+
assert_eq!(v, Some(Decimal::from(1)));
363+
let v = Decimal::from_i128(1);
364+
assert_eq!(v, Some(Decimal::from(1)));
365+
let v = Decimal::from_u128(1);
366+
assert_eq!(v, Some(Decimal::from(1)));
367+
}
368+
369+
#[test]
370+
fn test_try_from_f64() {
371+
let f=1.1;
372+
let v = Decimal::from_f64(f);
373+
println!("{:?}",v);
374+
if let Some(v)=v{
375+
println!("{}",v.to_string());
376+
}
302377
}
303378
}

0 commit comments

Comments
 (0)