-
Notifications
You must be signed in to change notification settings - Fork 31
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
Added a method that allows for conditional mutable access #132
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As noted in the comments, this approach might actually work, but needs some more care:
- Some argument/proof why this is OK to do.
- Deciding how to deal with the
Weak
type. - semver compatibility.
return None; | ||
} | ||
Some(unsafe { ptr.as_mut().unwrap() }) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 This might actually work.
However, some more argument / (non-formal) proof that this is actually OK and safe would be in place here.
Also, instead of having 3 unsafe
blocks just one after another, maybe we can just put all three things into a block.
drop(guard); | ||
assert_eq!(1, Arc::strong_count(&ptr)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a check for the try_mut access, calls for a dedicated test (I know there's one „hidden“ in this long method, but we probably should have a test where it can be found easily). Especially, making sure it is not accessible in all the cases listed above.
@@ -67,6 +68,9 @@ pub unsafe trait RefCnt: Clone { | |||
/// This must not be called by code outside of this crate. | |||
unsafe fn from_ptr(ptr: *const Self::Base) -> Self; | |||
|
|||
/// Returns the reference count of the pointer. | |||
unsafe fn ref_cnt(ptr: *const Self::Base) -> usize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, adding a new method into the trait would be a semver-breaking change.
Worse, the crate also offers (under a feature) RefCnt for Weak
… and I don't think this method is valid (eg. makes sense) for that type.
if ptr.is_null() { | ||
0 | ||
} else { | ||
1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look correct. This implementation is for things like ArcSwap<Option<Arc<X>>
. And the inner Arc can have multiple references.
Also, I'd suggest you do some measurements. This method will likely be expensive (the wait_for_readers thing). |
Adds a new method
try_mut
which if there is only a single reference to theArcSwap
grants mutable access to the insides