paritytech / substrate

Substrate: The platform for blockchain innovators
Apache License 2.0
8.39k stars 2.65k forks source link

Why doesn't `U256` implement `AtLeast32BitUnsigned`? #10847

Closed Cupnfish closed 2 years ago

Cupnfish commented 2 years ago

It's the same as the title says. I don't really understand why U256 doesn't implement this trait, is there any reason?

bkchr commented 2 years ago

The U256 is some Substrate external type. It comes from a Parity crate, but not directly from this repo.

error[E0277]: the trait bound `primitive_types::U256: num_traits::Unsigned` is not satisfied
   --> primitives/arithmetic/src/traits.rs:318:10
    |
318 |         test::<U256>();
    |                ^^^^ the trait `num_traits::Unsigned` is not implemented for `primitive_types::U256`
    |
note: required because of the requirements on the impl of `AtLeast32BitUnsigned` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:158:34
    |
158 | impl<T: AtLeast32Bit + Unsigned> AtLeast32BitUnsigned for T {}
    |                                  ^^^^^^^^^^^^^^^^^^^^     ^
note: required by a bound in `test`
   --> primitives/arithmetic/src/traits.rs:314:13
    |
314 |     fn test<T: AtLeast32BitUnsigned>() {}
    |                ^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

error[E0277]: the trait bound `primitive_types::U256: CheckedShl` is not satisfied
   --> primitives/arithmetic/src/traits.rs:318:10
    |
318 |         test::<U256>();
    |                ^^^^ the trait `CheckedShl` is not implemented for `primitive_types::U256`
    |
note: required because of the requirements on the impl of `BaseArithmetic` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:142:4
    |
142 |     > BaseArithmetic for T
    |       ^^^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32Bit` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:153:49
    |
153 | impl<T: BaseArithmetic + From<u16> + From<u32>> AtLeast32Bit for T {}
    |                                                 ^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32BitUnsigned` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:158:34
    |
158 | impl<T: AtLeast32Bit + Unsigned> AtLeast32BitUnsigned for T {}
    |                                  ^^^^^^^^^^^^^^^^^^^^     ^
note: required by a bound in `test`
   --> primitives/arithmetic/src/traits.rs:314:13
    |
314 |     fn test<T: AtLeast32BitUnsigned>() {}
    |                ^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

error[E0277]: the trait bound `primitive_types::U256: CheckedShr` is not satisfied
   --> primitives/arithmetic/src/traits.rs:318:10
    |
318 |         test::<U256>();
    |                ^^^^ the trait `CheckedShr` is not implemented for `primitive_types::U256`
    |
note: required because of the requirements on the impl of `BaseArithmetic` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:142:4
    |
142 |     > BaseArithmetic for T
    |       ^^^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32Bit` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:153:49
    |
153 | impl<T: BaseArithmetic + From<u16> + From<u32>> AtLeast32Bit for T {}
    |                                                 ^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32BitUnsigned` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:158:34
    |
158 | impl<T: AtLeast32Bit + Unsigned> AtLeast32BitUnsigned for T {}
    |                                  ^^^^^^^^^^^^^^^^^^^^     ^
note: required by a bound in `test`
   --> primitives/arithmetic/src/traits.rs:314:13
    |
314 |     fn test<T: AtLeast32BitUnsigned>() {}
    |                ^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

error[E0277]: the trait bound `primitive_types::U256: CheckedRem` is not satisfied
   --> primitives/arithmetic/src/traits.rs:318:10
    |
318 |         test::<U256>();
    |                ^^^^ the trait `CheckedRem` is not implemented for `primitive_types::U256`
    |
note: required because of the requirements on the impl of `BaseArithmetic` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:142:4
    |
142 |     > BaseArithmetic for T
    |       ^^^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32Bit` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:153:49
    |
153 | impl<T: BaseArithmetic + From<u16> + From<u32>> AtLeast32Bit for T {}
    |                                                 ^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32BitUnsigned` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:158:34
    |
158 | impl<T: AtLeast32Bit + Unsigned> AtLeast32BitUnsigned for T {}
    |                                  ^^^^^^^^^^^^^^^^^^^^     ^
note: required by a bound in `test`
   --> primitives/arithmetic/src/traits.rs:314:13
    |
314 |     fn test<T: AtLeast32BitUnsigned>() {}
    |                ^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

error[E0277]: the trait bound `parity_scale_codec::Compact<primitive_types::U256>: Decode` is not satisfied
   --> primitives/arithmetic/src/traits.rs:318:3
    |
318 |         test::<U256>();
    |         ^^^^^^^^^^^^ the trait `Decode` is not implemented for `parity_scale_codec::Compact<primitive_types::U256>`
    |
    = help: the following implementations were found:
              <parity_scale_codec::Compact<()> as Decode>
              <parity_scale_codec::Compact<T> as Decode>
              <parity_scale_codec::Compact<u128> as Decode>
              <parity_scale_codec::Compact<u16> as Decode>
            and 3 others
    = note: required because of the requirements on the impl of `HasCompact` for `primitive_types::U256`
note: required because of the requirements on the impl of `BaseArithmetic` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:142:4
    |
142 |     > BaseArithmetic for T
    |       ^^^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32Bit` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:153:49
    |
153 | impl<T: BaseArithmetic + From<u16> + From<u32>> AtLeast32Bit for T {}
    |                                                 ^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32BitUnsigned` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:158:34
    |
158 | impl<T: AtLeast32Bit + Unsigned> AtLeast32BitUnsigned for T {}
    |                                  ^^^^^^^^^^^^^^^^^^^^     ^
note: required by a bound in `test`
   --> primitives/arithmetic/src/traits.rs:314:13
    |
314 |     fn test<T: AtLeast32BitUnsigned>() {}
    |                ^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

error[E0277]: the trait bound `primitive_types::U256: PrimInt` is not satisfied
   --> primitives/arithmetic/src/traits.rs:318:10
    |
318 |         test::<U256>();
    |                ^^^^ the trait `PrimInt` is not implemented for `primitive_types::U256`
    |
    = note: required because of the requirements on the impl of `IntegerSquareRoot` for `primitive_types::U256`
note: required because of the requirements on the impl of `BaseArithmetic` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:142:4
    |
142 |     > BaseArithmetic for T
    |       ^^^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32Bit` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:153:49
    |
153 | impl<T: BaseArithmetic + From<u16> + From<u32>> AtLeast32Bit for T {}
    |                                                 ^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32BitUnsigned` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:158:34
    |
158 | impl<T: AtLeast32Bit + Unsigned> AtLeast32BitUnsigned for T {}
    |                                  ^^^^^^^^^^^^^^^^^^^^     ^
note: required by a bound in `test`
   --> primitives/arithmetic/src/traits.rs:314:13
    |
314 |     fn test<T: AtLeast32BitUnsigned>() {}
    |                ^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

error[E0277]: the trait bound `scale::compact::CompactRef<'_, primitive_types::U256>: Encode` is not satisfied
   --> primitives/arithmetic/src/traits.rs:318:3
    |
318 |         test::<U256>();
    |         ^^^^^^^^^^^^ the trait `Encode` is not implemented for `scale::compact::CompactRef<'_, primitive_types::U256>`
    |
    = help: the following implementations were found:
              <scale::compact::CompactRef<'a, ()> as Encode>
              <scale::compact::CompactRef<'a, T> as Encode>
              <scale::compact::CompactRef<'a, u128> as Encode>
              <scale::compact::CompactRef<'a, u16> as Encode>
            and 3 others
    = note: required because of the requirements on the impl of `for<'a> EncodeAsRef<'a, primitive_types::U256>` for `parity_scale_codec::Compact<primitive_types::U256>`
    = note: required because of the requirements on the impl of `HasCompact` for `primitive_types::U256`
note: required because of the requirements on the impl of `BaseArithmetic` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:142:4
    |
142 |     > BaseArithmetic for T
    |       ^^^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32Bit` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:153:49
    |
153 | impl<T: BaseArithmetic + From<u16> + From<u32>> AtLeast32Bit for T {}
    |                                                 ^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32BitUnsigned` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:158:34
    |
158 | impl<T: AtLeast32Bit + Unsigned> AtLeast32BitUnsigned for T {}
    |                                  ^^^^^^^^^^^^^^^^^^^^     ^
note: required by a bound in `test`
   --> primitives/arithmetic/src/traits.rs:314:13
    |
314 |     fn test<T: AtLeast32BitUnsigned>() {}
    |                ^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

error[E0277]: the trait bound `primitive_types::U256: From<parity_scale_codec::Compact<primitive_types::U256>>` is not satisfied
   --> primitives/arithmetic/src/traits.rs:318:10
    |
318 |         test::<U256>();
    |                ^^^^ the trait `From<parity_scale_codec::Compact<primitive_types::U256>>` is not implemented for `primitive_types::U256`
    |
    = help: the following implementations were found:
              <primitive_types::U256 as From<&'a [u8; 32]>>
              <primitive_types::U256 as From<&'a [u8]>>
              <primitive_types::U256 as From<&'a primitive_types::U256>>
              <primitive_types::U256 as From<&'static str>>
            and 14 others
    = note: required because of the requirements on the impl of `Into<primitive_types::U256>` for `parity_scale_codec::Compact<primitive_types::U256>`
    = note: required because of the requirements on the impl of `HasCompact` for `primitive_types::U256`
note: required because of the requirements on the impl of `BaseArithmetic` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:142:4
    |
142 |     > BaseArithmetic for T
    |       ^^^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32Bit` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:153:49
    |
153 | impl<T: BaseArithmetic + From<u16> + From<u32>> AtLeast32Bit for T {}
    |                                                 ^^^^^^^^^^^^     ^
note: required because of the requirements on the impl of `AtLeast32BitUnsigned` for `primitive_types::U256`
   --> primitives/arithmetic/src/traits.rs:158:34
    |
158 | impl<T: AtLeast32Bit + Unsigned> AtLeast32BitUnsigned for T {}
    |                                  ^^^^^^^^^^^^^^^^^^^^     ^
note: required by a bound in `test`
   --> primitives/arithmetic/src/traits.rs:314:13
    |
314 |     fn test<T: AtLeast32BitUnsigned>() {}
    |                ^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

Here you see all the missing traits to have this implemented automatically. (We can not implement the trait manually, because of the missing specialization Rust).

If you want that the type implements AtLeast32BitUnsigned you need to implemented the missing trait for U256 in the parity-common repo.