src: use C++20 consteval for FastStringKey

This makes it easier to avoid unintentional mis-usage of the API.

PR-URL: https://github.com/nodejs/node/pull/59148
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Ilyas Shabi <ilyasshabi94@gmail.com>
This commit is contained in:
Anna Henningsen authored and Antoine du Hamel committed 2025-08-04 13:35:53 +02:00
1 parent 3aee7625b9
commit 9075a1a4bf
2 files changed
+15 -2

No files matched your search

+8 -1
View File
@@ -598,7 +598,14 @@ constexpr bool FastStringKey::operator==(const FastStringKey& other) const {
return name_ == other.name_;
}
constexpr FastStringKey::FastStringKey(std::string_view name)
consteval FastStringKey::FastStringKey(std::string_view name)
: FastStringKey(name, 0) {}
constexpr FastStringKey FastStringKey::AllowDynamic(std::string_view name) {
return FastStringKey(name, 0);
}
constexpr FastStringKey::FastStringKey(std::string_view name, int dummy)
: name_(name), cached_hash_(HashImpl(name)) {}
constexpr std::string_view FastStringKey::as_string_view() const {
+7 -1
View File
@@ -817,7 +817,11 @@ class PersistentToLocal {
// computations.
class FastStringKey {
public:
constexpr explicit FastStringKey(std::string_view name);
// consteval ensures that the argument is a compile-time constant.
consteval explicit FastStringKey(std::string_view name);
// passing something that is not a compile-time constant needs explicit
// opt-in via this helper, as it defeats the purpose of FastStringKey.
static constexpr FastStringKey AllowDynamic(std::string_view name);
struct Hash {
constexpr size_t operator()(const FastStringKey& key) const;
@@ -827,6 +831,8 @@ class FastStringKey {
constexpr std::string_view as_string_view() const;
private:
constexpr explicit FastStringKey(std::string_view name, int dummy);
static constexpr size_t HashImpl(std::string_view str);
const std::string_view name_;