Skip to content

Commit 0865e6e

Browse files
author
bitfl0wer
committed
Add conversion from Option<PgUInt> to Option<uint>
1 parent 5290e8c commit 0865e6e

10 files changed

Lines changed: 100 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ fn main() {
2424
"Interact with the underlying BigDecimal type directly: {}",
2525
pg_u_64.as_big_decimal()
2626
);
27+
println!("You can also convert an Option<PgUInt> to a Option<uint> easily.")
28+
let somepguint: Option<PgU32> = Some(PgU32::from(123u32));
29+
let someuint: Option<u32> = somepguint.to_option_uint();
30+
assert_eq!(someuint, Some(123u32));
2731
println!("PgUint types can be converted to and from BigDecimals, and are storable in an sqlx::Postgres database.");
2832
println!("If you load a PgUint from a database successfully, you can be sure that it's a valid fixed-size unsigned integer.");
2933
}

sqlx-pg-uint-macros/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlx-pg-uint-macros"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
edition = "2021"
55
license = "MIT"
66
authors = ["bitfl0wer"]
@@ -11,5 +11,5 @@ rust-version = "1.61.0"
1111
proc-macro = true
1212

1313
[dependencies]
14-
quote = "1.0.38"
15-
syn = "2.0.98"
14+
quote = "1.0.40"
15+
syn = "2.0.101"

sqlx-pg-uint-macros/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ pub fn uint_wrapper_derive(input: TokenStream) -> TokenStream {
104104
stringed_num.parse().unwrap()
105105
}
106106

107+
/// Converts `Option<PgUint>` to `Option<[underlying integer type]>`.
108+
pub fn to_option_uint(&self) -> Option<<#name as UIntType>::Uint> {
109+
<Option<#name> as OptionPgUint<#name>>::to_option_uint(&Some(self.clone()))
110+
}
111+
107112
/// Creates a new instance of this type from the associated unsigned integer type
108113
pub fn new(num: <#name as UIntType>::Uint) -> Self {
109114
Self {
@@ -117,6 +122,12 @@ pub fn uint_wrapper_derive(input: TokenStream) -> TokenStream {
117122
}
118123
}
119124

125+
impl OptionPgUint<#name> for Option<#name> where #name: UIntType {
126+
fn to_option_uint(&self) -> Option<<#name as UIntType>::Uint> {
127+
self.clone().map(|v| v.to_uint())
128+
}
129+
}
130+
120131
impl TryFrom<BigDecimal> for #name {
121132
type Error = crate::Error;
122133

sqlx-pg-uint/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[package]
22
name = "sqlx-pg-uint"
3-
version = "0.9.0"
3+
version = "0.10.0"
44
edition = "2021"
55
license = "MIT"
66
authors = ["bitfl0wer"]
77
description = "SQLx compatible types to convert between Rust unsigned integers and the PostgreSQL `NUMERIC`/`DECIMAL` type seamlessly."
88
repository = "https://github.com/bitfl0wer/sqlx-pg-uint"
99
readme = "../README.md"
10-
rust-version = "1.74.1"
10+
rust-version = "1.81.0"
1111

1212
[dependencies]
1313
sqlx = { version = "^0.8", default-features = false, features = [

sqlx-pg-uint/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,8 @@ pub trait UIntType: private::Sealed + Display {
9090
/// The underlying integer type for the `PgUint` type.
9191
type Uint: private::Sealed + FromStr;
9292
}
93+
94+
pub(crate) trait OptionPgUint<T: UIntType> {
95+
/// Convert any `Option<PgUint>` to an `Option<[underlying integer type]>`
96+
fn to_option_uint(&self) -> Option<T::Uint>;
97+
}

sqlx-pg-uint/src/u128.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,19 @@ mod pg_u128_tests {
9696
let pg_u128 = PgU128::try_from(big_decimal.clone());
9797
assert_eq!(pg_u128.unwrap_err(), Error::InvalidValue(big_decimal));
9898
}
99+
100+
#[test]
101+
fn test_option_conversion() {
102+
let somepguint = Some(PgU128::from(123u128));
103+
let someuint = somepguint.to_option_uint();
104+
assert_eq!(someuint, Some(123u128));
105+
106+
let pguint = PgU128::from(123);
107+
let someuint = pguint.to_option_uint();
108+
assert_eq!(someuint, Some(123u128));
109+
110+
let pguint: Option<PgU128> = None;
111+
let someuint = pguint.to_option_uint();
112+
assert_eq!(someuint, None::<u128>);
113+
}
99114
}

sqlx-pg-uint/src/u16.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,19 @@ mod pg_u16_tests {
9696
let pg_u16 = PgU16::try_from(big_decimal.clone());
9797
assert_eq!(pg_u16.unwrap_err(), Error::InvalidValue(big_decimal));
9898
}
99+
100+
#[test]
101+
fn test_option_conversion() {
102+
let somepguint = Some(PgU16::from(123u16));
103+
let someuint = somepguint.to_option_uint();
104+
assert_eq!(someuint, Some(123u16));
105+
106+
let pguint = PgU16::from(123);
107+
let someuint = pguint.to_option_uint();
108+
assert_eq!(someuint, Some(123u16));
109+
110+
let pguint: Option<PgU16> = None;
111+
let someuint = pguint.to_option_uint();
112+
assert_eq!(someuint, None::<u16>);
113+
}
99114
}

sqlx-pg-uint/src/u32.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,19 @@ mod pg_u32_tests {
9595
let pg_u32 = PgU32::try_from(big_decimal.clone());
9696
assert_eq!(pg_u32.unwrap_err(), Error::InvalidValue(big_decimal));
9797
}
98+
99+
#[test]
100+
fn test_option_conversion() {
101+
let somepguint = Some(PgU32::from(123u32));
102+
let someuint = somepguint.to_option_uint();
103+
assert_eq!(someuint, Some(123u32));
104+
105+
let pguint = PgU32::from(123);
106+
let someuint = pguint.to_option_uint();
107+
assert_eq!(someuint, Some(123u32));
108+
109+
let pguint: Option<PgU32> = None;
110+
let someuint = pguint.to_option_uint();
111+
assert_eq!(someuint, None::<u32>);
112+
}
98113
}

sqlx-pg-uint/src/u64.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,19 @@ mod pg_u64_tests {
9595
let pg_u64 = PgU64::try_from(big_decimal.clone());
9696
assert_eq!(pg_u64.unwrap_err(), Error::InvalidValue(big_decimal));
9797
}
98+
99+
#[test]
100+
fn test_option_conversion() {
101+
let somepguint = Some(PgU64::from(123u64));
102+
let someuint = somepguint.to_option_uint();
103+
assert_eq!(someuint, Some(123u64));
104+
105+
let pguint = PgU64::from(123);
106+
let someuint = pguint.to_option_uint();
107+
assert_eq!(someuint, Some(123u64));
108+
109+
let pguint: Option<PgU64> = None;
110+
let someuint = pguint.to_option_uint();
111+
assert_eq!(someuint, None::<u64>);
112+
}
98113
}

sqlx-pg-uint/src/u8.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,19 @@ mod pg_u8_tests {
9696
let pg_u8 = PgU8::try_from(big_decimal.clone());
9797
assert_eq!(pg_u8.unwrap_err(), Error::InvalidValue(big_decimal));
9898
}
99+
100+
#[test]
101+
fn test_option_conversion() {
102+
let somepguint = Some(PgU8::from(123u8));
103+
let someuint = somepguint.to_option_uint();
104+
assert_eq!(someuint, Some(123u8));
105+
106+
let pguint = PgU8::from(123);
107+
let someuint = pguint.to_option_uint();
108+
assert_eq!(someuint, Some(123u8));
109+
110+
let pguint: Option<PgU8> = None;
111+
let someuint = pguint.to_option_uint();
112+
assert_eq!(someuint, None::<u8>);
113+
}
99114
}

0 commit comments

Comments
 (0)