diff --git a/stylus-sdk/src/abi/ints.rs b/stylus-sdk/src/abi/ints.rs index e70d432..f776176 100644 --- a/stylus-sdk/src/abi/ints.rs +++ b/stylus-sdk/src/abi/ints.rs @@ -1,7 +1,7 @@ // Copyright 2023-2024, Offchain Labs, Inc. // For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md -//! Support for generic integer types, found in [alloy_primitives]. +//! Support for generic integer types found in [alloy_primitives]. use alloy_primitives::{ruint::UintTryFrom, Signed, Uint}; use alloy_sol_types::{ @@ -205,21 +205,35 @@ impl From 0 { - overflow |= limbs[TL - 1] > Signed::::MASK; - limbs[TL - 1] &= Signed::::MASK; + overflow |= limbs[TL - 1] > mask(TB); + limbs[TL - 1] &= mask(TB); + } + if overflow { + // This should not happen. + panic!("overflow in int conversion"); } - */ Converted(Signed::from_limbs(limbs)) } } } +const fn mask(bits: usize) -> u64 { + if bits == 0 { + return 0; + } + let bits = bits % 64; + if bits == 0 { + u64::MAX + } else { + (1 << bits) - 1 + } +} + impl From> for Converted where IntBitCount: SupportedInt,