Skip to content

Commit 6743ad1

Browse files
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.
2 parents 467ed3c + 3dcb5a3 commit 6743ad1

File tree

6 files changed

+17
-62
lines changed

6 files changed

+17
-62
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1721,20 +1721,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17211721
}
17221722
}
17231723

1724-
if item_name.name == sym::as_str && rcvr_ty.peel_refs().is_str() {
1725-
let msg = "remove this method call";
1726-
let mut fallback_span = true;
1727-
if let SelfSource::MethodCall(expr) = source {
1728-
let call_expr = self.tcx.hir().expect_expr(self.tcx.parent_hir_id(expr.hir_id));
1729-
if let Some(span) = call_expr.span.trim_start(expr.span) {
1730-
err.span_suggestion(span, msg, "", Applicability::MachineApplicable);
1731-
fallback_span = false;
1732-
}
1733-
}
1734-
if fallback_span {
1735-
err.span_label(span, msg);
1736-
}
1737-
} else if let Some(similar_candidate) = similar_candidate {
1724+
if let Some(similar_candidate) = similar_candidate {
17381725
// Don't emit a suggestion if we found an actual method
17391726
// that had unsatisfied trait bounds
17401727
if unsatisfied_predicates.is_empty()

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
// tidy-alphabetical-start
9494
#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
9595
#![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
96+
#![cfg_attr(test, feature(str_as_str))]
9697
#![feature(alloc_layout_extra)]
9798
#![feature(allocator_api)]
9899
#![feature(array_chunks)]

library/alloc/src/rc/tests.rs

+4
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,11 @@ fn test_from_box_str() {
448448
use std::string::String;
449449

450450
let s = String::from("foo").into_boxed_str();
451+
assert_eq!((&&&s).as_str(), "foo");
452+
451453
let r: Rc<str> = Rc::from(s);
454+
assert_eq!((&r).as_str(), "foo");
455+
assert_eq!(r.as_str(), "foo");
452456

453457
assert_eq!(&r[..], "foo");
454458
}

library/core/src/str/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2740,6 +2740,17 @@ impl str {
27402740
pub fn substr_range(&self, substr: &str) -> Option<Range<usize>> {
27412741
self.as_bytes().subslice_range(substr.as_bytes())
27422742
}
2743+
2744+
/// Returns the same string as a string slice `&str`.
2745+
///
2746+
/// This method is redundant when used directly on `&str`, but
2747+
/// it helps dereferencing other string-like types to string slices,
2748+
/// for example references to `Box<str>` or `Arc<str>`.
2749+
#[inline]
2750+
#[unstable(feature = "str_as_str", issue = "130366")]
2751+
pub fn as_str(&self) -> &str {
2752+
self
2753+
}
27432754
}
27442755

27452756
#[stable(feature = "rust1", since = "1.0.0")]

tests/ui/suggestions/remove-as_str.rs

-21
This file was deleted.

tests/ui/suggestions/remove-as_str.stderr

-27
This file was deleted.

0 commit comments

Comments
 (0)