Skip to content

Commit

Permalink
Unrolled build for rust-lang#129550
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rust-timer authored Sep 23, 2024
2 parents 66b0b29 + 3dcb5a3 commit 901757d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 62 deletions.
15 changes: 1 addition & 14 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,20 +1721,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

if item_name.name == sym::as_str && rcvr_ty.peel_refs().is_str() {
let msg = "remove this method call";
let mut fallback_span = true;
if let SelfSource::MethodCall(expr) = source {
let call_expr = self.tcx.hir().expect_expr(self.tcx.parent_hir_id(expr.hir_id));
if let Some(span) = call_expr.span.trim_start(expr.span) {
err.span_suggestion(span, msg, "", Applicability::MachineApplicable);
fallback_span = false;
}
}
if fallback_span {
err.span_label(span, msg);
}
} else if let Some(similar_candidate) = similar_candidate {
if let Some(similar_candidate) = similar_candidate {
// Don't emit a suggestion if we found an actual method
// that had unsatisfied trait bounds
if unsatisfied_predicates.is_empty()
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
// tidy-alphabetical-start
#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
#![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
#![cfg_attr(test, feature(str_as_str))]
#![feature(alloc_layout_extra)]
#![feature(allocator_api)]
#![feature(array_chunks)]
Expand Down
4 changes: 4 additions & 0 deletions library/alloc/src/rc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,11 @@ fn test_from_box_str() {
use std::string::String;

let s = String::from("foo").into_boxed_str();
assert_eq!((&&&s).as_str(), "foo");

let r: Rc<str> = Rc::from(s);
assert_eq!((&r).as_str(), "foo");
assert_eq!(r.as_str(), "foo");

assert_eq!(&r[..], "foo");
}
Expand Down
11 changes: 11 additions & 0 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2740,6 +2740,17 @@ impl str {
pub fn substr_range(&self, substr: &str) -> Option<Range<usize>> {
self.as_bytes().subslice_range(substr.as_bytes())
}

/// Returns the same string as a string slice `&str`.
///
/// This method is redundant when used directly on `&str`, but
/// it helps dereferencing other string-like types to string slices,
/// for example references to `Box<str>` or `Arc<str>`.
#[inline]
#[unstable(feature = "str_as_str", issue = "130366")]
pub fn as_str(&self) -> &str {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
21 changes: 0 additions & 21 deletions tests/ui/suggestions/remove-as_str.rs

This file was deleted.

27 changes: 0 additions & 27 deletions tests/ui/suggestions/remove-as_str.stderr

This file was deleted.

0 comments on commit 901757d

Please sign in to comment.