Skip to content

Add Pin::static_ref, static_mut. #77726

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 6 commits into from
Oct 21, 2020

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Oct 8, 2020

This adds Pin::static_ref and Pin::static_mut, which convert a static reference to a pinned static reference.

Static references are effectively already pinned, as what they refer to has to live forever and can never be moved.


Context: I want to update the sys and sys_common mutexes/rwlocks/condvars to use Pin<&self> in their functions, instead of only warning in the unsafety comments that they may not be moved. That should make them a little bit less dangerous to use. Putting such an object in a static (e.g. through sys_common::StaticMutex) fulfills the requirements about never moving it, but right now there's no safe way to get a Pin<&T> to a static. This solves that.

@rust-highfive
Copy link
Contributor

r? @shepmaster

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 8, 2020
@m-ou-se
Copy link
Member Author

m-ou-se commented Oct 8, 2020

@rustbot modify labels: +T-libs

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 8, 2020
@withoutboats
Copy link
Contributor

Separate from my comments on #77801, I think this method is somewhat interesting. I can think of a use case of using it to pin a PinCell for example.

I think the mutable equivalent is even more interesting, though, because other than PinCell there is not much use for Pin<&T>. So I would add these unstably as a pair of methods: static_ref and static_mut:

impl<T: ?Sized> Pin<&'static T> {
    pub fn static_ref(r: &'static T) -> Self { ... }
}

impl<T: ?Sized> Pin<&'static mut T> {
    pub fn static_mut(r: &'static mut T) -> Self { ... }
}

@m-ou-se m-ou-se mentioned this pull request Oct 12, 2020
@shepmaster
Copy link
Member

r? @withoutboats

Co-authored-by: Peter Todd <pete@petertodd.org>
@dtolnay dtolnay changed the title Add Pin::new_static. Add Pin::static_ref, static_mut. Oct 15, 2020
Co-authored-by: David Tolnay <dtolnay@gmail.com>
@withoutboats
Copy link
Contributor

@rfcbot fcp merge

This PR adds two constructors for pinned references: from &'static T and &'static mut T; these constructors are sound because you can never move or invalidate something to which you have a 'static reference.

@rfcbot
Copy link
Collaborator

rfcbot commented Oct 16, 2020

Team member @withoutboats has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 16, 2020
@withoutboats
Copy link
Contributor

Another API we could consider in the future (but I'm not sure its useful enough) is adding leak to Pin<Box<T>> etc:

impl<T: ?Sized> Pin<Box<T>> {
    fn leak(this: Pin<Box<T>>) -> Pin<&'static mut T> { /* .. */ }
}

// equivalent expressions:
let x = Pin::leak(Box::pin(expr));
let x = Pin::static_mut(Box::leak(Box::new(expr)));

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see 3/6 checkboxes and this PR is just adding unstable methods so I think we can land without waiting for an FCP.

@dtolnay
Copy link
Member

dtolnay commented Oct 20, 2020

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 20, 2020

📌 Commit df95dce has been approved by dtolnay

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 20, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 21, 2020
…, r=dtolnay

Add Pin::static_ref, static_mut.

This adds `Pin::static_ref` and `Pin::static_mut`, which convert a static reference to a pinned static reference.

Static references are effectively already pinned, as what they refer to has to live forever and can never be moved.

---

Context: I want to update the `sys` and `sys_common` mutexes/rwlocks/condvars to use `Pin<&self>` in their functions, instead of only warning in the unsafety comments that they may not be moved. That should make them a little bit less dangerous to use. Putting such an object in a `static` (e.g. through `sys_common::StaticMutex`) fulfills the requirements about never moving it, but right now there's no safe way to get a `Pin<&T>` to a `static`. This solves that.
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 21, 2020
Rollup of 7 pull requests

Successful merges:

 - rust-lang#77726 (Add Pin::static_ref, static_mut.)
 - rust-lang#78002 (Tweak "object unsafe" errors)
 - rust-lang#78056 (BTreeMap: split off most code of remove and split_off)
 - rust-lang#78063 (Improve wording of "cannot multiply" type error)
 - rust-lang#78094 (rustdoc: Show the correct source filename in page titles, without `.html`)
 - rust-lang#78101 (fix static_ptr_ty for foreign statics)
 - rust-lang#78118 (Inline const followups)

Failed merges:

r? `@ghost`
@bors bors merged commit ff3c8cb into rust-lang:master Oct 21, 2020
@rustbot rustbot added this to the 1.49.0 milestone Oct 21, 2020
@rfcbot rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 21, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 22, 2020
…-tracking-issue, r=withoutboats

Add tracking issue number for pin_static_ref

Forgot to add a tracking issue in rust-lang#77726. Opened rust-lang#78186 as tracking issue.
@m-ou-se m-ou-se deleted the static-pin branch November 15, 2020 16:03
@Mark-Simulacrum Mark-Simulacrum mentioned this pull request Nov 15, 2020
@Mark-Simulacrum
Copy link
Member

This was backported into 1.48 (approved by me) in #79075 as a dependency of #77801.

@Mark-Simulacrum Mark-Simulacrum modified the milestones: 1.49.0, 1.48.0 Nov 15, 2020
@Mark-Simulacrum Mark-Simulacrum added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Nov 15, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 15, 2020
…ulacrum

[beta] next

This backports:

* Avoid installing external LLVM dylibs rust-lang#78986
* Install CI llvm into the library directory rust-lang#79074
* Revert "Revert "resolve: Avoid "self-confirming" import resolutions in one more case"" rust-lang#78784
* Bump Rustfmt and RLS rust-lang#78775
* Enforce no-move rule of ReentrantMutex using Pin and fix UB in stdio rust-lang#77801

For RLS/rustfmt compilation to succeed:
* change the order of type arguments on ControlFlow rust-lang#76614
* Add ControlFlow::is_{break,continue} methods rust-lang#78200
* Replace run_compiler with RunCompiler builder pattern rust-lang#77649

As a dependency of rust-lang#77801:
*  Add Pin::static_ref, static_mut. rust-lang#77726
@dtolnay dtolnay self-assigned this Mar 24, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants