Skip to content

Commit f3de4aa

Browse files
committed
Make some std::path::Prefix methods const
Insta-stabilize the method `std::path::Prefix::is_verbatim` as const. Also make private methods `is_drive` and `has_implicit_root` const. Possible because of the recent stabilization of const control flow. Also adds a test for `is_verbatim` in a const context.
1 parent 130359c commit f3de4aa

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

library/std/src/path.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,20 @@ impl<'a> Prefix<'a> {
214214
/// assert!(!Disk(b'C').is_verbatim());
215215
/// ```
216216
#[inline]
217+
#[rustc_const_stable(feature = "const_ip", since = "1.48.0")]
217218
#[stable(feature = "rust1", since = "1.0.0")]
218-
pub fn is_verbatim(&self) -> bool {
219+
pub const fn is_verbatim(&self) -> bool {
219220
use self::Prefix::*;
220221
matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..))
221222
}
222223

223224
#[inline]
224-
fn is_drive(&self) -> bool {
225+
const fn is_drive(&self) -> bool {
225226
matches!(*self, Prefix::Disk(_))
226227
}
227228

228229
#[inline]
229-
fn has_implicit_root(&self) -> bool {
230+
const fn has_implicit_root(&self) -> bool {
230231
!self.is_drive()
231232
}
232233
}

src/test/ui/consts/std/path.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-pass
2+
3+
use std::path::Prefix;
4+
5+
fn main() {
6+
const PREFIX : Prefix = Prefix::Disk(2);
7+
8+
const IS_VERBATIM : bool = PREFIX.is_verbatim();
9+
assert!(!IS_VERBATIM);
10+
}

0 commit comments

Comments
 (0)