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

Guard types should implement more traits #24372

Closed
huonw opened this issue Apr 13, 2015 · 15 comments
Closed

Guard types should implement more traits #24372

huonw opened this issue Apr 13, 2015 · 15 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. 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

@huonw
Copy link
Member

huonw commented Apr 13, 2015

Types like std::sync::MutexGuard are semantically just a &mut with a destructor, and so it makes sense for them to implement the same sort of traits, e.g. Display and Debug.

@huonw huonw added the A-libs label Apr 13, 2015
@steveklabnik
Copy link
Member

Triage: MutexGuard does not implement those traits today.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum
Copy link
Member

Debug is implemented today, but not Display.

@rust-lang/libs: This seems relevant to libz blitz, we probably want to work out some guidelines for wrapper types like this (what traits they should "forward"). For example, I can't think of a good Display implementation for MutexGuard, but perhaps there is one.

To an extent, it could be argued that most if not all std traits should be forwarded through types like MutexGuard, but this feels like it gets painful very quickly.

@sfackler
Copy link
Member

The Display impl for MutexGuard would just delegate to the inner value's Display.

@brson
Copy link
Contributor

brson commented Jun 20, 2017

Smart pointers and guards should eagerly implement common traits. Debug at least should be transparent, custom implemented to defer to the inner type. Not clear whether Eq/Ord should be implemented against the inner type.

Next step here is to identify such types in std and file a PR.

Here's an issue for the guidelines: rust-lang/api-guidelines#94

@brson brson added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. and removed I-nominated labels Jun 20, 2017
@brson brson changed the title Guard types in std::sync should implement more traits Guard types should implement more traits Jun 20, 2017
@ChrisMacNaughton
Copy link
Contributor

I'd like to have a go at this one

@ChrisMacNaughton
Copy link
Contributor

ChrisMacNaughton commented Jun 22, 2017

As a first pass, I'm looking at ensuring that MutexGuard, RwLockReadGuard, and RwLockWriteGuard impl Display and Debug. How does that sound?

@Mark-Simulacrum
Copy link
Member

Sounds good! You can also look at things like Arc and RefCells guards.

@ChrisMacNaughton
Copy link
Contributor

@Mark-Simulacrum RefCell doesn't seem to have a Guard? https://doc.rust-lang.org/std/cell/index.html#structs

@Mark-Simulacrum
Copy link
Member

@ChrisMacNaughton
Copy link
Contributor

Thanks!

ChrisMacNaughton added a commit to ChrisMacNaughton/rust that referenced this issue Jun 22, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 22, 2017
…excrichton

Ensure Guard types impl Display & Debug

Fixes rust-lang#24372
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 23, 2017
…excrichton

Ensure Guard types impl Display & Debug

Fixes rust-lang#24372
@brson brson reopened this Jun 23, 2017
@brson
Copy link
Contributor

brson commented Jun 23, 2017

Thanks a bunch @ChrisMacNaughton.

I'm reopening this because we also want Eq, PartialEq, Ord, PartialOrd, and Hash for these types.

@rust-lang/libs if we implement e.g. impl Eq<Rhs = MutexGuard<T>> for MutexGuard<T> should we also implement impl Eq<Rhs = T> for MutexGuard<T>?

That would also seem to call for something like impl Eq<Rhs = MutexGuard<T>> for T, but that doesn't seem like something that can be written in std, or can it?

@alexcrichton
Copy link
Member

I've never been clear on the set of Eq/PartialEq and such impls we have (those with a type parameter), but adding more seems fine by me.

@ghost
Copy link

ghost commented Jul 7, 2017

What about Borrow/BorrowMut/AsRef/AsMut impls?

@Mark-Simulacrum Mark-Simulacrum added C-enhancement Category: An issue proposing an enhancement or a PR with one. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Jul 22, 2017
@Mark-Simulacrum Mark-Simulacrum removed the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
marcusbuffett added a commit to marcusbuffett/rust that referenced this issue Sep 23, 2017
Implements Hash, PartialEq, Eq, PartialOrd, and Ord for
Ref, RefMut, MutexGuard, RwLockReadGuard, RwLockWriteGuard

Fixes rust-lang#24372
@dtolnay
Copy link
Member

dtolnay commented Nov 19, 2017

I am closing for now because I believe we are going to hold off on adding more of these impls.

Ok we got a chance to discuss this during the libs triage meeting today. In discussing this we ended up being increasingly wary of the guideline we set forth to add these trait impls and ended up concluding that we may not wish to add these at this time.

These implementations, as added here, wouldn't enable something like MutexGuard<i32> == i32, but that's arguably something we'd like to work. Adding that, however, precludes MutexGuard<i32> == MutexGuard<i32>. We didn't believe there was a clear way to implement this right now as all current strategies had downsides.

The possibility of specialization in the future may end up providing a solution to this problem, and @aturon also mentioned that the lang team is going to be working during the impl period on "deref coercion ergonomics" to alleviate much of the need for these impls in the first place.

In light of all that I'm going to close this for now and we'll likely be removing the guideline indicating that these traits should be implemented temporarily.

@dtolnay dtolnay closed this as completed Nov 19, 2017
@Stargateur
Copy link
Contributor

These implementations, as added here, wouldn't enable something like MutexGuard<i32> == i32, but that's arguably something we'd like to work. Adding that, however, precludes MutexGuard<i32> == MutexGuard<i32>. We didn't believe there was a clear way to implement this right now as all current strategies had downsides.

What strategy and what are the downside ? Following https://stackoverflow.com/q/66753005/7076153 question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. 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.
Projects
None yet
Development

No branches or pull requests

10 participants