From eb0576d364f3e627f4c6bdbfc729289fbce95df9 Mon Sep 17 00:00:00 2001 From: Jakub Zeifert Date: Fri, 4 Oct 2024 11:28:13 +0200 Subject: [PATCH] Refactor cpp.rs formatting and change lazy_static to OnceLock for Cpp_keyword check --- internal/compiler/Cargo.toml | 1 - internal/compiler/generator/cpp.rs | 131 +++++------------------------ 2 files changed, 22 insertions(+), 110 deletions(-) diff --git a/internal/compiler/Cargo.toml b/internal/compiler/Cargo.toml index 8c5afdde4a3..1ef770dbac8 100644 --- a/internal/compiler/Cargo.toml +++ b/internal/compiler/Cargo.toml @@ -52,7 +52,6 @@ itertools = { workspace = true } once_cell = "1" url = "2.2.1" linked_hash_set = "0.1.4" -lazy_static = "1.4" # for processing and embedding the rendered image (texture) image = { workspace = true, optional = true, features = ["default"] } diff --git a/internal/compiler/generator/cpp.rs b/internal/compiler/generator/cpp.rs index 195919f2669..7eeb6efcc8c 100644 --- a/internal/compiler/generator/cpp.rs +++ b/internal/compiler/generator/cpp.rs @@ -8,11 +8,10 @@ use std::fmt::Write; use std::io::BufWriter; - +use std::collections::HashSet; +use std::sync::OnceLock; use lyon_path::geom::euclid::approxeq::ApproxEq; -use lazy_static::lazy_static; -use std::collections::HashSet; /// The configuration for the C++ code generator #[derive(Clone, Debug, Default, PartialEq)] @@ -22,115 +21,29 @@ pub struct Config { pub header_include: String, } -// static initialization of C++ keywords Hashset -lazy_static! { - static ref CPP_KEYWORDS: HashSet<&'static str> = { - let keywords: HashSet<&str> = HashSet::from([ - "alignas", - "alignof", - "and", - "and_eq", - "asm", - "atomic_cancel", - "atomic_commit", - "atomic_noexcept", - "auto", - "bitand", - "bitor", - "bool", - "break", - "case", - "catch", - "char", - "char8_t", - "char16_t", - "char32_t", - "class", - "compl", - "concept", - "const", - "consteval", - "constexpr", - "constinit", - "const_cast", - "continue", - "co_await", - "co_return", - "co_yield", - "decltype", - "default", - "delete", - "do", - "double", - "dynamic_cast", - "else", - "enum", - "explicit", - "export", - "extern", - "false", - "float", - "for", - "friend", - "goto", - "if", - "inline", - "int", - "long", - "mutable", - "namespace", - "new", - "noexcept", - "not", - "not_eq", - "nullptr", - "operator", - "or", - "or_eq", - "private", - "protected", - "public", - "reflexpr", - "register", - "reinterpret_cast", - "requires", - "return", - "short", - "signed", - "sizeof", - "static", - "static_assert", - "static_cast", - "struct", - "switch", - "synchronized", - "template", - "this", - "thread_local", - "throw", - "true", - "try", - "typedef", - "typeid", - "typename", - "union", - "unsigned", - "using", - "virtual", - "void", - "volatile", - "wchar_t", - "while", - "xor", - "xor_eq", - ]); - keywords - }; -} // Check if word is one of C++ keywords fn is_cpp_keyword(word: &str) -> bool { - CPP_KEYWORDS.contains(word) + static CPP_KEYWORDS: OnceLock> = OnceLock::new(); + let keywords = CPP_KEYWORDS.get_or_init(|| { + let keywords: HashSet<&str> = HashSet::from([ + "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", + "atomic_noexcept", "auto", "bitand", "bitor", "bool", "break", "case", "catch", + "char", "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const", + "consteval", "constexpr", "constinit", "const_cast", "continue", "co_await", + "co_return", "co_yield", "decltype", "default", "delete", "do", "double", + "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float", + "for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace", + "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", + "protected", "public", "reflexpr", "register", "reinterpret_cast", "requires", + "return", "short", "signed", "sizeof", "static", "static_assert", "static_cast", + "struct", "switch", "synchronized", "template", "this", "thread_local", "throw", + "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", + "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq", + ]); + keywords + }); + keywords.contains(word) } fn ident(ident: &str) -> String {