File tree Expand file tree Collapse file tree 4 files changed +37
-5
lines changed Expand file tree Collapse file tree 4 files changed +37
-5
lines changed Original file line number Diff line number Diff line change @@ -629,7 +629,18 @@ impl OsStr {
629
629
s. as_ref ( )
630
630
}
631
631
632
- /// Creates a new `OsStr` from a `str`.
632
+ /// Creates a new [`OsStr`] from a [`str`].
633
+ ///
634
+ /// This method supports const expressions. However, if you don't need const,
635
+ /// you should probably use the [`OsStr::new`] method instead.
636
+ ///
637
+ /// # Examples
638
+ ///
639
+ /// ```
640
+ /// use std::ffi::OsStr;
641
+ ///
642
+ /// const OS_STR: OsStr = OsStr::from_str("foo");
643
+ /// ```
633
644
#[ inline]
634
645
#[ unstable( feature = "const_path" , reason = "TBD" , issue = "none" ) ]
635
646
pub const fn from_str ( s : & str ) -> & OsStr {
Original file line number Diff line number Diff line change @@ -163,3 +163,10 @@ fn into_rc() {
163
163
assert_eq ! ( & * rc2, os_str) ;
164
164
assert_eq ! ( & * arc2, os_str) ;
165
165
}
166
+
167
+ #[ test]
168
+ pub fn test_const ( ) {
169
+ const STR : & str = "/foo/bar" ;
170
+ const OS_STR : & OsStr = OsStr :: from_str ( STR ) ;
171
+ assert_eq ! ( OS_STR , OsStr :: new( STR ) ) ;
172
+ }
Original file line number Diff line number Diff line change @@ -1951,12 +1951,19 @@ impl Path {
1951
1951
Self :: from_os_str ( s. as_ref ( ) )
1952
1952
}
1953
1953
1954
- /// Creates a new `Path` from an `OsStr`.
1954
+ /// Creates a new [ `Path`] from an [ `OsStr`] .
1955
1955
///
1956
- /// This is a cost-free conversion.
1956
+ /// This method supports const expressions. However, if you don't need const,
1957
+ /// you should probably use the [`Path::new`] method instead.
1958
+ ///
1959
+ /// # Examples
1960
+ ///
1961
+ /// ```
1962
+ /// use std::ffi::OsStr;
1963
+ /// use std::path::Path;
1957
1964
///
1958
- /// You should probably use the [` Path::new`] method instead,
1959
- /// however, this method supports const expressions
1965
+ /// const PATH: Path = Path::from_os_str(OsStr::from_str("/foo/bar"));
1966
+ /// ```
1960
1967
#[ inline]
1961
1968
#[ unstable( feature = "const_path" , reason = "TBD" , issue = "none" ) ]
1962
1969
pub const fn from_os_str ( s : & OsStr ) -> & Path {
Original file line number Diff line number Diff line change @@ -1665,6 +1665,13 @@ fn test_ord() {
1665
1665
ord ! ( Equal , "foo/bar" , "foo/bar//" ) ;
1666
1666
}
1667
1667
1668
+ #[ test]
1669
+ pub fn test_const ( ) {
1670
+ const STR : & str = "/foo/bar" ;
1671
+ const PATH : & Path = Path :: from_os_str ( OsStr :: from_str ( STR ) ) ;
1672
+ assert_eq ! ( PATH , Path :: new( STR ) ) ;
1673
+ }
1674
+
1668
1675
#[ bench]
1669
1676
fn bench_path_cmp_fast_path_buf_sort ( b : & mut test:: Bencher ) {
1670
1677
let prefix = "my/home" ;
You can’t perform that action at this time.
0 commit comments