Skip to content

Impl AsRef, Borrow for Ref, RefMut #101981

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

Closed
wants to merge 2 commits into from
Closed

Conversation

dhardy
Copy link
Contributor

@dhardy dhardy commented Sep 18, 2022

Summary: Implement AsRef, Borrow for std::cell::Ref, RefMut.

Sorry if I didn't exactly follow a guide. This is too small to need an RFC?

I'm not certain about the #[stable] attrs, but it seems there's little choice in the matter (given that one can't have unstable impls of existing traits).

Motivation

Why? Because we can. This is what AsRef and Borrow are for? (More generally: any impl of Deref should impl AsRef too?)

Further motivation:

trait Foo<T: ?Sized> {
    type Ref<'b>: std::borrow::Borrow<T>;
    fn borrow(&self) -> Self::Ref<'_>;
}

struct MyCell(std::cell::RefCell<String>);
impl Foo<str> for MyCell {
    type Ref<'b> = std::cell::Ref<'b>; // avoids the need to construct a new type here
    fn borrow(&self) -> Self::Ref<'_> {
        std::cell::Ref::map(self.0.borrow(), String::as_str)
    }
}

ACP: rust-lang/libs-team#215

@rust-highfive
Copy link
Contributor

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot
Copy link
Collaborator

rustbot commented Sep 18, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 18, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 18, 2022
@dhardy
Copy link
Contributor Author

dhardy commented Sep 18, 2022

Hmm, the Borrow impls may violate the "soft requirements" on the Borrow type:

x.borrow() == y.borrow() should give the same result as x == y

But Ref does not impl PartialEq.


Unfortunately neither AsRef nor Borrow are exactly the ideal trait for the above example: AsRef does not have a blanket impl<T> AsRef<T> for T while Borrow has the above "soft requirement on behaviour" in its documentation.

@dhardy
Copy link
Contributor Author

dhardy commented Sep 18, 2022

@rustbot label +T-libs-api -T-libs

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 18, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
    Checking rustc_resolve v0.0.0 (/checkout/compiler/rustc_resolve)
error[E0507]: cannot move out of a shared reference
   --> compiler/rustc_infer/src/infer/resolve.rs:209:32
    |
209 |               ty::ReVar(_) => Ok(self
210 | |                 .infcx
211 | |                 .lexical_region_resolutions
212 | |                 .borrow()
213 | |                 .as_ref()
213 | |                 .as_ref()
214 | |                 .expect("region resolution not performed")
    | |__________________|_______________________________________|
    | |__________________|_______________________________________|
    |                    |                                       move occurs because value has type `Option<LexicalRegionResolutions<'_>>`, which does not implement the `Copy` trait
    |                    value moved due to this method call
    |
note: this function takes ownership of the receiver `self`, which moves value
    |
735 |     pub const fn expect(self, msg: &str) -> T {
    |                         ^^^^
    |                         ^^^^
help: consider calling `.as_ref()` to borrow the type's contents
    |
214 |                 .as_ref().expect("region resolution not performed")

For more information about this error, try `rustc --explain E0507`.
error: could not compile `rustc_infer` due to previous error
warning: build failed, waiting for other jobs to finish...
warning: build failed, waiting for other jobs to finish...
error: could not compile `rustc_infer` due to previous error
error[E0507]: cannot move out of a shared reference
    --> compiler/rustc_resolve/src/lib.rs:1577:44
     |
1577 |         for (trait_name, trait_binding) in traits.as_ref().unwrap().iter() {
     |                                            ^^^^^^^^^^^^^^^^--------
     |                                            |               |
     |                                            |               value moved due to this method call
     |                                            move occurs because value has type `Option<std::boxed::Box<[(rustc_span::symbol::Ident, &NameBinding<'_>)]>>`, which does not implement the `Copy` trait
     |
note: this function takes ownership of the receiver `self`, which moves value
     |
772  |     pub const fn unwrap(self) -> T {
     |                         ^^^^
     |                         ^^^^
help: consider calling `.as_ref()` to borrow the type's contents
     |
1577 |         for (trait_name, trait_binding) in traits.as_ref().as_ref().unwrap().iter() {

error: could not compile `rustc_resolve` due to previous error
error: could not compile `rustc_resolve` due to previous error
Build completed unsuccessfully in 0:02:28

@bors
Copy link
Collaborator

bors commented Dec 20, 2022

☔ The latest upstream changes (presumably #105951) made this pull request unmergeable. Please resolve the merge conflicts.

@anden3
Copy link
Contributor

anden3 commented Apr 22, 2023

Hello @dhardy! I noticed that there's a merge conflict for this PR, what's the status of it? :)

@anden3 anden3 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 22, 2023
@dhardy
Copy link
Contributor Author

dhardy commented Apr 23, 2023

Status: no feedback. I'm not going to waste time resolving CI/merge issues on a PR that no one wants to comment on.

@Noratrieb
Copy link
Member

Hi, thank you for your PR! The process for adding new library APIs was changed and if you want to push your PR forward, you should open an ACP instead to get additional visibility. The libs API team does not have enough time to go though everything, but your change seems simple enough that it could get approved.

@Dylan-DPC Dylan-DPC added the S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. label Apr 23, 2023
@Dylan-DPC Dylan-DPC removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 16, 2023
@dtolnay dtolnay added the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Sep 28, 2023
@dhardy
Copy link
Contributor Author

dhardy commented Sep 6, 2024

Closing (see the ACP, rust-lang/libs-team#215).

@dhardy dhardy closed this Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants