You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking Issue for {Mutex, RwLock, ReentrantLock}::data_ptr #140368
This is a tracking issue for allowing unguarded access to the inner value of a Mutex or RwLock. Previous discussion here: rust-lang/libs-team#531
Public API
// std::syncimpl<T: ?Sized>Mutex<T>{pubfndata_ptr(&self) -> *mutT;}impl<T: ?Sized>RwLock<T>{pubfndata_ptr(&self) -> *mutT;}// This type is unstable under feature `reentrant_lock`impl<T: ?Sized>ReentrantLock<T>{pubfndata_ptr(&self) -> *mutT;}
Steps / History
(Remember to update the S-tracking-* label when checking boxes.)
@jplatte, @tgross35 or anyone with access: Please update the list of feature gates to also include #![feature(reentrant_lock_data_ptr)].
Thanks in advance.
This tracking issue and the previous discussion (so far) haven't discussed making (or not making) data_ptr a const fn. It seems to be very easy (pull request).
A use case: static array data held in a Mutex (never modified after initialized) AND further static pointers/references/slices to such an array (with LLVM de-duplication "disabled" by passing the Mutex through core::hint::black_box).
An example (which doesn't compile with the current implementation; it should compile with that pull request unless there's a typo in this example - I'm not re-configuring my toolchain at the moment):
#![cfg_attr(feature = "mx", feature(mutex_data_ptr))]typeU8Array = [u8;3];pubstaticMX: std::sync::Mutex<U8Array> = core::hint::black_box(std::sync::Mutex::new([b'A',b'B',b'C']));// data_ptr() needs to be `const fn`:pubstaticSTR_FULL:&'staticstr = unsafe{ core::str::from_utf8_unchecked((&*MX.data_ptr()as&U8Array).as_slice())};
Current implementation doesn't allow such pointers/references/slices to be static. A workaround is to wrap the cast slice arithmetic (if any) in functions. The casts are zero-cost and slice arithmetic may be inlined/optimized away, but it's still ugly:
This use case is becoming important given the planned 2027-or-so discontinuation of/hard error for static mut. There is no other zero-cost portable or foreign-static-free alternative (other than LazyLock, which is not zero cost).
However, even worse than "ugliness", data_ptr by being "non-const" prevents its results to be used in static expressions (possibly returning an empty tuple ()), whose only purpose is compile-time checks/validations (compile time "tests"). Yes, controversial or personal preference, and limited by using const functions/expressions only. But, helpful, especially in IDE's with rust-analyzer: Compile-time failures of such expressions warn the developer early, without a need to run any tests = faster iterations. (Again, an example that should compile with that pull request but I didn't check. I do have similar use cases which involve pointer/slice arithmetic: hash-injector -> signal.rs):
As-is implementation is const fn-compatible: data_ptr for all three types can be changed to const fn. Are there any platform/architecture-specific, design or future-proofing reasons against this?
I don't think there is any reason not to make these const.
This use case is becoming important given the planned 2027-or-so discontinuation of/hard error for static mut. There is no other zero-cost portable or foreign-static-free alternative (other than LazyLock, which is not zero cost).
I believe the consensus here has been amended and static mut is no longer going away, see #53639.
Hey @Amanieu, I'm curious what would be needed to move this forward. Maybe the open Qs from the issue description need to be discussed in a libs-api team meeting?
Activity
Auto merge of #140369 - jplatte:mutex-rwlock-data-ptr, r=Amanieu
Rollup merge of rust-lang#140369 - jplatte:mutex-rwlock-data-ptr, r=A…
Rollup merge of rust-lang#140369 - jplatte:mutex-rwlock-data-ptr, r=A…
Rollup merge of rust-lang#140369 - jplatte:mutex-rwlock-data-ptr, r=A…
Unrolled build for #140369
Rollup merge of rust-lang#140369 - jplatte:mutex-rwlock-data-ptr, r=A…
rust-lang#140368 Mutex/RwLock/ReentrantLock::data_ptr to be const fn
peter-lyons-kehl commentedon Sep 22, 2025
@jplatte, @tgross35 or anyone with access: Please update the list of feature gates to also include
#![feature(reentrant_lock_data_ptr)].Thanks in advance.
peter-lyons-kehl commentedon Sep 22, 2025
This tracking issue and the previous discussion (so far) haven't discussed making (or not making)
data_ptraconst fn. It seems to be very easy (pull request).A use case:
staticarray data held in aMutex(never modified after initialized) AND furtherstaticpointers/references/slices to such an array (with LLVM de-duplication "disabled" by passing theMutexthroughcore::hint::black_box).An example (which doesn't compile with the current implementation; it should compile with that pull request unless there's a typo in this example - I'm not re-configuring my toolchain at the moment):
Current implementation doesn't allow such pointers/references/slices to be
static. A workaround is to wrap the cast slice arithmetic (if any) in functions. The casts are zero-cost and slice arithmetic may be inlined/optimized away, but it's still ugly:This use case is becoming important given the planned 2027-or-so discontinuation of/hard error for
static mut. There is no other zero-cost portable or foreign-static-free alternative (other than LazyLock, which is not zero cost).However, even worse than "ugliness",
data_ptrby being "non-const" prevents its results to be used instaticexpressions (possibly returning an empty tuple()), whose only purpose is compile-time checks/validations (compile time "tests"). Yes, controversial or personal preference, and limited by usingconstfunctions/expressions only. But, helpful, especially in IDE's withrust-analyzer: Compile-time failures of such expressions warn the developer early, without a need to run any tests = faster iterations. (Again, an example that should compile with that pull request but I didn't check. I do have similar use cases which involve pointer/slice arithmetic: hash-injector -> signal.rs):As-is implementation is
const fn-compatible:data_ptrfor all three types can be changed toconst fn. Are there any platform/architecture-specific, design or future-proofing reasons against this?[-]Tracking Issue for {Mutex, RwLock}::data_ptr[/-][+]Tracking Issue for {Mutex, RwLock, ReentrantLock}::data_ptr[/+]tgross35 commentedon Sep 22, 2025
I don't think there is any reason not to make these
const.I believe the consensus here has been amended and
static mutis no longer going away, see #53639.Amanieu commentedon Sep 22, 2025
I also agree. Since this is still an unstable API, you can just send a PR to change it to
const fn.1 remaining item
Auto merge of #146943 - matthiaskrgr:rollup-oh3k78y, r=matthiaskrgr
Rollup merge of rust-lang#146904 - peter-lyons-kehl:140368_data_ptr_c…
Auto merge of #146946 - matthiaskrgr:rollup-fsmrqez, r=matthiaskrgr
Rollup merge of rust-lang#146904 - peter-lyons-kehl:140368_data_ptr_c…
Auto merge of #146946 - matthiaskrgr:rollup-fsmrqez, r=matthiaskrgr
Auto merge of #146946 - matthiaskrgr:rollup-fsmrqez, r=matthiaskrgr
Unrolled build for #146904
Rollup merge of rust-lang#146904 - peter-lyons-kehl:140368_data_ptr_c…
Auto merge of rust-lang#146946 - matthiaskrgr:rollup-fsmrqez, r=matth…
Auto merge of #146946 - matthiaskrgr:rollup-fsmrqez, r=matthiaskrgr
Auto merge of #146946 - matthiaskrgr:rollup-fsmrqez, r=matthiaskrgr
Rollup merge of rust-lang#146904 - peter-lyons-kehl:140368_data_ptr_c…
jplatte commentedon Jun 11, 2026
Hey @Amanieu, I'm curious what would be needed to move this forward. Maybe the open Qs from the issue description need to be discussed in a libs-api team meeting?