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

From<UnderlyingType> and TryFrom<UnderlyingType> #13

Open
hecatia-elegua opened this issue Apr 28, 2023 · 2 comments
Open

From<UnderlyingType> and TryFrom<UnderlyingType> #13

hecatia-elegua opened this issue Apr 28, 2023 · 2 comments

Comments

@hecatia-elegua
Copy link
Collaborator

hecatia-elegua commented Apr 28, 2023

Writing some_num.try_into()? can be useful, I noted this somewhere and forgot about it, so I don't have a direct usecase.
Most likely macro generated code would need less typing and casting?

Oh right, it could replace try_new, too.
That's actually the point. The ux crate does that as well and it keeps the interface clean.
From<UnderlyingType> would replace new then.
Of course this would need deprecation notes or if that's too extreme we can use feature gates here again (I hope not)

@hecatia-elegua hecatia-elegua changed the title Should we have TryFrom<UnderlyingType>? From<UnderlyingType> and TryFrom<UnderlyingType> May 17, 2023
@danlehmann
Copy link
Owner

The reason I avoided this for now is because then it's no longer const.

But TryFrom would be possible to add on top of try_new(). The downside then is that you have multiple ways of doing the same thing

@hecatia-elegua
Copy link
Collaborator Author

hecatia-elegua commented May 30, 2023

TryFrom can be used generically, which is a different thing.

Another argument for not providing it would be that users should be nudged in the right direction and since it's not const yet, I agree that we should not provide it unless a usecase crops up.

Edit: less writing for stuff like this maybe:

// generated by macro
const fn set_field3<T: TryInto<u4>>(&mut self, value: T) {
    let field3 = value.try_into().expect("value is not convertible to u4");
    //...
}
lalala.set_field3(6); //maybe 6u8
lalala.set_field3(u2::new(6));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants