From c48e79753d8965855ace30cfa595c577c6103868 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Tue, 7 Oct 2025 18:15:17 +0800 Subject: [PATCH] refactor: cancel the namespace Rust. - all sub-functions are put into respective position. --- doc/src/premise_and_principle.dox | 2 +- src/CMakeLists.txt | 13 ++++---- src/yycc/{rust => }/option.hpp | 4 +-- src/yycc/{rust => }/panic.cpp | 8 ++--- src/yycc/{rust => }/panic.hpp | 8 ++--- src/yycc/prelude.hpp | 49 +++++++++++++++++++++++++++++++ src/yycc/{rust => }/primitive.hpp | 3 +- src/yycc/{rust => }/result.hpp | 4 +-- src/yycc/rust/prelude.hpp | 49 ------------------------------- src/yycc/string.hpp | 9 ++++++ test/yycc/constraint.cpp | 2 +- test/yycc/constraint/builder.cpp | 2 +- test/yycc/flag_enum.cpp | 2 +- test/yycc/num/op.cpp | 2 +- test/yycc/num/parse.cpp | 2 +- test/yycc/num/safe_cast.cpp | 2 +- test/yycc/num/safe_op.cpp | 2 +- test/yycc/num/stringify.cpp | 2 +- test/yycc/string/op.cpp | 2 +- test/yycc/string/reinterpret.cpp | 2 +- 20 files changed, 89 insertions(+), 80 deletions(-) rename src/yycc/{rust => }/option.hpp (90%) rename src/yycc/{rust => }/panic.cpp (88%) rename src/yycc/{rust => }/panic.hpp (92%) create mode 100644 src/yycc/prelude.hpp rename src/yycc/{rust => }/primitive.hpp (93%) rename src/yycc/{rust => }/result.hpp (98%) delete mode 100644 src/yycc/rust/prelude.hpp create mode 100644 src/yycc/string.hpp diff --git a/doc/src/premise_and_principle.dox b/doc/src/premise_and_principle.dox index bd6eafc..643b117 100644 --- a/doc/src/premise_and_principle.dox +++ b/doc/src/premise_and_principle.dox @@ -29,7 +29,7 @@ In short words, this library use UTF8 encoding everywhere except some special ca \li Traditional format function in yycc::string::op. Traditional format function provide some overloads for ordinary string formatting. That's because this feature is so common to use in some cases. -\li The message of Rust panic in yycc::rust::panic. +\li The message of Rust panic in yycc::panic. Due to the limitation of \c std::format, we only can use ordinary string as its message content. \li The message of standard library exception. For the compatibility with C++ standard library exception, diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a4c0e59..1ae4469 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,7 +15,7 @@ PRIVATE yycc/string/op.cpp yycc/patch/fopen.cpp yycc/patch/stream.cpp - yycc/rust/panic.cpp + yycc/panic.cpp yycc/env.cpp yycc/windows/com.cpp yycc/windows/dialog.cpp @@ -53,6 +53,7 @@ FILES yycc/macro/class_copy_move.hpp yycc/macro/printf_checker.hpp yycc/flag_enum.hpp + yycc/string.hpp yycc/string/reinterpret.hpp yycc/string/op.hpp yycc/patch/ptr_pad.hpp @@ -64,11 +65,11 @@ FILES yycc/num/safe_cast.hpp yycc/num/safe_op.hpp yycc/num/op.hpp - yycc/rust/prelude.hpp - yycc/rust/primitive.hpp - yycc/rust/panic.hpp - yycc/rust/option.hpp - yycc/rust/result.hpp + yycc/primitive.hpp + yycc/option.hpp + yycc/result.hpp + yycc/prelude.hpp + yycc/panic.hpp yycc/env.hpp yycc/windows/import_guard_head.hpp yycc/windows/import_guard_tail.hpp diff --git a/src/yycc/rust/option.hpp b/src/yycc/option.hpp similarity index 90% rename from src/yycc/rust/option.hpp rename to src/yycc/option.hpp index f327433..a8547c4 100644 --- a/src/yycc/rust/option.hpp +++ b/src/yycc/option.hpp @@ -7,7 +7,7 @@ * This namespace reproduce Rust Option type, and its members Some and None in C++. * However Option is not important than Result, so its implementation is very casual. */ -namespace yycc::rust::option { +namespace yycc::option { template using Option = std::optional; @@ -22,4 +22,4 @@ namespace yycc::rust::option { return OptionType(std::nullopt); } -} // namespace yycc::rust::option +} // namespace yycc::option diff --git a/src/yycc/rust/panic.cpp b/src/yycc/panic.cpp similarity index 88% rename from src/yycc/rust/panic.cpp rename to src/yycc/panic.cpp index 4296135..e77ddc7 100644 --- a/src/yycc/rust/panic.cpp +++ b/src/yycc/panic.cpp @@ -1,6 +1,6 @@ #include "panic.hpp" -#include "../carton/termcolor.hpp" -#include "../patch/stream.hpp" +#include "carton/termcolor.hpp" +#include "patch/stream.hpp" #include #include #include @@ -10,7 +10,7 @@ using namespace yycc::patch::stream; -namespace yycc::rust::panic { +namespace yycc::panic { void panic(const char* file, int line, const std::u8string_view& msg) { // Output message in stderr. @@ -36,4 +36,4 @@ namespace yycc::rust::panic { std::abort(); } -} // namespace yycc::rust::panic +} // namespace yycc::panic diff --git a/src/yycc/rust/panic.hpp b/src/yycc/panic.hpp similarity index 92% rename from src/yycc/rust/panic.hpp rename to src/yycc/panic.hpp index 25f18e1..dcbd479 100644 --- a/src/yycc/rust/panic.hpp +++ b/src/yycc/panic.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../patch/format.hpp" +#include "patch/format.hpp" #include #include @@ -21,7 +21,7 @@ * In this way, unexpected behavior in our code will cause the program to exit immediately, outputting error information and stack traces. * Standard library exceptions will also cause the program to exit, but without stack information. */ -namespace yycc::rust::panic { +namespace yycc::panic { /** * @brief Immediately crashes the entire program like Rust's \c panic! macro. @@ -31,7 +31,7 @@ namespace yycc::rust::panic { * However, this format function is specially modified that it can accept UTF8 format string and UTF8 string argument. * More preciously, it is "format" in \c yycc::patch::format namespace. */ -#define RS_PANIC(msg, ...) ::yycc::rust::panic::panic(__FILE__, __LINE__, ::yycc::patch::format::format(msg __VA_OPT__(, ) __VA_ARGS__)) +#define RS_PANIC(msg, ...) ::yycc::panic::panic(__FILE__, __LINE__, ::yycc::patch::format::format(msg __VA_OPT__(, ) __VA_ARGS__)) /** * @brief Immediately crashes the entire program like Rust's \c panic! macro. @@ -44,4 +44,4 @@ namespace yycc::rust::panic { */ [[noreturn]] void panic(const char* file, int line, const std::u8string_view& msg); -} // namespace yycc::rust::panic +} // namespace yycc::panic diff --git a/src/yycc/prelude.hpp b/src/yycc/prelude.hpp new file mode 100644 index 0000000..cb79d67 --- /dev/null +++ b/src/yycc/prelude.hpp @@ -0,0 +1,49 @@ +#pragma once + +// Rust prelude section +#include "primitive.hpp" +#include "result.hpp" +#include "option.hpp" +#include "panic.hpp" +#include + +namespace yycc::prelude { + // Include primitive types + +#define NS_YYCC_PRIMITIVE ::yycc::primitive + + using i8 = NS_YYCC_PRIMITIVE::i8; + using i16 = NS_YYCC_PRIMITIVE::i16; + using i32 = NS_YYCC_PRIMITIVE::i32; + using i64 = NS_YYCC_PRIMITIVE::i64; + using u8 = NS_YYCC_PRIMITIVE::u8; + using u16 = NS_YYCC_PRIMITIVE::u16; + using u32 = NS_YYCC_PRIMITIVE::u32; + using u64 = NS_YYCC_PRIMITIVE::u64; + + using isize = NS_YYCC_PRIMITIVE::isize; + using usize = NS_YYCC_PRIMITIVE::usize; + + using f32 = NS_YYCC_PRIMITIVE::f32; + using f64 = NS_YYCC_PRIMITIVE::f64; + + using str = NS_YYCC_PRIMITIVE::str; + +#undef NS_YYCC_PRIMITIVE + + // Other types + using String = std::u8string; + template + using Vec = std::vector; + + // Expose Result and Option + using namespace ::yycc::option; + using namespace ::yycc::result; + + // Panic are introduced by including header file + // so we do not need re-expose it. + +} // namespace yycc::prelude + +// Expose all members +using namespace ::yycc::prelude; diff --git a/src/yycc/rust/primitive.hpp b/src/yycc/primitive.hpp similarity index 93% rename from src/yycc/rust/primitive.hpp rename to src/yycc/primitive.hpp index 72d2d61..970a17e 100644 --- a/src/yycc/rust/primitive.hpp +++ b/src/yycc/primitive.hpp @@ -3,7 +3,7 @@ #include #include -namespace yycc::rust::primitive { +namespace yycc::primitive { // `bool` is keyword so should not declare it anymore. // `char` is keyword so should not declare it anymore. @@ -25,4 +25,3 @@ namespace yycc::rust::primitive { using str = std::u8string_view; } - diff --git a/src/yycc/rust/result.hpp b/src/yycc/result.hpp similarity index 98% rename from src/yycc/rust/result.hpp rename to src/yycc/result.hpp index aab4103..eb85566 100644 --- a/src/yycc/rust/result.hpp +++ b/src/yycc/result.hpp @@ -36,7 +36,7 @@ * Similarly, when using \c std::cerr 's \c operator<< overload, you also need to write suitable adapters. * @remarks This namespace only work with environment supporting `std::expected` (i.e. C++ 23). */ -namespace yycc::rust::result { +namespace yycc::result { /** * @brief Equivalent Rust \c Result in C++ @@ -74,4 +74,4 @@ namespace yycc::rust::result { return ResultType(std::unexpect, std::forward(args)...); } -} // namespace yycc::rust::result +} // namespace yycc::result diff --git a/src/yycc/rust/prelude.hpp b/src/yycc/rust/prelude.hpp deleted file mode 100644 index af16733..0000000 --- a/src/yycc/rust/prelude.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once - -// Rust prelude section -#include "primitive.hpp" -#include "result.hpp" -#include "option.hpp" -#include "panic.hpp" -#include - -namespace yycc::rust::prelude { - // Include primitive types - -#define NS_RUST_PRIMITIVE ::yycc::rust::primitive - - using i8 = NS_RUST_PRIMITIVE::i8; - using i16 = NS_RUST_PRIMITIVE::i16; - using i32 = NS_RUST_PRIMITIVE::i32; - using i64 = NS_RUST_PRIMITIVE::i64; - using u8 = NS_RUST_PRIMITIVE::u8; - using u16 = NS_RUST_PRIMITIVE::u16; - using u32 = NS_RUST_PRIMITIVE::u32; - using u64 = NS_RUST_PRIMITIVE::u64; - - using isize = NS_RUST_PRIMITIVE::isize; - using usize = NS_RUST_PRIMITIVE::usize; - - using f32 = NS_RUST_PRIMITIVE::f32; - using f64 = NS_RUST_PRIMITIVE::f64; - - using str = NS_RUST_PRIMITIVE::str; - -#undef NS_RUST_PRIMITIVE - - // Other types - using String = std::u8string; - template - using Vec = std::vector; - - // Expose Result and Option - using namespace ::yycc::rust::option; - using namespace ::yycc::rust::result; - - // Panic are introduced by including header file - // so we do not need re-expose it. - -} // namespace yycc::prelude::rust - -// Expose all members -using namespace ::yycc::rust::prelude; diff --git a/src/yycc/string.hpp b/src/yycc/string.hpp new file mode 100644 index 0000000..3c80bfd --- /dev/null +++ b/src/yycc/string.hpp @@ -0,0 +1,9 @@ +#pragma once + +// TODO: +// Add content safe, Rust-like string container "String" +// and string view "str" in there. +// Once we add it, all string process function can be migrated as their class member, +// or keep them independendly but change the type of parameter into our string types. + +namespace yycc::string {} diff --git a/test/yycc/constraint.cpp b/test/yycc/constraint.cpp index 4f3c245..00e66b3 100644 --- a/test/yycc/constraint.cpp +++ b/test/yycc/constraint.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #define CONSTRAINT ::yycc::constraint::Constraint diff --git a/test/yycc/constraint/builder.cpp b/test/yycc/constraint/builder.cpp index e007267..03c802f 100644 --- a/test/yycc/constraint/builder.cpp +++ b/test/yycc/constraint/builder.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #define BUILDER ::yycc::constraint::builder using namespace std::literals::string_view_literals; diff --git a/test/yycc/flag_enum.cpp b/test/yycc/flag_enum.cpp index 976086a..409f35b 100644 --- a/test/yycc/flag_enum.cpp +++ b/test/yycc/flag_enum.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include #define FLAG_ENUM ::yycc::flag_enum diff --git a/test/yycc/num/op.cpp b/test/yycc/num/op.cpp index 100a567..950c5e9 100644 --- a/test/yycc/num/op.cpp +++ b/test/yycc/num/op.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #define OP ::yycc::num::op diff --git a/test/yycc/num/parse.cpp b/test/yycc/num/parse.cpp index 9f74147..dca6124 100644 --- a/test/yycc/num/parse.cpp +++ b/test/yycc/num/parse.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #define PARSE ::yycc::num::parse diff --git a/test/yycc/num/safe_cast.cpp b/test/yycc/num/safe_cast.cpp index baff1f9..40412cf 100644 --- a/test/yycc/num/safe_cast.cpp +++ b/test/yycc/num/safe_cast.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include #define CAST ::yycc::num::safe_cast diff --git a/test/yycc/num/safe_op.cpp b/test/yycc/num/safe_op.cpp index 1f11f50..e1d8b70 100644 --- a/test/yycc/num/safe_op.cpp +++ b/test/yycc/num/safe_op.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #define OP ::yycc::num::safe_op diff --git a/test/yycc/num/stringify.cpp b/test/yycc/num/stringify.cpp index e908633..d9806c9 100644 --- a/test/yycc/num/stringify.cpp +++ b/test/yycc/num/stringify.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #define STRINGIFY ::yycc::num::stringify diff --git a/test/yycc/string/op.cpp b/test/yycc/string/op.cpp index 5ab270f..7bc2151 100644 --- a/test/yycc/string/op.cpp +++ b/test/yycc/string/op.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #define OP ::yycc::string::op using namespace std::literals::string_view_literals; diff --git a/test/yycc/string/reinterpret.cpp b/test/yycc/string/reinterpret.cpp index 0dbe128..7367f66 100644 --- a/test/yycc/string/reinterpret.cpp +++ b/test/yycc/string/reinterpret.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include #define REINTERPRET ::yycc::string::reinterpret #define AS_UINT8(p) static_cast(p)