Skip to content

Add some AsRef implementations for smart pointers #139318

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

MatthijsKok
Copy link

@MatthijsKok MatthijsKok commented Apr 3, 2025

Implements part of #132138

This is my first time contributing to the Rust Project.
Please let me know if I need to make any changes, or update the PR in any way.
I have read the contributing guidelines.

@rustbot
Copy link
Collaborator

rustbot commented Apr 3, 2025

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 3, 2025
@rust-log-analyzer

This comment has been minimized.

@MatthijsKok MatthijsKok changed the title Add some AsRef/AsMut implementations for smart pointers Add some AsRef implementations for smart pointers Apr 3, 2025
@rust-log-analyzer

This comment has been minimized.

@jhpratt
Copy link
Member

jhpratt commented Apr 3, 2025

as something that needs FCP,

r? libs

@rustbot rustbot assigned ibraheemdev and unassigned jhpratt Apr 3, 2025
@xizheyin
Copy link
Contributor

xizheyin commented Apr 3, 2025

CI failed. You can check for success locally before pushing. :)

@MatthijsKok
Copy link
Author

MatthijsKok commented Apr 3, 2025

CI failed. You can check for success locally before pushing. :)

Thanks for the suggestion, thats very useful :) I think I fixed the CI now, at least it works locally. CI is green!

@ibraheemdev
Copy link
Member

r? libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 3, 2025
@rustbot rustbot assigned dtolnay and unassigned ibraheemdev Apr 3, 2025
@thaliaarchi
Copy link
Contributor

I think Arc should be done together with Rc, and probably UniqueRc too. I don’t think the reference counted types should grow apart.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 6, 2025
@rustbot

This comment has been minimized.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 6, 2025
@MatthijsKok
Copy link
Author

I think Arc should be done together with Rc, and probably UniqueRc too. I don’t think the reference counted types should grow apart.

Yes that was my initial plan as well. However, adding the same impl blocks for Arc resulted in compilation errors inside src/tools/rust-analyzer's smol_str dependency, and I wanted to not add changes "all over the place" for my first PR.

I could try to resolve the errors produced by adding back Arc. If the diff that fixes it is relatively small, would you like me to add it back?

@thaliaarchi
Copy link
Contributor

thaliaarchi commented Apr 6, 2025

I think we're going to get inference failures with any amount of AsRef impls, unfortunately. My guess is the merit of this PR will be judged by the extent of the inference failures seen in a crater run, though I'm not a rust-lang reviewer.

I think this PR should either be only Box or Box, Rc, Arc, and UniqueRc. I suspect you're seeing inference failures for Arc<str> and not Rc<str>, because Arc<str> is more popular. Limiting it to Box would probably be wise, unless libs-api reviewers want to do a go/no-go with all of them.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I am not confident than any of these can be added without widespread breakage. For example, the new Box impls break pretty reasonable-looking code like the following:

fn main() {
    let b = "...".to_owned().into_boxed_str();
    println!("{}", b.as_ref());
}
error[E0283]: type annotations needed
 --> src/main.rs:3:22
  |
3 |     println!("{}", b.as_ref());
  |                      ^^^^^^
  |
  = note: multiple `impl`s satisfying `Box<str>: AsRef<_>` found in the following crates: `alloc`, `std`:
          - impl AsRef<OsStr> for Box<str>;
          - impl AsRef<Path> for Box<str>;
          - impl<A> AsRef<[u8]> for Box<str, A>
            where A: Allocator;
          - impl<T, A> AsRef<T> for Box<T, A>
            where A: Allocator, T: ?Sized;
help: try using a fully qualified path to specify the expected types
  |
3 -     println!("{}", b.as_ref());
3 +     println!("{}", <Box<str> as AsRef<T>>::as_ref(&b));
  |

If you'd like to see a crater run, I would recommend starting with just 1 single new impl, whichever of these is your favorite. Please send a separate PR for each impl that we should crater, and we can keep this PR as-is to keep track of all the impls we'd ideally like to add.

@dtolnay dtolnay added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-crater This change needs a crater run to check for possible breakage in the ecosystem. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants