From 9a991d4f632e778d20efa04bdd781b1b4bca418f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 5 Nov 2023 19:50:20 +0100 Subject: [PATCH] chore: clippy --- crates/primitives/src/transaction/tx_value.rs | 89 +++++++------------ 1 file changed, 30 insertions(+), 59 deletions(-) diff --git a/crates/primitives/src/transaction/tx_value.rs b/crates/primitives/src/transaction/tx_value.rs index cd1f3caa35480..db949f1d08e71 100644 --- a/crates/primitives/src/transaction/tx_value.rs +++ b/crates/primitives/src/transaction/tx_value.rs @@ -1,20 +1,10 @@ -#[allow(unused_imports)] -// suppress warning for UIntTryTo, which is required only when optimism feature is disabled -use crate::{ - ruint::{ToUintError, UintTryFrom, UintTryTo}, - U256, -}; +use crate::{ruint::UintTryFrom, U256}; use alloy_rlp::{Decodable, Encodable, Error}; use reth_codecs::{add_arbitrary_tests, Compact}; use serde::{Deserialize, Serialize}; -#[cfg(any(test, feature = "arbitrary"))] -use proptest::{ - arbitrary::ParamsFor, - strategy::{BoxedStrategy, Strategy}, -}; - /// TxValue is the type of the `value` field in the various Ethereum transactions structs. +/// /// While the field is 256 bits, for many chains it's not possible for the field to use /// this full precision, hence we use a wrapper type to allow for overriding of encoding. #[add_arbitrary_tests(compact, rlp)] @@ -22,7 +12,7 @@ use proptest::{ pub struct TxValue(U256); impl From for U256 { - /// unwrap Value to U256 + #[inline] fn from(value: TxValue) -> U256 { value.0 } @@ -30,50 +20,31 @@ impl From for U256 { impl From for TxValue where - Self: UintTryFrom, + U256: UintTryFrom, { - /// construct a Value from misc. other uint types - fn from(value: T) -> Self { - match Self::uint_try_from(value) { - Ok(n) => n, - Err(e) => panic!("Uint conversion error: {e}"), - } - } -} - -impl UintTryFrom for TxValue { - #[inline] - fn uint_try_from(value: U256) -> Result> { - Ok(Self(value)) - } -} - -impl UintTryFrom for TxValue { - #[inline] - fn uint_try_from(value: u128) -> Result> { - Ok(Self(U256::from(value))) - } -} - -impl UintTryFrom for TxValue { #[inline] - fn uint_try_from(value: u64) -> Result> { - Ok(Self(U256::from(value))) + #[track_caller] + fn from(value: T) -> Self { + Self(U256::uint_try_from(value).unwrap()) } } impl Encodable for TxValue { + #[inline] fn encode(&self, out: &mut dyn bytes::BufMut) { self.0.encode(out) } + + #[inline] fn length(&self) -> usize { self.0.length() } } impl Decodable for TxValue { + #[inline] fn decode(buf: &mut &[u8]) -> Result { - Ok(TxValue(U256::decode(buf)?)) + U256::decode(buf).map(Self) } } @@ -83,7 +54,7 @@ impl Decodable for TxValue { /// This optimization should be disabled for chains such as Optimism, where /// some tx values may require more than 128-bit precision. impl Compact for TxValue { - #[allow(unreachable_code)] + #[inline] fn to_compact(self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]>, @@ -94,14 +65,11 @@ impl Compact for TxValue { } #[cfg(not(feature = "optimism"))] { - // SAFETY: For ethereum mainnet this is safe as the max value is - // 120000000000000000000000000 wei - let i: u128 = self.0.uint_try_to().expect("value could not be converted to u128"); - i.to_compact(buf) + self.0.to::().to_compact(buf) } } - #[allow(unreachable_code)] + #[inline] fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8]) { #[cfg(feature = "optimism")] { @@ -118,35 +86,38 @@ impl Compact for TxValue { #[cfg(any(test, feature = "arbitrary"))] impl<'a> arbitrary::Arbitrary<'a> for TxValue { + #[inline] fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { #[cfg(feature = "optimism")] { - Ok(Self(U256::arbitrary(u)?)) + U256::arbitrary(u).map(Self) } - #[cfg(not(feature = "optimism"))] { - Ok(Self::try_from(u128::arbitrary(u)?).expect("to fit")) + u128::arbitrary(u).map(Self::from) } } } #[cfg(any(test, feature = "arbitrary"))] impl proptest::arbitrary::Arbitrary for TxValue { - type Parameters = ParamsFor<()>; - fn arbitrary_with(_: Self::Parameters) -> Self::Strategy { + #[cfg(feature = "optimism")] + type Strategy = proptest::arbitrary::Mapped; + #[cfg(not(feature = "optimism"))] + type Strategy = proptest::arbitrary::Mapped; + type Parameters = ::Parameters; + + #[inline] + fn arbitrary_with((): Self::Parameters) -> Self::Strategy { + use proptest::strategy::Strategy; + #[cfg(feature = "optimism")] { - proptest::prelude::any::().prop_map(Self).boxed() + proptest::prelude::any::().prop_map(Self) } - #[cfg(not(feature = "optimism"))] { - proptest::prelude::any::() - .prop_map(|num| Self::try_from(num).expect("to fit")) - .boxed() + proptest::prelude::any::().prop_map(Self::from) } } - - type Strategy = BoxedStrategy; }