Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve conversions of types in the std lib #5797

Open
7 of 8 tasks
SwayStar123 opened this issue Mar 28, 2024 · 0 comments · May be fixed by #5800
Open
7 of 8 tasks

Improve conversions of types in the std lib #5797

SwayStar123 opened this issue Mar 28, 2024 · 0 comments · May be fixed by #5800
Assignees
Labels
blocked breaking May cause existing user code to break. Requires a minor or major release. code quality lib: std Standard library P: medium

Comments

@SwayStar123
Copy link
Member

SwayStar123 commented Mar 28, 2024

There are many missing pairs of conversions that are possible but not implemented in the standard library, aswell as some non trait implementations which are duplicates of their trait implementation counterparts.

  • Resolve duplicate functions
  • From< u8 > for u16
  • From<u8, u16> for u32
  • From<u8, u16, u32> for u64
  • From<u8, u16, u32, u64> for U128
  • From< U128 > for u256, b256
  • TryFrom < U128> for u64, u32, u16, u8
  • TryFrom< B512 > for b256, u256

Remove duplicate functions

There are implementations such as

impl u16 {
    pub fn try_as_u8(self) -> Option<u8> {
        if self <= u8::max().as_u16() {
            Some(asm(input: self) {
                input: u8
            })
        } else {
            None
        }
    }
}

which serve to attempt conversion of a u16 to a u8, however, there is already an implementation of

impl TryFrom<u16> for u8 {
    fn try_from(u: u16) -> Option<Self> {
        if u > u8::max().as_u16() {
            None
        } else {
            Some(asm(r1: u) {
                r1: u8
            })
        }
    }
}

which serves the same purpose, and with the generic auto implementation of TryInto via

impl<T, U> TryInto<U> for T
where
    U: TryFrom<T>,
{
    fn try_into(self) -> Option<U> {
        U::try_from(self)
    }
}

renders the try_as_* functions redundant.

@SwayStar123 SwayStar123 added lib: std Standard library code quality breaking May cause existing user code to break. Requires a minor or major release. P: medium labels Mar 28, 2024
@SwayStar123 SwayStar123 self-assigned this Mar 28, 2024
@SwayStar123 SwayStar123 changed the title Revamp and add conversions to standard library Improve conversions of types in the std lib Mar 28, 2024
IGI-111 added a commit that referenced this issue Apr 2, 2024
## Description
Adds From<u8> for u16

Partially addresses #5797

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: IGI-111 <igi-111@protonmail.com>
SwayStar123 added a commit that referenced this issue Apr 3, 2024
## Description
Adds implementation for From<u8> and From<u16> for u32

Partially addresses #5797 

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
SwayStar123 added a commit that referenced this issue Apr 3, 2024
## Description
Adds implementations for From<u8>, From<u16>, From<u32> for u64

Partially addresses #5797 

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
SwayStar123 added a commit that referenced this issue Apr 24, 2024
## Description
Adds impl From<u8,u16,u32,u64> for U128

Partially addresses #5797 

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Cameron Carstens <bitzoic.eth@gmail.com>
Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
SwayStar123 added a commit that referenced this issue Apr 29, 2024
## Description
Implements  From< U128 > for u256, b256

Partially addresses #5797 

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
SwayStar123 added a commit that referenced this issue May 1, 2024
## Description
Implements TryFrom< U128 > for <u64,u32,u16,u8>

Partially addresses #5797 

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
SwayStar123 added a commit that referenced this issue May 21, 2024
## Description
Adds TryFrom implementation for b256 and u256 from B512

partially addresses #5797

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
Co-authored-by: Cameron Carstens <bitzoic.eth@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked breaking May cause existing user code to break. Requires a minor or major release. code quality lib: std Standard library P: medium
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant