From 43a6ff7f4c6b112e3360d658ef58a6aaba95ff1c Mon Sep 17 00:00:00 2001 From: 0xPlish Date: Tue, 11 Mar 2025 21:47:00 -0700 Subject: [PATCH 1/2] implement TryFrom for FixValue variants, checking exponent before conversion --- Cargo.toml | 2 +- flake.nix | 1 + src/fix_value.rs | 24 +++++++++++++++++++----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cdd5d4e..9632442 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ name = "fix" idl-build = ["anchor-lang/idl-build"] [dependencies] -anchor-lang = "0.30" +anchor-lang = "0.30.1" anyhow = "1.0.82" muldiv = "1.0.1" num-traits = "0.2.17" diff --git a/flake.nix b/flake.nix index f5bf4a0..9a3326e 100644 --- a/flake.nix +++ b/flake.nix @@ -26,6 +26,7 @@ openssl pkg-config rust-bin.stable.latest.complete + rust-analyzer ]; }; }; diff --git a/src/fix_value.rs b/src/fix_value.rs index e7969a2..05f40e3 100644 --- a/src/fix_value.rs +++ b/src/fix_value.rs @@ -1,6 +1,8 @@ use crate::typenum::{Integer, U10}; use crate::Fix; -use anchor_lang::prelude::{borsh, AnchorDeserialize, AnchorSerialize, InitSpace}; + +use anchor_lang::error::ErrorCode::InvalidNumericConversion; +use anchor_lang::prelude::{borsh, AnchorDeserialize, AnchorSerialize, InitSpace, Result}; use paste::paste; macro_rules! impl_fix_value { @@ -33,13 +35,18 @@ macro_rules! impl_fix_value { } } - impl From<[<$sign FixValue $bits>]> for Fix + impl TryFrom<[<$sign FixValue $bits>]> for Fix where Bits: From<[<$sign:lower $bits>]>, Exp: Integer, { - fn from(value: [<$sign FixValue $bits>]) -> Fix { - Fix::new(value.bits.into()) + type Error = anchor_lang::error::Error; + fn try_from(value: [<$sign FixValue $bits>]) -> Result> { + if value.exp == Exp::to_i8() { + Ok(Fix::new(value.bits.into())) + } else { + Err(InvalidNumericConversion.into()) + } } } } @@ -71,7 +78,7 @@ mod tests { fn []() -> Result<()> { let start = Kilo::new([<69 $sign:lower $bits>]); let there: [<$sign FixValue $bits>] = start.into(); - let back: Kilo<[<$sign:lower $bits>]> = there.into(); + let back: Kilo<[<$sign:lower $bits>]> = there.try_into()?; assert_eq!(there, [<$sign FixValue $bits>]::new(69, 3)); Ok(assert_eq!(start, back)) } @@ -83,6 +90,13 @@ mod tests { let back = AnchorDeserialize::deserialize(&mut bytes.as_slice())?; Ok(assert_eq!(start, back)) } + + #[test] + fn []() -> Result<()> { + let pow11 = [<$sign FixValue $bits>]::new(42, -11); + let wrong = TryInto::]>>::try_into(pow11); + Ok(assert_eq!(Err(InvalidNumericConversion.into()), wrong)) + } } }; } From 7be0f9cfd9bd935e587b36995678f6c864f5e959 Mon Sep 17 00:00:00 2001 From: 0xPlish Date: Wed, 12 Mar 2025 13:31:04 -0700 Subject: [PATCH 2/2] Bump package version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9632442..a5201d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hylo-fix" -version = "0.2.0" +version = "0.3.0" edition = "2021" description = "Fixed-point number types with Solana Anchor support" authors = ["0xPlish ", "Curtis McEnroe "]