rust
rust copied to clipboard
Add boxed_str.as_str()
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. It's not comparable with other string types via PartialEq, and won't work for lookups in HashSet<String>.
Dereferencing such box to &str requires ugly syntax like &**boxed_str or &***boxed_str, with the exact amount of *s.
String has a simple solution for this: the as_str() method, and Box<str> should too.
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
r? libs-api
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.
@rustbot author
I've created a tracking issue: #130366
r=me once this passes CI.
The job x86_64-gnu-llvm-17 failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)
------
> importing cache manifest from ghcr.io/rust-lang/rust-ci-cache:fb97a1d6377f6cf2227825318ca4bbde3889e0c420746f5a03ba46a07e9a862725c26a09b9fc49a0d129ebd75935d3f6cd19acf41cc4267a6846fd4aa574b12c:
------
##[endgroup]
Setting extra environment values for docker: --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-17]
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure:
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-17', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-17/bin/llvm-config
configure: llvm.link-shared := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
- | -^^^^^^-- help: remove this method call
+ | ^^^^^^
+ |
+ = note: see issue #130366 <https://github.com/rust-lang/rust/issues/130366> for more information
+ = help: add `#![feature(str_as_str)]` to the crate attributes to enable
+ = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
- error[E0599]: no method named `as_str` found for reference `&'a str` in the current scope
+ error[E0658]: use of unstable library feature 'str_as_str'
8 --> $DIR/remove-as_str.rs:7:7
9 |
9 |
10 LL | s.as_str();
- | -^^^^^^-- help: remove this method call
+ | ^^^^^^
+ |
+ = note: see issue #130366 <https://github.com/rust-lang/rust/issues/130366> for more information
+ = help: add `#![feature(str_as_str)]` to the crate attributes to enable
+ = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
- error[E0599]: no method named `as_str` found for mutable reference `&mut str` in the current scope
+ error[E0658]: use of unstable library feature 'str_as_str'
14 --> $DIR/remove-as_str.rs:12:7
15 |
15 |
16 LL | s.as_str();
- | -^^^^^^-- help: remove this method call
+ | ^^^^^^
+ |
+ = note: see issue #130366 <https://github.com/rust-lang/rust/issues/130366> for more information
+ = help: add `#![feature(str_as_str)]` to the crate attributes to enable
+ = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
- error[E0599]: no method named `as_str` found for reference `&&str` in the current scope
+ error[E0658]: use of unstable library feature 'str_as_str'
20 --> $DIR/remove-as_str.rs:17:7
21 |
21 |
22 LL | s.as_str();
- | -^^^^^^-- help: remove this method call
+ | ^^^^^^
+ |
+ = note: see issue #130366 <https://github.com/rust-lang/rust/issues/130366> for more information
+ = help: add `#![feature(str_as_str)]` to the crate attributes to enable
+ = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
25 error: aborting due to 4 previous errors
26
- For more information about this error, try `rustc --explain E0599`.
---
To only update this specific test, also pass `--test-args suggestions/remove-as_str.rs`
error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/suggestions/remove-as_str.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/suggestions/remove-as_str" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/suggestions/remove-as_str/auxiliary"
--- stderr -------------------------------
error[E0658]: use of unstable library feature 'str_as_str'
##[error] --> /checkout/tests/ui/suggestions/remove-as_str.rs:2:7
|
|
LL | s.as_str();
| ^^^^^^
|
= note: see issue #130366 <https://github.com/rust-lang/rust/issues/130366> for more information
= help: add `#![feature(str_as_str)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature 'str_as_str'
##[error] --> /checkout/tests/ui/suggestions/remove-as_str.rs:7:7
|
LL | s.as_str();
LL | s.as_str();
| ^^^^^^
|
= note: see issue #130366 <https://github.com/rust-lang/rust/issues/130366> for more information
= help: add `#![feature(str_as_str)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature 'str_as_str'
##[error] --> /checkout/tests/ui/suggestions/remove-as_str.rs:12:7
|
LL | s.as_str();
LL | s.as_str();
| ^^^^^^
|
= note: see issue #130366 <https://github.com/rust-lang/rust/issues/130366> for more information
= help: add `#![feature(str_as_str)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature 'str_as_str'
##[error] --> /checkout/tests/ui/suggestions/remove-as_str.rs:17:7
|
LL | s.as_str();
LL | s.as_str();
| ^^^^^^
|
= note: see issue #130366 <https://github.com/rust-lang/rust/issues/130366> for more information
= help: add `#![feature(str_as_str)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0658`.
------------------------------------------
A job failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)
@bors r=joshtriplett,dtolnay
:pushpin: Commit 3dcb5a39622fe705647ea0764a355a10b6913832 has been approved by joshtriplett,dtolnay
It is now in the queue for this repository.