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

Proposal: One more way to make self referial structs. #2664

Closed
tema3210 opened this issue Mar 17, 2019 · 4 comments
Closed

Proposal: One more way to make self referial structs. #2664

tema3210 opened this issue Mar 17, 2019 · 4 comments

Comments

@tema3210
Copy link

tema3210 commented Mar 17, 2019

Proposal

My idea constists of making a kind of special trait OnCopy, which should have a method to update a value of &this pointer, in order to allow struct refer to itself, outer code shouldn't change it, method should be called only by runtime.As addition in such case we would like to have ability to know is this hook created or not(for compiler needs, program logic).
Usage of the trait should be marked explictly #[Derive(OnCopy)].

UPD: I meant to have no need to overload it, it just may be called to update self-refs in struct, in time of process of move, e.g should be not overloaded. And implemented automatically

UPD 2: Another variant of the proposed trait is building two derive macroses instead of one. One named like #[Derive(SelfRefBase)], and tells compiler that this struct can have self-ref's(Just marker for adding blanket impl. ). Second named like #[Derive(SelfRef)], should mark field that refers to object in struct, require it to be a ref.(via adding it during compile time into static byte array(which indicates which refs in struct should be updated, in order to point to right location))

I've seen the #2613, main difference of these proposals is that there this trait should be marked explictly by user, and not be implemented by one.

@Ixrec
Copy link
Contributor

Ixrec commented Mar 17, 2019

This feature is generally called "move constructors" after the similar feature in C++11.

The biggest problem with any variation on a move constructors proposal is that it abandons the guarantee that moving a Copy type really means memcopy/bitwise copy without any arbitrary user-defined code. In particular, removing that guarantee now would be a breaking change, since unsafe code can currently rely on things like "moves can't panic".

The second biggest problem is that move constructors don't actually solve the self-referential struct problem anyway. They obviously help in some simple cases, but to make a fully general and sound solution you need some other language feature like "generative existential lifetimes" (unfortunately I don't think anyone ever wrote out a proper explanation of what this is), which would probably make move constructors superfluous.

https://internals.rust-lang.org/t/idea-limited-custom-move-semantics-through-explicitly-specified-relocations/6704 is one past thread discussing these kinds of ideas.

@eaglgenes101
Copy link

For the first issue, I've floated around the idea of a Relocate trait which permits the explicit moving of a !Unpin value behind a Pin. Reception was rather cold when I presented it, so I wouldn't count on getting much support, especially since it doesn't have a reply to other common concerns.

@F001
Copy link
Contributor

F001 commented Mar 22, 2019

Similar ideas had been discussed many times: #2613

@jonas-schievink
Copy link
Contributor

Closing in favor of the existing discussions. It is unlikely that Rust will get something like this.

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

5 participants