From 6fa7d6ca16f5f3704bfd48fd3721e8ea329e971b Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Wed, 14 Feb 2024 18:30:24 -0500 Subject: [PATCH] Use intrinsic --- library/core/src/fmt/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index f95ab8dd5f39..dde8c0c5e2b6 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -430,6 +430,19 @@ impl<'a> Arguments<'a> { _ => None, } } + + /// Same as `as_str`, but will only return a `Some` value if it can be determined at compile time. + #[inline] + const fn as_const_str(&self) -> Option<&'static str> { + let s = self.as_str(); + // if unsafe { core::intrinsics::is_val_statically_known(matches!((self.pieces, self.args), ([], []) | ([_], []))) } { + if unsafe { core::intrinsics::is_val_statically_known(s) } { + s + } else { + None + } + + } } #[stable(feature = "rust1", since = "1.0.0")]