New core type suggestion - StructP

I wanted to suggest a new inherent type - a StructP.

At a low level, I honestly do not know the techniques for implementing "pinning", but the idea is this is inherently a struct that will not move. With StructP, you'd be able to have self-referential structs without workarounds with the tradeoff that it will always get pinned, which may or may not be an issue for you.

1 Like

Currently with Rust you can first build the struct, pass it around, and then pin it. With this the struct will be built already pinned.

The upside seems to be that you don't get a Pin<Something> out of it but rather a Something is already pinned. (Is it an upside? You lose all methods of Pin as well)

Is it worth it?

Yeah, the idea is 'permanently pinned' since it needs to be, so no pinning methods needed (I don't think). The other thought is IF the struct has a self-reference (i know not allowed now, but in future rust), then it will be recognized as such and automatically pinned when allocated. That way, no structP type needed, just use regular struct. That said, I don't know ANYTHING about the details of pinning and how much/where it causes issues of not being able to move something out of initial allocation.

And the message to the developer is this. "We don't like people who keep referring to themselves, so if you do, expect to get pinned to the mat!"

How do you create such a struct, considering that even returning it will move it?

3 Likes

Presumably, the language would have similar restrictions for these as it does for unsized types. Without a solution for placement new, though, they could only be safely be constructed as local variables with all access mediated through & or &mut.

2 Likes

This is basically how the self-referencial structs you can construct in safe Rust today work.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.