Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Allow shared access to SyncCell for types that are already Sync #7718

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/bevy_utils/src/synccell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ impl<T: ?Sized> SyncCell<T> {
&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
/// to its inner value, to skip constructing with [`new()`](SyncCell::new()).
pub fn from_mut(r: &'_ mut T) -> &'_ mut SyncCell<T> {
Expand All @@ -38,6 +46,6 @@ impl<T: ?Sized> SyncCell<T> {
}

// SAFETY: `Sync` only allows multithreaded access via immutable reference.
// As `SyncCell` requires an exclusive reference to access the wrapped value,
// marking this type as `Sync` does not actually allow threaded access to the inner value.
// As `SyncCell` requires an exclusive reference to access the wrapped value for `!Sync` types,
// marking this type as `Sync` does not actually allow unsychronized access to the inner value.
unsafe impl<T: ?Sized> Sync for SyncCell<T> {}