Skip to content

Commit 6fa7d6c

Browse files
committed
Use intrinsic
1 parent d712e3f commit 6fa7d6c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

library/core/src/fmt/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,19 @@ impl<'a> Arguments<'a> {
430430
_ => None,
431431
}
432432
}
433+
434+
/// Same as `as_str`, but will only return a `Some` value if it can be determined at compile time.
435+
#[inline]
436+
const fn as_const_str(&self) -> Option<&'static str> {
437+
let s = self.as_str();
438+
// if unsafe { core::intrinsics::is_val_statically_known(matches!((self.pieces, self.args), ([], []) | ([_], []))) } {
439+
if unsafe { core::intrinsics::is_val_statically_known(s) } {
440+
s
441+
} else {
442+
None
443+
}
444+
445+
}
433446
}
434447

435448
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)