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

Add str.as_str() for easy Deref to string slices #129550

Merged
merged 1 commit into from
Sep 23, 2024

Conversation

kornelski
Copy link
Contributor

@kornelski kornelski commented Aug 25, 2024

Working with Box<str> is cumbersome, because in places like iter.filter() it can end up being &Box<str> or even &&Box<str>, and such type doesn't always get auto-dereferenced as expected.

Dereferencing such box to &str requires ugly syntax like &**boxed_str or &***boxed_str, with the exact amount of *s.

Box<str> is not easily comparable with other string types via PartialEq. Box<str> won't work for lookups in types like HashSet<String>, because Borrow<String> won't take types like &Box<str>. OTOH set.contains(s.as_str()) works nicely regardless of levels of indirection.

String has a simple solution for this: the as_str() method, and Box<str> should too.

@rustbot
Copy link
Collaborator

rustbot commented Aug 25, 2024

r? @cuviper

rustbot has assigned @cuviper.
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 Aug 25, 2024
@tgross35
Copy link
Contributor

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 Aug 25, 2024
@rustbot rustbot assigned Amanieu and unassigned cuviper Aug 25, 2024
@Amanieu Amanieu added I-libs-api-nominated Nominated for discussion during a libs-api team meeting. and removed I-libs-api-nominated Nominated for discussion during a libs-api team meeting. labels Aug 30, 2024
@Amanieu
Copy link
Member

Amanieu commented Sep 3, 2024

We discussed this in the @rust-lang/libs-api meeting. We agree that such a method is useful, however it would be better to place this method on str directly, which allows it work for other smart pointer types such as Arc or Rc. Additionally, since this is a new API it must be marked as unstable and a tracking issue needs to be created for it.

@Amanieu
Copy link
Member

Amanieu commented Sep 13, 2024

@rustbot author

@rustbot rustbot 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 Sep 13, 2024
@GrigorenkoPV
Copy link
Contributor

I've created a tracking issue: #130366

@kornelski kornelski changed the title Add boxed_str.as_str() Add str.as_str() for easy Deref to string slices Sep 16, 2024
@joshtriplett
Copy link
Member

r=me once this passes CI.

@rust-log-analyzer

This comment has been minimized.

library/std/src/lib.rs Outdated Show resolved Hide resolved
library/alloc/src/lib.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@dtolnay
Copy link
Member

dtolnay commented Sep 22, 2024

@bors r=joshtriplett,dtolnay

@bors
Copy link
Contributor

bors commented Sep 22, 2024

📌 Commit 3dcb5a3 has been approved by joshtriplett,dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 22, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 22, 2024
…,dtolnay

Add str.as_str() for easy Deref to string slices

Working with `Box<str>` is cumbersome, because in places like `iter.filter()` it can end up being `&Box<str>` or even `&&Box<str>`, and such type doesn't always get auto-dereferenced as expected.

Dereferencing such box to `&str` requires ugly syntax like `&**boxed_str` or `&***boxed_str`, with the exact amount of `*`s.

`Box<str>` is [not easily comparable with other string types](rust-lang#129852) via `PartialEq`. `Box<str>` won't work for lookups in types like `HashSet<String>`, because `Borrow<String>` won't take types like `&Box<str>`. OTOH `set.contains(s.as_str())` works nicely regardless of levels of indirection.

`String` has a simple solution for this: the `as_str()` method, and `Box<str>` should too.
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 22, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#122565 (Try to write the panic message with a single `write_all` call)
 - rust-lang#129550 (Add str.as_str() for easy Deref to string slices)
 - rust-lang#130659 (Support `char::encode_utf16` in const scenarios.)
 - rust-lang#130705 (No longer mark RTN as incomplete)
 - rust-lang#130712 (Don't call `ty::Const::normalize` in error reporting)
 - rust-lang#130713 (Mark `u8::make_ascii_uppercase` and `u8::make_ascii_lowercase` as const.)
 - rust-lang#130714 (Introduce `structurally_normalize_const`, use it in `rustc_hir_typeck`)
 - rust-lang#130715 (Replace calls to `ty::Const::{try_}eval` in mir build/pattern analysis)

r? `@ghost`
`@rustbot` modify labels: rollup
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Sep 23, 2024
…,dtolnay

Add str.as_str() for easy Deref to string slices

Working with `Box<str>` is cumbersome, because in places like `iter.filter()` it can end up being `&Box<str>` or even `&&Box<str>`, and such type doesn't always get auto-dereferenced as expected.

Dereferencing such box to `&str` requires ugly syntax like `&**boxed_str` or `&***boxed_str`, with the exact amount of `*`s.

`Box<str>` is [not easily comparable with other string types](rust-lang#129852) via `PartialEq`. `Box<str>` won't work for lookups in types like `HashSet<String>`, because `Borrow<String>` won't take types like `&Box<str>`. OTOH `set.contains(s.as_str())` works nicely regardless of levels of indirection.

`String` has a simple solution for this: the `as_str()` method, and `Box<str>` should too.
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 23, 2024
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#129550 (Add str.as_str() for easy Deref to string slices)
 - rust-lang#130344 (Handle unsized consts with type `str`  in v0 symbol mangling)
 - rust-lang#130659 (Support `char::encode_utf16` in const scenarios.)
 - rust-lang#130705 (No longer mark RTN as incomplete)
 - rust-lang#130712 (Don't call `ty::Const::normalize` in error reporting)
 - rust-lang#130713 (Mark `u8::make_ascii_uppercase` and `u8::make_ascii_lowercase` as const.)
 - rust-lang#130714 (Introduce `structurally_normalize_const`, use it in `rustc_hir_typeck`)
 - rust-lang#130715 (Replace calls to `ty::Const::{try_}eval` in mir build/pattern analysis)
 - rust-lang#130723 (Add test for `available_parallelism()`)
 - rust-lang#130726 (tests: Remove spuriously failing vec-tryinto-array codegen test)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 510fc34 into rust-lang:master Sep 23, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Sep 23, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 23, 2024
Rollup merge of rust-lang#129550 - kornelski:boxasstr, r=joshtriplett,dtolnay

Add str.as_str() for easy Deref to string slices

Working with `Box<str>` is cumbersome, because in places like `iter.filter()` it can end up being `&Box<str>` or even `&&Box<str>`, and such type doesn't always get auto-dereferenced as expected.

Dereferencing such box to `&str` requires ugly syntax like `&**boxed_str` or `&***boxed_str`, with the exact amount of `*`s.

`Box<str>` is [not easily comparable with other string types](rust-lang#129852) via `PartialEq`. `Box<str>` won't work for lookups in types like `HashSet<String>`, because `Borrow<String>` won't take types like `&Box<str>`. OTOH `set.contains(s.as_str())` works nicely regardless of levels of indirection.

`String` has a simple solution for this: the `as_str()` method, and `Box<str>` should too.
@kornelski kornelski deleted the boxasstr branch September 24, 2024 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

10 participants