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

Replace TrustCell implementation with atomic_refcell::AtomicRefCell #224

Merged

Conversation

Imberflur
Copy link
Contributor

@Imberflur Imberflur commented Feb 5, 2023

This PR replaces TrustCell with atomic_refcell::AtomicCell which provides near identical functionality.

Advantages of this change:

  • atomic_refcell uses an alternative strategy to handle overflows of the read counter which allows for a single atomic operation when acquiring a read guard.
  • Removing the TrustCell implementation reduces the amount of unsafe code in this crate.

Based on top of commits from: #223
Waiting on: bholley/atomic_refcell#18

Comment on lines +552 to +556
/// # Safety
///
/// If this is used to replace the `Box<dyn Resource>` with a different one, the new one must
/// have a `TypeId` that matches the one in the `ResourceId` provided here.
pub unsafe fn try_fetch_internal(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh almost forgot!

The one other thing I did in these changes is to mark this method as unsafe since it would otherwise allow safely swapping out the Box<dyn Resource> stored at a particular ResourceId with any other arbitrary Box<dyn Resource> that doesn't necessarily correspond to the ResourceId.

@Imberflur Imberflur mentioned this pull request Feb 5, 2023
@Imberflur Imberflur force-pushed the replace-trustcell-with-atomicrefcell branch from 8bcc2b8 to 967a7d0 Compare February 25, 2023 21:52
Also mark a method `unsafe` that would otherwise allow safe code to swap
the trait object stored at a particular `ResourceId`.
@Imberflur Imberflur force-pushed the replace-trustcell-with-atomicrefcell branch from 967a7d0 to b119397 Compare April 24, 2023 21:57
@Imberflur Imberflur marked this pull request as ready for review April 24, 2023 22:04
Copy link
Member

@torkleyy torkleyy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Good catch on the unsafe function.

@Imberflur Imberflur merged commit e898794 into amethyst:master Apr 25, 2023
@Imberflur Imberflur deleted the replace-trustcell-with-atomicrefcell branch April 25, 2023 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants