Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const Path initialisation #92930

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,27 @@ impl OsStr {
s.as_ref()
}

/// Creates a new [`OsStr`] from a [`str`].
///
/// This method supports const expressions. However, if you don't need const,
/// you should probably use the [`OsStr::new`] method instead.
///
/// # Examples
///
/// ```
/// #![feature(const_path)]
/// use std::ffi::OsStr;
///
/// const OS_STR: &OsStr = OsStr::from_str("foo");
/// ```
#[inline]
#[unstable(feature = "const_path", reason = "TBD", issue = "none")]
pub const fn from_str(s: &str) -> &OsStr {
Self::from_inner(Slice::from_str(s))
}

#[inline]
fn from_inner(inner: &Slice) -> &OsStr {
const fn from_inner(inner: &Slice) -> &OsStr {
// SAFETY: OsStr is just a wrapper of Slice,
// therefore converting &Slice to &OsStr is safe.
unsafe { &*(inner as *const Slice as *const OsStr) }
Expand Down Expand Up @@ -1263,7 +1282,7 @@ impl AsRef<OsStr> for OsString {
impl AsRef<OsStr> for str {
#[inline]
fn as_ref(&self) -> &OsStr {
OsStr::from_inner(Slice::from_str(self))
OsStr::from_str(self)
}
}

Expand Down
7 changes: 7 additions & 0 deletions library/std/src/ffi/os_str/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,10 @@ fn into_rc() {
assert_eq!(&*rc2, os_str);
assert_eq!(&*arc2, os_str);
}

#[test]
pub fn test_const() {
const STR: &str = "/foo/bar";
const OS_STR: &OsStr = OsStr::from_str(STR);
assert_eq!(OS_STR, OsStr::new(STR));
}
22 changes: 21 additions & 1 deletion library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,27 @@ impl Path {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
Self::from_os_str(s.as_ref())
}

/// Creates a new [`Path`] from an [`OsStr`].
///
/// This method supports const expressions. However, if you don't need const,
/// you should probably use the [`Path::new`] method instead.
///
/// # Examples
///
/// ```
/// #![feature(const_path)]
/// use std::ffi::OsStr;
/// use std::path::Path;
///
/// const PATH: &Path = Path::from_os_str(OsStr::from_str("/foo/bar"));
/// ```
#[inline]
#[unstable(feature = "const_path", reason = "TBD", issue = "none")]
pub const fn from_os_str(s: &OsStr) -> &Path {
unsafe { &*(s as *const OsStr as *const Path) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

osstr.as_ref() -> Path already exists. Imo it's better to wait until that can be made const before introducing additional API surface for the sole purpose of supporting constification.

}

/// Yields the underlying [`OsStr`] slice.
Expand Down
7 changes: 7 additions & 0 deletions library/std/src/path/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,13 @@ fn test_ord() {
ord!(Equal, "foo/bar", "foo/bar//");
}

#[test]
pub fn test_const() {
const STR: &str = "/foo/bar";
const PATH: &Path = Path::from_os_str(OsStr::from_str(STR));
assert_eq!(PATH, Path::new(STR));
}

#[test]
#[cfg(unix)]
fn test_unix_absolute() {
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/unix/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ impl Buf {

impl Slice {
#[inline]
fn from_u8_slice(s: &[u8]) -> &Slice {
const fn from_u8_slice(s: &[u8]) -> &Slice {
unsafe { mem::transmute(s) }
}

#[inline]
pub fn from_str(s: &str) -> &Slice {
pub const fn from_str(s: &str) -> &Slice {
Slice::from_u8_slice(s.as_bytes())
}

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/windows/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Buf {

impl Slice {
#[inline]
pub fn from_str(s: &str) -> &Slice {
pub const fn from_str(s: &str) -> &Slice {
unsafe { mem::transmute(Wtf8::from_str(s)) }
}

Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl Wtf8 {
///
/// Since WTF-8 is a superset of UTF-8, this always succeeds.
#[inline]
pub fn from_str(value: &str) -> &Wtf8 {
pub const fn from_str(value: &str) -> &Wtf8 {
unsafe { Wtf8::from_bytes_unchecked(value.as_bytes()) }
}

Expand All @@ -516,7 +516,7 @@ impl Wtf8 {
/// Since the byte slice is not checked for valid WTF-8, this functions is
/// marked unsafe.
#[inline]
unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 {
const unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 {
mem::transmute(value)
}

Expand Down