diff --git a/src/curr/hash.rs b/src/curr/hash.rs index 1bc3b840..7605d4ae 100644 --- a/src/curr/hash.rs +++ b/src/curr/hash.rs @@ -4,6 +4,6 @@ mod bytes; #[cfg(feature = "hex")] impl super::Hash { pub fn from_hex(s: &str) -> Result { - Ok(super::Hash(create::hex::padded_hex_from_str(s)?)) + Ok(super::Hash(crate::hex::padded_hex_from_str(s)?)) } } diff --git a/src/curr/hash/bytes.rs b/src/curr/hash/bytes.rs index a1531d09..b007fb3a 100644 --- a/src/curr/hash/bytes.rs +++ b/src/curr/hash/bytes.rs @@ -15,6 +15,7 @@ impl TryFrom for Hash { } } +#[cfg(feature = "alloc")] impl TryFrom for stellar_strkey::Contract { type Error = super::super::Error; fn try_from(value: HashIdPreimage) -> Result { diff --git a/src/curr/num_conversions.rs b/src/curr/num_conversions.rs index 153e6673..743dce8e 100644 --- a/src/curr/num_conversions.rs +++ b/src/curr/num_conversions.rs @@ -21,6 +21,14 @@ impl From<(u128, u128)> for UInt256Parts { } } +impl From for (u128, u128) { + fn from(parts: UInt256Parts) -> Self { + let hi = (u128::from(parts.hi_hi) << 64) | u128::from(parts.hi_lo); + let lo = (u128::from(parts.lo_hi) << 64) | u128::from(parts.lo_lo); + (hi, lo) + } +} + impl From<(i128, i128)> for Int256Parts { fn from((hi, lo): (i128, i128)) -> Self { let Int128Parts { @@ -40,6 +48,14 @@ impl From<(i128, i128)> for Int256Parts { } } +impl From for (i128, i128) { + fn from(parts: Int256Parts) -> Self { + let hi = (i128::from(parts.hi_hi) << 64) | i128::from(parts.hi_lo); + let lo = (i128::from(parts.lo_hi) << 64) | i128::from(parts.lo_lo); + (hi, lo) + } +} + impl From for UInt128Parts { fn from(val: u128) -> Self { let hi = (val >> 64) as u64; @@ -99,14 +115,3 @@ impl FromStr for Int128Parts { Ok(i128::from_str(s).map_err(|_| Self::Err::Invalid)?.into()) } } - -#[cfg(test)] -mod test { - use super::*; - #[test] - fn round_trip_u128() { - let u128_val: u128 = 0x1234567890abcdef1234567890abcdefu128; - let xdr_val: UInt128Parts = u128_val.into(); - assert_eq!(xdr_val.into(), u128_val); - } -} diff --git a/src/next/hash.rs b/src/next/hash.rs index 1bc3b840..7605d4ae 100644 --- a/src/next/hash.rs +++ b/src/next/hash.rs @@ -4,6 +4,6 @@ mod bytes; #[cfg(feature = "hex")] impl super::Hash { pub fn from_hex(s: &str) -> Result { - Ok(super::Hash(create::hex::padded_hex_from_str(s)?)) + Ok(super::Hash(crate::hex::padded_hex_from_str(s)?)) } } diff --git a/src/next/hash/bytes.rs b/src/next/hash/bytes.rs index a1531d09..b007fb3a 100644 --- a/src/next/hash/bytes.rs +++ b/src/next/hash/bytes.rs @@ -15,6 +15,7 @@ impl TryFrom for Hash { } } +#[cfg(feature = "alloc")] impl TryFrom for stellar_strkey::Contract { type Error = super::super::Error; fn try_from(value: HashIdPreimage) -> Result { diff --git a/src/next/num_conversions.rs b/src/next/num_conversions.rs index 153e6673..743dce8e 100644 --- a/src/next/num_conversions.rs +++ b/src/next/num_conversions.rs @@ -21,6 +21,14 @@ impl From<(u128, u128)> for UInt256Parts { } } +impl From for (u128, u128) { + fn from(parts: UInt256Parts) -> Self { + let hi = (u128::from(parts.hi_hi) << 64) | u128::from(parts.hi_lo); + let lo = (u128::from(parts.lo_hi) << 64) | u128::from(parts.lo_lo); + (hi, lo) + } +} + impl From<(i128, i128)> for Int256Parts { fn from((hi, lo): (i128, i128)) -> Self { let Int128Parts { @@ -40,6 +48,14 @@ impl From<(i128, i128)> for Int256Parts { } } +impl From for (i128, i128) { + fn from(parts: Int256Parts) -> Self { + let hi = (i128::from(parts.hi_hi) << 64) | i128::from(parts.hi_lo); + let lo = (i128::from(parts.lo_hi) << 64) | i128::from(parts.lo_lo); + (hi, lo) + } +} + impl From for UInt128Parts { fn from(val: u128) -> Self { let hi = (val >> 64) as u64; @@ -99,14 +115,3 @@ impl FromStr for Int128Parts { Ok(i128::from_str(s).map_err(|_| Self::Err::Invalid)?.into()) } } - -#[cfg(test)] -mod test { - use super::*; - #[test] - fn round_trip_u128() { - let u128_val: u128 = 0x1234567890abcdef1234567890abcdefu128; - let xdr_val: UInt128Parts = u128_val.into(); - assert_eq!(xdr_val.into(), u128_val); - } -} diff --git a/tests/num_conversions.rs b/tests/num_conversions.rs index 0b16c309..0c8291f0 100644 --- a/tests/num_conversions.rs +++ b/tests/num_conversions.rs @@ -6,10 +6,10 @@ #[cfg(feature = "curr")] use stellar_xdr::curr as stellar_xdr; -// #[cfg(feature = "next")] -// use stellar_xdr::next as stellar_xdr; +#[cfg(feature = "next")] +use stellar_xdr::next as stellar_xdr; -use stellar_xdr::{Int128Parts, UInt128Parts}; +use stellar_xdr::{Int128Parts, UInt128Parts, UInt256Parts}; #[test] fn round_trip_u128() { @@ -29,8 +29,16 @@ fn round_trip_i128() { #[test] fn round_trip_u256() { - let u256_val = 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdefu128; - let xdr_val: UInt128Parts = u256_val.into(); - let u256_val2: u128 = xdr_val.into(); - assert_eq!(u256_val, u256_val2); + let (hi, lo) = (0x1234567890abcdefu128, 0x1234567890abcdefu128); + let xdr_val: UInt256Parts = (hi, lo).into(); + let (hi2, lo2): (u128, u128) = xdr_val.into(); + assert_eq!((hi, lo), (hi2, lo2)); +} + +#[test] +fn round_trip_i256() { + let (hi, lo) = (0x1234567890abcdefi128, 0x1234567890abcdefi128); + let xdr_val: Int256Parts = (hi, lo).into(); + let (hi2, lo2): (i128, i128) = xdr_val.into(); + assert_eq!((hi, lo), (hi2, lo2)); }