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

Remove duplicate functions from conversions library #5800

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

SwayStar123
Copy link
Member

Description

Partially closes #5797

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.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • 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
  • 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.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@SwayStar123 SwayStar123 added lib: std Standard library breaking May cause existing user code to break. Requires a minor or major release. labels Mar 29, 2024
@SwayStar123 SwayStar123 self-assigned this Mar 29, 2024
@SwayStar123
Copy link
Member Author

Blocked by #5883

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking May cause existing user code to break. Requires a minor or major release. lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve conversions of types in the std lib
1 participant