3 ms·
These would be very welcome changes. How do self references work when the struct is moved, given that they're pointers? Also I'm not sure about having a featu
by IshKebab 2y ago
These would be very welcome changes.
How do self references work when the struct is moved, given that they're pointers?
Also I'm not sure about having a feature that only works for private functions. I can imagine that would be very weird and annoying. I think it would be fine if it were allowed for public functions too, we just need better built in tooling to warn you about API breaking changes. After all you can already break API compatibility in obvious and very subtle ways. It would be great if Rust said something like "this is an API incompatible change; you need to increase the version number".
I think there are third party tools that try to do that but it would be great if it was first party.
- alkonaut 2y ago> How do self references work when the struct is moved, given that they're pointers? Are all references necessarily (global) pointers? Couldn't self references be offsets?
- Rusky 2y agoYes, they are necessarily global pointers. If a self reference is an offset, what happens when you take its address (and pass that somewhere else that doesn't know where it came from)? Now every "reference to a reference" (and similar things) can dynamically be a global pointer or a relative one, with no way to tell how to use it.
- alkonaut 2y agoAm I necessarily always allowed to convert a reference to a numeric address?
- Rusky 2y agoYes, it's safe to cast a reference to a raw pointer and a raw pointer to an address. It might have been nicer in some ways if the language had restricted that a bit more up front, but it would probably have taken some design work to do that while still supporting a lot of the low-level stuff people need to do.
- alkonaut 2y agoAn internal reference could only be cast to a relative memory offset. It’s not a global memory address in disguise like a normal reference. So the same casting rules wouldn't apply.
- cptroot 2y agoI believe this self-references proposal won't cover structs that point inside themselves. Instead it would only allow you to store references that point inside other members heap allocations.