Skip to content

Commit 2f0c05e

Browse files
committed
test: added tests for const-ness
1 parent f60155a commit 2f0c05e

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

library/std/src/ffi/os_str.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,18 @@ impl OsStr {
629629
s.as_ref()
630630
}
631631

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+
/// ```
633644
#[inline]
634645
#[unstable(feature = "const_path", reason = "TBD", issue = "none")]
635646
pub const fn from_str(s: &str) -> &OsStr {

library/std/src/ffi/os_str/tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,10 @@ fn into_rc() {
163163
assert_eq!(&*rc2, os_str);
164164
assert_eq!(&*arc2, os_str);
165165
}
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+
}

library/std/src/path.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,12 +1951,19 @@ impl Path {
19511951
Self::from_os_str(s.as_ref())
19521952
}
19531953

1954-
/// Creates a new `Path` from an `OsStr`.
1954+
/// Creates a new [`Path`] from an [`OsStr`].
19551955
///
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;
19571964
///
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+
/// ```
19601967
#[inline]
19611968
#[unstable(feature = "const_path", reason = "TBD", issue = "none")]
19621969
pub const fn from_os_str(s: &OsStr) -> &Path {

library/std/src/path/tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,13 @@ fn test_ord() {
16651665
ord!(Equal, "foo/bar", "foo/bar//");
16661666
}
16671667

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+
16681675
#[bench]
16691676
fn bench_path_cmp_fast_path_buf_sort(b: &mut test::Bencher) {
16701677
let prefix = "my/home";

0 commit comments

Comments
 (0)