-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Better wrapper structs #3596
Comments
Do you really want a newtype wrapper without any restrictions on accessing the underlying type? For example, should it be valid to add |
That's a very good question I'm not sure about. There are two possible solutions:
But I think that raises an even better question: should it be possible to add a u32 to Millimeter and raise a Millimeter to the power of a u32?
So this boils down to two questions:
|
@kennytm Do you have any extra feedback? I need more info than just a confused emoji. If there is something that you are confused about, I can answer it. |
@coolCucumber-cat yes have you researched for existing proposals before? this is most likely a duplicate of #949 and #2242 |
@shepmaster Can I get more feedback than a reaction? I want to hear your reasoning and maybe I can improve or close this issue |
@coolCucumber-cat stop tagging individuals like that, it's rude. Clicking on a reaction button doesn't express willingness to participate in the discussion. And it doesn't give you permission to cause a distraction for someone simply demanding their time and attention.
You have been made aware your issue is a duplicate. Start by addressing the conclusions of the previous work. |
@senekor Sorry if I don't know all the rules. Is this GitHub specific because I've never heard of that before. I wasn't demanding them to, I was asking if they could just help me out, maybe they could improve. They don't have to respond if they don't want to. About the duplicate, I forgot to address it, it wasn't intentional, but I don't think it is a duplicate because it's vaguely the same concept, but implemented in a very different way, as far as I understand. There aren't really any similarities and those other issues don't really even concretely talk about how it should be implemented. |
So there isn't inheritance in Rust but you can easily make wrapper structs and implement methods on that. Since this is quite common, there could be a way to avoid needing to always access the extra fields. It's common to use a tuple with one field but normal structs are common too, like
String
which is a wrapper over aVec<u8>
. I'm not sure how to do this or if it's even needed. If you have your own ideas or you can improve my ideas, feel free to comment.So my main idea is just a sugar syntax over a tuple with a single field. It would be like the inner type, but with the extra methods from the outer struct. I have 2 ideas how to do this syntax for this idea, using measurements as wrappers over numbers:
wrapper
as a placeholder):Which can then be used like this:
Millimeter
is treated exactly like a u32 but with those extra methods fromMillimeter
too. Sou32
works a little bit like a trait thatMillimeter
implements and you can pass it to anything that expects au32
and can also be coerced to au32
:If you don't understand how something might work, I can give more specific examples or you can send me an example and I'll tell you how it would. Or you can send me examples how you think it should work or give an example that demonstrates why it won't work. There could be many situtations where it is very ambiguous.
Is it even worth the effort? I'm trying to learn more about Rust. Understanding what the boundaries are, so explanations why this won't work are appreciated 😁.
The text was updated successfully, but these errors were encountered: