PinCell use new_unchecked

I assume you’re asking whether this API would be sound. Note that even if it was sound, it would not really be necessary since we can build an even easier to use API in the form of the pin! macro. Yes, it’s a macro, but macros aren’t all that bad, and also, there is no known way to avoid the need for a macro, as far as I’m aware, so a macro is the only option anyway. Which makes post like yours somewhat interesting since the premise is likely thar you're trying to change that and demonstrate a way to avoid macros after all.

Note that you do need to study the safety requirements for Pin carefully though. And indeed this API is unsound under those requirements, since it allows a user to circumvent the drop guarantee of pinning by wrapping the PinCell in a ManuallyDrop. Rust Playground


For further discussion/information/context on the drop guarantee and why it might be useful, feel free to take a look at this semi-recent thread where a different user presented their own idea of a presumably-sound macro-free stack-pinning API that failed to be sound due to the drop guarantee.

4 Likes