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

Implement trait Into<T> in lib-std #3399

Open
ControlCplusControlV opened this issue Nov 21, 2022 · 2 comments
Open

Implement trait Into<T> in lib-std #3399

ControlCplusControlV opened this issue Nov 21, 2022 · 2 comments
Labels
enhancement New feature or request lib: std Standard library

Comments

@ControlCplusControlV
Copy link
Contributor

Sway should have an Into trait in the Standard library for

pub trait Into<T> {
    fn into(self) -> T;
}

Also From implementation should automatically derive Into

@ControlCplusControlV ControlCplusControlV added the lib: std Standard library label Nov 21, 2022
@emilyaherbert emilyaherbert added the enhancement New feature or request label Nov 21, 2022
@mohammadfawaz mohammadfawaz changed the title Implement Generic Into in the std Implement trait Into<T> in lib-std Nov 23, 2022
@mohammadfawaz
Copy link
Contributor

mohammadfawaz commented Nov 23, 2022

This requires generic traits which we already have. It also requires trait constraints on impl blocks so that we can write:

// From implies Into
impl<T, U> Into<U> for T
where
    U: From<T>,
{
    /// Calls `U::from(self)`.
    ///
    /// That is, this conversion is whatever the implementation of
    /// `From<T>` for `U` chooses to do.
    fn into(self) -> U {
        U::from(self)
    }
}

where Into is defined as:

pub trait Into<T>: Sized {
    /// Converts this type into the (usually inferred) input type.
    fn into(self) -> T;
}

@nfurfaro
Copy link
Contributor

nfurfaro commented Jan 5, 2023

@mohammadfawaz in light of #3637 , this issue could probably be simplified not to require any trait constraints, as we're moving towards decoupling From & Into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lib: std Standard library
Projects
Status: Todo
Development

No branches or pull requests

4 participants