Skip to content

Commit a197ebd

Browse files
committed
{CStr,OsStr,Path}::as_$1
1 parent 2636cb4 commit a197ebd

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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/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
@@ -3208,6 +3208,17 @@ impl Path {
32083208
Display { inner: self.inner.display() }
32093209
}
32103210

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

0 commit comments

Comments
 (0)