|
| 1 | +#![doc = include_str!("../README.md")] |
1 | 2 | use std::{cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, ptr::NonNull};
|
2 | 3 |
|
3 | 4 | /// Type-erased borrow of some unknown type chosen when constructing this type.
|
@@ -249,13 +250,35 @@ impl<'a, T> From<&'a [T]> for ThinSlicePtr<'a, T> {
|
249 | 250 | }
|
250 | 251 | }
|
251 | 252 |
|
252 |
| -pub(crate) trait UnsafeCellDeref<'a, T> { |
| 253 | +mod private { |
| 254 | + use std::cell::UnsafeCell; |
| 255 | + |
| 256 | + pub trait SealedUnsafeCell {} |
| 257 | + impl<'a, T> SealedUnsafeCell for &'a UnsafeCell<T> {} |
| 258 | +} |
| 259 | + |
| 260 | +/// Extension trait for helper methods on [`UnsafeCell`] |
| 261 | +pub trait UnsafeCellDeref<'a, T>: private::SealedUnsafeCell { |
| 262 | + /// # Safety |
| 263 | + /// - The returned value must be unique and not alias any mutable or immutable references to the contents of the [`UnsafeCell`]. |
| 264 | + /// - At all times, you must avoid data races. If multiple threads have access to the same [`UnsafeCell`], then any writes must have a proper happens-before relation to all other accesses or use atomics ([`UnsafeCell`] docs for reference). |
253 | 265 | unsafe fn deref_mut(self) -> &'a mut T;
|
| 266 | + |
| 267 | + /// # Safety |
| 268 | + /// - For the lifetime `'a` of the returned value you must not construct a mutable reference to the contents of the [`UnsafeCell`]. |
| 269 | + /// - At all times, you must avoid data races. If multiple threads have access to the same [`UnsafeCell`], then any writes must have a proper happens-before relation to all other accesses or use atomics ([`UnsafeCell`] docs for reference). |
254 | 270 | unsafe fn deref(self) -> &'a T;
|
| 271 | + |
| 272 | + /// Returns a copy of the contained value. |
| 273 | + /// |
| 274 | + /// # Safety |
| 275 | + /// - The [`UnsafeCell`] must not currently have a mutable reference to its content. |
| 276 | + /// - At all times, you must avoid data races. If multiple threads have access to the same [`UnsafeCell`], then any writes must have a proper happens-before relation to all other accesses or use atomics ([`UnsafeCell`] docs for reference). |
255 | 277 | unsafe fn read(self) -> T
|
256 | 278 | where
|
257 | 279 | T: Copy;
|
258 | 280 | }
|
| 281 | + |
259 | 282 | impl<'a, T> UnsafeCellDeref<'a, T> for &'a UnsafeCell<T> {
|
260 | 283 | #[inline]
|
261 | 284 | unsafe fn deref_mut(self) -> &'a mut T {
|
|
0 commit comments