Skip to content

Pin is unsound due to the literal constructor #139013

Closed as not planned
Closed as not planned
@hxuhack

Description

@hxuhack

The safe constructor Pin::new(pointer: Ptr) restricts the pointed object with the Unpin trait. However, this restriction can be bypassed using the literal constructor of Pin or the pin! macro.

Why not prevent this by making the literal constructor private and modifying the pin! macro as follows?

pub struct Pin<Ptr> {
    __pointer: Ptr,
}

macro_rules! safe_pin {
    ($value:expr) => {
        Pin::<&mut _>::new(&mut $value)
    };
}

To the best of my knowledge, the side effect is that API users can no longer create a Pin object using pin! if the object does not implement Unpin. However, developers can still use Pin::new_unchecked(pointer: Ptr) to achieve the same result. More importantly, I believe unsoundness must not be tolerated in safe Rust.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-pinArea: PinC-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions