-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Make RwLockReadGuard
covariant over its type parameter
#80392
Labels
A-concurrency
Area: Concurrency
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
E-help-wanted
Call for participation: Help is requested to fix this issue.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
Comments
This was referenced Dec 26, 2020
LeSeulArtichaut
added
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
labels
Dec 28, 2020
the8472
removed
the
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
label
Feb 8, 2022
yaahc
added
A-concurrency
Area: Concurrency
E-help-wanted
Call for participation: Help is requested to fix this issue.
labels
Feb 8, 2022
I would love to give this a try! 👍 |
First steps:
|
JohnTitor
added a commit
to JohnTitor/rust
that referenced
this issue
Jun 22, 2022
Make RwLockReadGuard covariant Hi, first time contributor here, if anything is not as expected, please let me know. `RwLockReadGoard`'s type constructor is invariant. Since it behaves like a smart pointer to an immutable reference, there is no reason that it should not be covariant. Take e.g. ``` fn test_read_guard_covariance() { fn do_stuff<'a>(_: RwLockReadGuard<'_, &'a i32>, _: &'a i32) {} let j: i32 = 5; let lock = RwLock::new(&j); { let i = 6; do_stuff(lock.read().unwrap(), &i); } drop(lock); } ``` where the compiler complains that &i doesn't live long enough. If `RwLockReadGuard` is covariant, then the above code is accepted because the lifetime can be shorter than `'a`. In order for `RwLockReadGuard` to be covariant, it can't contain a full reference to the `RwLock`, which can never be covariant (because it exposes a mutable reference to the underlying data structure). By reducing the data structure to the required pieces of `RwLock`, the rest falls in place. If there is a better way to do a test that tests successful compilation, please let me know. Fixes rust-lang#80392
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-concurrency
Area: Concurrency
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
E-help-wanted
Call for participation: Help is requested to fix this issue.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
Currently
RwLockReadGuard
is invariant over its type parameter, meaning that for instance this sound code:Will not compile. However, since
RwLockReadGuard
does not allow for mutation of the inner value, it would be sound to make it covariant, and it would be useful in some circumstances.See also the same issue in parking_lot.
The text was updated successfully, but these errors were encountered: