Add str.as_str() for easy dereferencing of Box · qinheping/verify-rust-std@81c4805 (original) (raw)
3 files changed
lines changed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -93,6 +93,7 @@ | ||
| 93 | 93 | // tidy-alphabetical-start |
| 94 | 94 | #![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))] |
| 95 | 95 | #![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))] |
| 96 | +#![cfg_attr(test, feature(str_as_str))] | |
| 96 | 97 | #![feature(alloc_layout_extra)] |
| 97 | 98 | #![feature(allocator_api)] |
| 98 | 99 | #![feature(array_chunks)] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -448,7 +448,11 @@ fn test_from_box_str() { | ||
| 448 | 448 | use std:🧵:String; |
| 449 | 449 | |
| 450 | 450 | let s = String::from("foo").into_boxed_str(); |
| 451 | +assert_eq!((&&&s).as_str(), "foo"); | |
| 452 | + | |
| 451 | 453 | let r: Rc<str> = Rc::from(s); |
| 454 | +assert_eq!((&r).as_str(), "foo"); | |
| 455 | +assert_eq!(r.as_str(), "foo"); | |
| 452 | 456 | |
| 453 | 457 | assert_eq!(&r[..], "foo"); |
| 454 | 458 | } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2737,6 +2737,17 @@ impl str { | ||
| 2737 | 2737 | pub fn substr_range(&self, substr: &str) -> Option<Range<usize>> { |
| 2738 | 2738 | self.as_bytes().subslice_range(substr.as_bytes()) |
| 2739 | 2739 | } |
| 2740 | + | |
| 2741 | +/// Returns the same string as a string slice `&str`. | |
| 2742 | + /// | |
| 2743 | + /// This method is redundant when used directly on `&str`, but | |
| 2744 | + /// it helps dereferencing other string-like types to string slices, | |
| 2745 | + /// for example references to `Box` or `Arc`. | |
| 2746 | + #[inline] | |
| 2747 | +#[unstable(feature = "str_as_str", issue = "130366")] | |
| 2748 | +pub fn as_str(&self) -> &str { | |
| 2749 | +self | |
| 2750 | +} | |
| 2740 | 2751 | } |
| 2741 | 2752 | |
| 2742 | 2753 | #[stable(feature = "rust1", since = "1.0.0")] |