-
Notifications
You must be signed in to change notification settings - Fork 30
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
with_upgraded
methods on upgradable read guards
#66
with_upgraded
methods on upgradable read guards
#66
Conversation
Allow temporarily upgrading an upgradable read guard to a write guard given only a mutable reference to the guard. `RwLockUpgradableReadLockArc::with_upgraded_blocking`, along with the other missing blocking arc rwlock methods, are left to future work. Investigating whether it's possible to have sound `async` equivalents of these methods is also left to future work.
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.
Not sure about this API. Feels like you can do the same thing with:
let mut guard = Some(...);
{
let mut upgraded = guard.take().unwrap().upgrade().await;
f(&mut upgraded);
guard = Some(upgraded.downgrade());
}
Yes, and in fact that's the workaround I use now. However, this PR's API has 2 advantages:
|
The guards are niched (at least I think they are) so there shouldn't be any space wasted. The movement here amount to around four small
This is still possible: let mut guard = Some(...);
{
let upgraded = guard.take().unwrap().upgrade().await;
struct RestoreOnDrop<'a, T> {
value: T,
slot: &'a mut Option<T>
}
impl<'a, T> Drop for RestoreOnDrop<'a, T> {
fn drop() {
*self.slot = Some(value.downgrade());
}
let mut upgraded = RestoreOnDrop {
value: upgraded,
slot: &mut guard,
};
f(&mut upgraded.value);
} Yes, this is a little bit more code. However, it's a truly cursed use case if you still need your guard held on your |
To explain my use-case: I have a resource (represented by a struct), that's managed by a long-running async task. The task regularly reads from the resource, and occasionally writes; the write operations are short and don't cross |
Thanks for explaining in great detail but as @notgull pointed out, this seems like a very niche use case.
Why not pass an |
So I can |
I'm going to say that we shouldn't add this. It's a pretty niche use case that doesn't fit well with the rest of the API, and it can be worked around for these niche use cases. I'd say that the cost of adding this API and maintaining it for the foreseeable future outweighs the potential utility it would provide. Thanks anyways! |
Allow temporarily upgrading an upgradable read guard to a write guard given only a mutable reference to the guard.
RwLockUpgradableReadLockArc::with_upgraded_blocking
, along with the other missing blocking arc rwlock methods, is left to future work.Investigating whether it's possible to have sound
async
equivalents of these methods is also left to future work.