Rollup merge of #141467 - cyrgani:const-empty-stringlikes, r=Amanieu

make `OsString::new` and `PathBuf::new` unstably const

Since #129041, `String::into_bytes` is `const`, which allows making `OsString::new` and `PathBuf::new` unstably const now.
Not sure what the exact process for this is; does it need an ACP?
This commit is contained in:
Matthias Krüger 2025-06-04 07:54:33 +02:00 committed by GitHub
commit 88620b400e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 7 additions and 5 deletions

View file

@ -137,7 +137,8 @@ impl OsString {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[inline]
pub fn new() -> OsString {
#[rustc_const_unstable(feature = "const_pathbuf_osstring_new", issue = "141520")]
pub const fn new() -> OsString {
OsString { inner: Buf::from_string(String::new()) }
}

View file

@ -1191,7 +1191,8 @@ impl PathBuf {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[inline]
pub fn new() -> PathBuf {
#[rustc_const_unstable(feature = "const_pathbuf_osstring_new", issue = "141520")]
pub const fn new() -> PathBuf {
PathBuf { inner: OsString::new() }
}

View file

@ -115,7 +115,7 @@ impl Buf {
}
#[inline]
pub fn from_string(s: String) -> Buf {
pub const fn from_string(s: String) -> Buf {
Buf { inner: s.into_bytes() }
}

View file

@ -92,7 +92,7 @@ impl Buf {
}
#[inline]
pub fn from_string(s: String) -> Buf {
pub const fn from_string(s: String) -> Buf {
Buf { inner: Wtf8Buf::from_string(s) }
}

View file

@ -209,7 +209,7 @@ impl Wtf8Buf {
///
/// Since WTF-8 is a superset of UTF-8, this always succeeds.
#[inline]
pub fn from_string(string: String) -> Wtf8Buf {
pub const fn from_string(string: String) -> Wtf8Buf {
Wtf8Buf { bytes: string.into_bytes(), is_known_utf8: true }
}