Use to_i64 for clarity

This commit is contained in:
Niels Mündler 2023-12-18 15:18:05 +01:00 committed by Lucas
parent d06f2f6008
commit b6acdde552
1 changed files with 4 additions and 2 deletions

View File

@ -1,7 +1,7 @@
use std::{collections::VecDeque, mem::size_of, ops::Deref, rc::Rc}; use std::{collections::VecDeque, mem::size_of, ops::Deref, rc::Rc};
use num_bigint::BigInt; use num_bigint::BigInt;
use num_traits::Signed; use num_traits::{Signed, ToPrimitive};
use pallas_primitives::babbage::{self as pallas, PlutusData}; use pallas_primitives::babbage::{self as pallas, PlutusData};
use crate::{ use crate::{
@ -403,13 +403,15 @@ pub fn from_pallas_bigint(n: &pallas::BigInt) -> BigInt {
} }
pub fn to_pallas_bigint(n: &BigInt) -> pallas::BigInt { pub fn to_pallas_bigint(n: &BigInt) -> pallas::BigInt {
if let Ok(i) = <&BigInt as TryInto<i64>>::try_into(n) { if let Some(i) = n.to_i64() {
let pallas_int: pallas_codec::utils::Int = i.into(); let pallas_int: pallas_codec::utils::Int = i.into();
pallas::BigInt::Int(pallas_int) pallas::BigInt::Int(pallas_int)
} else if n.is_positive() { } else if n.is_positive() {
let (_, bytes) = n.to_bytes_be(); let (_, bytes) = n.to_bytes_be();
pallas::BigInt::BigUInt(bytes.into()) pallas::BigInt::BigUInt(bytes.into())
} else { } else {
// Note that this would break if n == 0
// BUT n == 0 always fits into 64bits and hence would end up in the first branch.
let n: BigInt = n + 1; let n: BigInt = n + 1;
let (_, bytes) = n.to_bytes_be(); let (_, bytes) = n.to_bytes_be();
pallas::BigInt::BigNInt(bytes.into()) pallas::BigInt::BigNInt(bytes.into())