-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Conversation
r? @shepmaster (rust_highfive has picked a reviewer for you, use r? to override) |
@rustbot modify labels: +T-libs |
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 I think the mutable equivalent is even more interesting, though, because other than PinCell there is not much use for 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 { ... }
} |
Co-authored-by: Peter Todd <pete@petertodd.org>
Co-authored-by: David Tolnay <dtolnay@gmail.com>
@rfcbot fcp merge This PR adds two constructors for pinned references: from |
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. |
Another API we could consider in the future (but I'm not sure its useful enough) is adding 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))); |
There was a problem hiding this 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.
@bors r+ |
📌 Commit df95dce has been approved by |
…, 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.
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`
…-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.
…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
This adds
Pin::static_ref
andPin::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
andsys_common
mutexes/rwlocks/condvars to usePin<&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 astatic
(e.g. throughsys_common::StaticMutex
) fulfills the requirements about never moving it, but right now there's no safe way to get aPin<&T>
to astatic
. This solves that.