File tree Expand file tree Collapse file tree 3 files changed +24
-5
lines changed Expand file tree Collapse file tree 3 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -629,8 +629,15 @@ impl OsStr {
629
629
s. as_ref ( )
630
630
}
631
631
632
+ /// Creates a new `OsStr` from a `str`.
632
633
#[ inline]
633
- fn from_inner ( inner : & Slice ) -> & OsStr {
634
+ #[ unstable( feature = "const_path" , reason = "TBD" , issue = "none" ) ]
635
+ pub const fn from_str ( s : & str ) -> & OsStr {
636
+ Self :: from_inner ( Slice :: from_str ( s) )
637
+ }
638
+
639
+ #[ inline]
640
+ const fn from_inner ( inner : & Slice ) -> & OsStr {
634
641
// SAFETY: OsStr is just a wrapper of Slice,
635
642
// therefore converting &Slice to &OsStr is safe.
636
643
unsafe { & * ( inner as * const Slice as * const OsStr ) }
@@ -1249,7 +1256,7 @@ impl AsRef<OsStr> for OsString {
1249
1256
impl AsRef < OsStr > for str {
1250
1257
#[ inline]
1251
1258
fn as_ref ( & self ) -> & OsStr {
1252
- OsStr :: from_inner ( Slice :: from_str ( self ) )
1259
+ OsStr :: from_str ( self )
1253
1260
}
1254
1261
}
1255
1262
Original file line number Diff line number Diff line change @@ -1948,7 +1948,19 @@ impl Path {
1948
1948
/// ```
1949
1949
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1950
1950
pub fn new < S : AsRef < OsStr > + ?Sized > ( s : & S ) -> & Path {
1951
- unsafe { & * ( s. as_ref ( ) as * const OsStr as * const Path ) }
1951
+ Self :: from_os_str ( s. as_ref ( ) )
1952
+ }
1953
+
1954
+ /// Creates a new `Path` from an `OsStr`.
1955
+ ///
1956
+ /// This is a cost-free conversion.
1957
+ ///
1958
+ /// You should probably use the [`Path::new`] method instead,
1959
+ /// however, this method supports const expressions
1960
+ #[ inline]
1961
+ #[ unstable( feature = "const_path" , reason = "TBD" , issue = "none" ) ]
1962
+ pub const fn from_os_str ( s : & OsStr ) -> & Path {
1963
+ unsafe { & * ( s as * const OsStr as * const Path ) }
1952
1964
}
1953
1965
1954
1966
/// Yields the underlying [`OsStr`] slice.
Original file line number Diff line number Diff line change @@ -186,12 +186,12 @@ impl Buf {
186
186
187
187
impl Slice {
188
188
#[ inline]
189
- fn from_u8_slice ( s : & [ u8 ] ) -> & Slice {
189
+ const fn from_u8_slice ( s : & [ u8 ] ) -> & Slice {
190
190
unsafe { mem:: transmute ( s) }
191
191
}
192
192
193
193
#[ inline]
194
- pub fn from_str ( s : & str ) -> & Slice {
194
+ pub const fn from_str ( s : & str ) -> & Slice {
195
195
Slice :: from_u8_slice ( s. as_bytes ( ) )
196
196
}
197
197
You can’t perform that action at this time.
0 commit comments