Skip to content

Commit 59042f3

Browse files
committed
Reword MaybeUninit::assume_init_read doc
* Add a introductory sentence to the thread safety paragraph in Safety section * Clarify that a bad usage example is actually library UB
1 parent 1033c71 commit 59042f3

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

library/core/src/mem/maybe_uninit.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ impl<T> MaybeUninit<T> {
726726
/// `assume_init_read` and then [`assume_init`]), it is your responsibility
727727
/// to ensure that data may indeed be duplicated.
728728
///
729-
/// Additionally, if you call this function from a different thread than the one
729+
/// Since this function does not require `T: Send`, you also need to carefully consider
730+
/// thread safety. If you call this function from a different thread than the one
730731
/// that holds the `MaybeUninit<T>`, it logically constitutes a cross-thread ownership
731732
/// transfer of the contained value `T`. You are responsible for guaranteeing
732733
/// the thread safety of the transfer. Note that `MaybeUninit<T>` is [`Sync`] if `T`
@@ -793,12 +794,14 @@ impl<T> MaybeUninit<T> {
793794
///
794795
/// let mtx = Mutex::new(0u32);
795796
/// let x = MaybeUninit::new(mtx.lock().unwrap());
796-
/// // Moving the `MutexGuard<'_, u32>: !Send + Sync` to another thread.
797-
/// // ⚠️ Thread safety is not guaranteed here!
797+
/// // Moving the `MutexGuard<'_, u32>: !Send + Sync` to another thread,
798+
/// // thread safety not guaranteed here.
798799
/// thread::scope(|s| {
799800
/// // This compiles because `MaybeUninit<MutexGuard<'_, u32>>` is `Sync`.
800801
/// s.spawn(|| {
801802
/// let _unused = unsafe { x.assume_init_read() };
803+
/// // `_unused: MutexGuard<'_, u32>` is dropped here, on a different thread
804+
/// // than the one that locked the mutex; this is library-level UB ⚠️!
802805
/// });
803806
/// });
804807
/// ```

0 commit comments

Comments
 (0)