alloy-rs / core

High-performance, well-tested & documented core libraries for Ethereum, in Rust
https://alloy.rs
Apache License 2.0
764 stars 139 forks source link

[Info] Cannot use `U256::from(B256)` due to `ruint` limitation #554

Open greged93 opened 6 months ago

greged93 commented 6 months ago

Component

primitives

What version of Alloy are you on?

v0.6.4

Operating System

macOS (Apple Silicon)

Describe the bug

Despite the implementation of From<FixedBytes<N> for Uint<N*8>, it is currently not possible to run the below code:

use alloy_primitives::{FixedBytes, Uint};

fn main() {
    let fb = FixedBytes::<32>::new([1u8; 32]);
    let u = Uint::<256, 4>::from(fb);
    dbg!(u);
}

This is due to the line Uint::<256, 4>::from(fb), resolving the from to https://github.com/recmo/uint/blob/main/src/from.rs#L147, which requires the provided type to implement TryFrom and not From, causing the below error:

error[E0271]: type mismatch resolving `<Uint<256, 4> as TryFrom<FixedBytes<32>>>::Error == ToUintError<Uint<256, 4>>`
   --> src/main.rs:5:34
    |
5   |     let u = Uint::<256, 4>::from(fb);
    |             -------------------- ^^ expected `ToUintError<Uint<256, 4>>`, found `Infallible`
    |             |
    |             required by a bound introduced by this call
    |
    = note: expected enum `ToUintError<Uint<256, 4>>`
               found enum `Infallible`
    = note: required for `Uint<256, 4>` to implement `UintTryFrom<FixedBytes<32>>`
note: required by a bound in `ruint::from::<impl Uint<BITS, LIMBS>>::from`
DaniPopes commented 6 months ago

Yeah this is a limitation of ruint, for now you'll have to either do <U256 as From<_>>::from or use let u: U256 = bytes.into();.

I'll keep this open for visibility.