paritytech / paritytech/parity-common
[uint] conversion between uint types
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 311
- Forks
- 245
- PR merge metrics
- No merged PRs in 30d
Description
Is there an existing issue?
- I have searched the existing issues. There is no such request yet, but here's a request with the most similar vibe: #691
Experiencing problems? Have you tried our Stack Exchange first?
- This is not a support question.
Question:
What is recommended way to convert one type of uint to another?
Background:
I've developed a high-level lib for creating custom DeFi-oriented types that are implemented in pure Rust, where uint types serve as underlying types.
Details:
In certain scenarios, I need to expand the type to prevent overflow in intermediate calculations. For instance, there are cases where I must extend the type from U256 to U512. I'm exploring the best approach to convert between different uint types.
Based on my observations, it seems that try_from is only implemented for primitive types. One option is to manually rewrite the u64 values, but I believe there might be a more elegant solution. Perhaps it's worth considering the addition of a try_from implementation for converting between types at the uint library level.
As an illustration, consider the following two types:
construct_uint! {
pub struct U256(4);
}
construct_uint! {
pub struct U512(8);
}
In this context, creating a U512 type based on a U256 instance could look like this:
U512::try_from(U256::MAX)
Are you willing to help with this request?
I am willing to create a proposal for implementation if the feature is deemed useful.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the construct_uint! entry point and the existing conversion behavior for primitive types. Compare the U256 and U512 examples, then determine the intended success and overflow behavior for conversions between uint types; the work is done when that behavior is implemented and covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100