Allow shared access to SyncCell
for types that are already Sync
(#7718)
# Objective The type `SyncCell<T>` (added in #5483) is used to force any wrapped type to be `Sync`, by only allowing exclusive access to the wrapped value. This restriction is unnecessary for types which are already `Sync`. --- ## Changelog + Added the method `read` to `SyncCell`, which allows shared access to values that already implement the `Sync` trait.
This commit is contained in:
parent
69d3a15eae
commit
38568ccf1f
@ -29,6 +29,14 @@ impl<T: ?Sized> SyncCell<T> {
|
|||||||
&mut self.inner
|
&mut self.inner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For types that implement [`Sync`], get shared access to this `SyncCell`'s inner value.
|
||||||
|
pub fn read(&self) -> &T
|
||||||
|
where
|
||||||
|
T: Sync,
|
||||||
|
{
|
||||||
|
&self.inner
|
||||||
|
}
|
||||||
|
|
||||||
/// Build a mutable reference to a `SyncCell` from a mutable reference
|
/// Build a mutable reference to a `SyncCell` from a mutable reference
|
||||||
/// to its inner value, to skip constructing with [`new()`](SyncCell::new()).
|
/// to its inner value, to skip constructing with [`new()`](SyncCell::new()).
|
||||||
pub fn from_mut(r: &'_ mut T) -> &'_ mut SyncCell<T> {
|
pub fn from_mut(r: &'_ mut T) -> &'_ mut SyncCell<T> {
|
||||||
@ -38,6 +46,6 @@ impl<T: ?Sized> SyncCell<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SAFETY: `Sync` only allows multithreaded access via immutable reference.
|
// SAFETY: `Sync` only allows multithreaded access via immutable reference.
|
||||||
// As `SyncCell` requires an exclusive reference to access the wrapped value,
|
// As `SyncCell` requires an exclusive reference to access the wrapped value for `!Sync` types,
|
||||||
// marking this type as `Sync` does not actually allow threaded access to the inner value.
|
// marking this type as `Sync` does not actually allow unsychronized access to the inner value.
|
||||||
unsafe impl<T: ?Sized> Sync for SyncCell<T> {}
|
unsafe impl<T: ?Sized> Sync for SyncCell<T> {}
|
||||||
|
Loading…
Reference in New Issue
Block a user