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

Expose more methods from mapped guards #444

Open
katietheqt-old opened this issue Jun 12, 2024 · 7 comments
Open

Expose more methods from mapped guards #444

katietheqt-old opened this issue Jun 12, 2024 · 7 comments

Comments

@katietheqt-old
Copy link

katietheqt-old commented Jun 12, 2024

While very useful, mapped guards do not currently expose methods such as raw and force_unlock, which makes using them in certain contexts quite difficult. From what I can tell, there's no soundness reason why they haven't been exposed. Would a PR adding the same APIs to them as the other guards be accepted?

@Amanieu
Copy link
Owner

Amanieu commented Jun 23, 2024

Yes, I would I accept a PR for these. Fundamentally these are unsafe functions so it's fine to expose them with appropriate warnings.

@Earthcomputer
Copy link

I also came across a situation where I needed to downgrade a write mapped guard to a read mapped guard. So there are a few APIs missing.

@grantslatton
Copy link

@Amanieu is there any fundamental reason why a mapped guard can't have unlocked(f)? I find myself wanting it right now.

It's maybe slightly weird since you'd actually be unlocking the parent object, not the mapped result, but that's what I want to do.

Open to workarounds too.

@Amanieu
Copy link
Owner

Amanieu commented Jan 26, 2025

That's fundamentally unsound because while the mutex is a unlocked, another thread can lock it and then invalidate the mapping. For example, if the mutex contains an enum which has been mapped to one variant, another thread can lock the mutex and switch the enum to the other variant.

@grantslatton
Copy link

@Amanieu would it be sound if MappedMutexGuard didn't have a pointer to the mapped data, but rather the original data, and then an impl Fn(&Original) -> &Mapped that it called whenever dereferencing?

(obviously you probably don't want to do this for many reasons, just curious for my own education on soundness here)

@Amanieu
Copy link
Owner

Amanieu commented Jan 26, 2025

Yes that would be sound, but then you would lose the type erasure since Original still needs to be in the guard type.

@grantslatton
Copy link

That makes sense

If anyone is googling this and trying to do the same thing, I worked around this by making a trait like WriteGuard: DerefMut that has unlocked and map methods, so I can simulate type erasure by using impl WriteGuard<Target = MappedType> all over

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

No branches or pull requests

4 participants