Skip to content

Commit 20f7ac7

Browse files
authored
Unrolled build for #145933
Rollup merge of #145933 - GrigorenkoPV:thing_as_thing, r=Amanieu Expand `str_as_str` to more types Tracking issue: #130366 ACP: rust-lang/libs-team#643 This PR expands `str_from_str` feature and adds analogous methods to more types. Namely: - `&CStr` - `&[T]`, `&mut [T]` - `&OsStr` - `&Path` - `&ByteStr`, `&mut ByteStr` (tracking issue: #134915) (technically was not part of ACP)
2 parents ed0006a + 48f7f31 commit 20f7ac7

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

library/core/src/bstr/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ impl ByteStr {
6868
ByteStr::from_bytes(bytes.as_ref())
6969
}
7070

71+
/// Returns the same string as `&ByteStr`.
72+
///
73+
/// This method is redundant when used directly on `&ByteStr`, but
74+
/// it helps dereferencing other "container" types,
75+
/// for example `Box<ByteStr>` or `Arc<ByteStr>`.
76+
#[inline]
77+
// #[unstable(feature = "str_as_str", issue = "130366")]
78+
#[unstable(feature = "bstr", issue = "134915")]
79+
pub const fn as_byte_str(&self) -> &ByteStr {
80+
self
81+
}
82+
83+
/// Returns the same string as `&mut ByteStr`.
84+
///
85+
/// This method is redundant when used directly on `&mut ByteStr`, but
86+
/// it helps dereferencing other "container" types,
87+
/// for example `Box<ByteStr>` or `MutexGuard<ByteStr>`.
88+
#[inline]
89+
// #[unstable(feature = "str_as_str", issue = "130366")]
90+
#[unstable(feature = "bstr", issue = "134915")]
91+
pub const fn as_mut_byte_str(&mut self) -> &mut ByteStr {
92+
self
93+
}
94+
7195
#[doc(hidden)]
7296
#[unstable(feature = "bstr_internals", issue = "none")]
7397
#[inline]

library/core/src/ffi/c_str.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,17 @@ impl CStr {
648648
pub fn display(&self) -> impl fmt::Display {
649649
crate::bstr::ByteStr::from_bytes(self.to_bytes())
650650
}
651+
652+
/// Returns the same string as a string slice `&CStr`.
653+
///
654+
/// This method is redundant when used directly on `&CStr`, but
655+
/// it helps dereferencing other string-like types to string slices,
656+
/// for example references to `Box<CStr>` or `Arc<CStr>`.
657+
#[inline]
658+
#[unstable(feature = "str_as_str", issue = "130366")]
659+
pub const fn as_c_str(&self) -> &CStr {
660+
self
661+
}
651662
}
652663

653664
#[stable(feature = "c_string_eq_c_str", since = "1.90.0")]

library/core/src/slice/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,6 +4908,28 @@ impl<T> [T] {
49084908

49094909
if start <= self.len() && end <= self.len() { Some(start..end) } else { None }
49104910
}
4911+
4912+
/// Returns the same slice `&[T]`.
4913+
///
4914+
/// This method is redundant when used directly on `&[T]`, but
4915+
/// it helps dereferencing other "container" types to slices,
4916+
/// for example `Box<[T]>` or `Arc<[T]>`.
4917+
#[inline]
4918+
#[unstable(feature = "str_as_str", issue = "130366")]
4919+
pub const fn as_slice(&self) -> &[T] {
4920+
self
4921+
}
4922+
4923+
/// Returns the same slice `&mut [T]`.
4924+
///
4925+
/// This method is redundant when used directly on `&mut [T]`, but
4926+
/// it helps dereferencing other "container" types to slices,
4927+
/// for example `Box<[T]>` or `MutexGuard<[T]>`.
4928+
#[inline]
4929+
#[unstable(feature = "str_as_str", issue = "130366")]
4930+
pub const fn as_mut_slice(&mut self) -> &mut [T] {
4931+
self
4932+
}
49114933
}
49124934

49134935
impl<T> [MaybeUninit<T>] {

library/std/src/ffi/os_str.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,17 @@ impl OsStr {
12781278
pub fn display(&self) -> Display<'_> {
12791279
Display { os_str: self }
12801280
}
1281+
1282+
/// Returns the same string as a string slice `&OsStr`.
1283+
///
1284+
/// This method is redundant when used directly on `&OsStr`, but
1285+
/// it helps dereferencing other string-like types to string slices,
1286+
/// for example references to `Box<OsStr>` or `Arc<OsStr>`.
1287+
#[inline]
1288+
#[unstable(feature = "str_as_str", issue = "130366")]
1289+
pub const fn as_os_str(&self) -> &OsStr {
1290+
self
1291+
}
12811292
}
12821293

12831294
#[stable(feature = "box_from_os_str", since = "1.17.0")]

library/std/src/path.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,17 @@ impl Path {
32063206
Display { inner: self.inner.display() }
32073207
}
32083208

3209+
/// Returns the same path as `&Path`.
3210+
///
3211+
/// This method is redundant when used directly on `&Path`, but
3212+
/// it helps dereferencing other `PathBuf`-like types to `Path`s,
3213+
/// for example references to `Box<Path>` or `Arc<Path>`.
3214+
#[inline]
3215+
#[unstable(feature = "str_as_str", issue = "130366")]
3216+
pub const fn as_path(&self) -> &Path {
3217+
self
3218+
}
3219+
32093220
/// Queries the file system to get information about a file, directory, etc.
32103221
///
32113222
/// This function will traverse symbolic links to query information about the

0 commit comments

Comments
 (0)