1
0

refactor: cancel the namespace Rust.

- all sub-functions are put into respective position.
This commit is contained in:
2025-10-07 18:15:17 +08:00
parent eda801d3c7
commit c48e79753d
20 changed files with 89 additions and 80 deletions

View File

@ -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. \li Traditional format function in yycc::string::op.
Traditional format function provide some overloads for ordinary string formatting. Traditional format function provide some overloads for ordinary string formatting.
That's because this feature is so common to use in some cases. 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. 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. \li The message of standard library exception.
For the compatibility with C++ standard library exception, For the compatibility with C++ standard library exception,

View File

@ -15,7 +15,7 @@ PRIVATE
yycc/string/op.cpp yycc/string/op.cpp
yycc/patch/fopen.cpp yycc/patch/fopen.cpp
yycc/patch/stream.cpp yycc/patch/stream.cpp
yycc/rust/panic.cpp yycc/panic.cpp
yycc/env.cpp yycc/env.cpp
yycc/windows/com.cpp yycc/windows/com.cpp
yycc/windows/dialog.cpp yycc/windows/dialog.cpp
@ -53,6 +53,7 @@ FILES
yycc/macro/class_copy_move.hpp yycc/macro/class_copy_move.hpp
yycc/macro/printf_checker.hpp yycc/macro/printf_checker.hpp
yycc/flag_enum.hpp yycc/flag_enum.hpp
yycc/string.hpp
yycc/string/reinterpret.hpp yycc/string/reinterpret.hpp
yycc/string/op.hpp yycc/string/op.hpp
yycc/patch/ptr_pad.hpp yycc/patch/ptr_pad.hpp
@ -64,11 +65,11 @@ FILES
yycc/num/safe_cast.hpp yycc/num/safe_cast.hpp
yycc/num/safe_op.hpp yycc/num/safe_op.hpp
yycc/num/op.hpp yycc/num/op.hpp
yycc/rust/prelude.hpp yycc/primitive.hpp
yycc/rust/primitive.hpp yycc/option.hpp
yycc/rust/panic.hpp yycc/result.hpp
yycc/rust/option.hpp yycc/prelude.hpp
yycc/rust/result.hpp yycc/panic.hpp
yycc/env.hpp yycc/env.hpp
yycc/windows/import_guard_head.hpp yycc/windows/import_guard_head.hpp
yycc/windows/import_guard_tail.hpp yycc/windows/import_guard_tail.hpp

View File

@ -7,7 +7,7 @@
* This namespace reproduce Rust Option type, and its members Some and None in C++. * 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. * However Option is not important than Result, so its implementation is very casual.
*/ */
namespace yycc::rust::option { namespace yycc::option {
template<typename T> template<typename T>
using Option = std::optional<T>; using Option = std::optional<T>;
@ -22,4 +22,4 @@ namespace yycc::rust::option {
return OptionType(std::nullopt); return OptionType(std::nullopt);
} }
} // namespace yycc::rust::option } // namespace yycc::option

View File

@ -1,6 +1,6 @@
#include "panic.hpp" #include "panic.hpp"
#include "../carton/termcolor.hpp" #include "carton/termcolor.hpp"
#include "../patch/stream.hpp" #include "patch/stream.hpp"
#include <cstdlib> #include <cstdlib>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
@ -10,7 +10,7 @@
using namespace yycc::patch::stream; using namespace yycc::patch::stream;
namespace yycc::rust::panic { namespace yycc::panic {
void panic(const char* file, int line, const std::u8string_view& msg) { void panic(const char* file, int line, const std::u8string_view& msg) {
// Output message in stderr. // Output message in stderr.
@ -36,4 +36,4 @@ namespace yycc::rust::panic {
std::abort(); std::abort();
} }
} // namespace yycc::rust::panic } // namespace yycc::panic

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "../patch/format.hpp" #include "patch/format.hpp"
#include <string_view> #include <string_view>
#include <format> #include <format>
@ -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. * 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. * 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. * @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. * 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. * 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. * @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); [[noreturn]] void panic(const char* file, int line, const std::u8string_view& msg);
} // namespace yycc::rust::panic } // namespace yycc::panic

49
src/yycc/prelude.hpp Normal file
View File

@ -0,0 +1,49 @@
#pragma once
// Rust prelude section
#include "primitive.hpp"
#include "result.hpp"
#include "option.hpp"
#include "panic.hpp"
#include <vector>
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<typename T>
using Vec = std::vector<T>;
// 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;

View File

@ -3,7 +3,7 @@
#include <cstddef> #include <cstddef>
#include <string_view> #include <string_view>
namespace yycc::rust::primitive { namespace yycc::primitive {
// `bool` is keyword so should not declare it anymore. // `bool` is keyword so should not declare it anymore.
// `char` 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; using str = std::u8string_view;
} }

View File

@ -36,7 +36,7 @@
* Similarly, when using \c std::cerr 's \c operator<< overload, you also need to write suitable adapters. * 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). * @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++ * @brief Equivalent Rust \c Result in C++
@ -74,4 +74,4 @@ namespace yycc::rust::result {
return ResultType(std::unexpect, std::forward<Args>(args)...); return ResultType(std::unexpect, std::forward<Args>(args)...);
} }
} // namespace yycc::rust::result } // namespace yycc::result

View File

@ -1,49 +0,0 @@
#pragma once
// Rust prelude section
#include "primitive.hpp"
#include "result.hpp"
#include "option.hpp"
#include "panic.hpp"
#include <vector>
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<typename T>
using Vec = std::vector<T>;
// 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;

9
src/yycc/string.hpp Normal file
View File

@ -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 {}

View File

@ -2,7 +2,7 @@
#include <yycc.hpp> #include <yycc.hpp>
#include <yycc/constraint.hpp> #include <yycc/constraint.hpp>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define CONSTRAINT ::yycc::constraint::Constraint #define CONSTRAINT ::yycc::constraint::Constraint

View File

@ -2,7 +2,7 @@
#include <yycc.hpp> #include <yycc.hpp>
#include <yycc/constraint/builder.hpp> #include <yycc/constraint/builder.hpp>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define BUILDER ::yycc::constraint::builder #define BUILDER ::yycc::constraint::builder
using namespace std::literals::string_view_literals; using namespace std::literals::string_view_literals;

View File

@ -3,7 +3,7 @@
#include <yycc/flag_enum.hpp> #include <yycc/flag_enum.hpp>
#include <cinttypes> #include <cinttypes>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define FLAG_ENUM ::yycc::flag_enum #define FLAG_ENUM ::yycc::flag_enum

View File

@ -2,7 +2,7 @@
#include <yycc.hpp> #include <yycc.hpp>
#include <yycc/num/op.hpp> #include <yycc/num/op.hpp>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define OP ::yycc::num::op #define OP ::yycc::num::op

View File

@ -2,7 +2,7 @@
#include <yycc.hpp> #include <yycc.hpp>
#include <yycc/num/parse.hpp> #include <yycc/num/parse.hpp>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define PARSE ::yycc::num::parse #define PARSE ::yycc::num::parse

View File

@ -3,7 +3,7 @@
#include <yycc/num/safe_cast.hpp> #include <yycc/num/safe_cast.hpp>
#include <yycc/macro/ptr_size_detector.hpp> #include <yycc/macro/ptr_size_detector.hpp>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define CAST ::yycc::num::safe_cast #define CAST ::yycc::num::safe_cast

View File

@ -4,7 +4,7 @@
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define OP ::yycc::num::safe_op #define OP ::yycc::num::safe_op

View File

@ -2,7 +2,7 @@
#include <yycc.hpp> #include <yycc.hpp>
#include <yycc/num/stringify.hpp> #include <yycc/num/stringify.hpp>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define STRINGIFY ::yycc::num::stringify #define STRINGIFY ::yycc::num::stringify

View File

@ -2,7 +2,7 @@
#include <yycc.hpp> #include <yycc.hpp>
#include <yycc/string/op.hpp> #include <yycc/string/op.hpp>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define OP ::yycc::string::op #define OP ::yycc::string::op
using namespace std::literals::string_view_literals; using namespace std::literals::string_view_literals;

View File

@ -3,7 +3,7 @@
#include <yycc.hpp> #include <yycc.hpp>
#include <yycc/string/reinterpret.hpp> #include <yycc/string/reinterpret.hpp>
#include <yycc/rust/prelude.hpp> #include <yycc/prelude.hpp>
#define REINTERPRET ::yycc::string::reinterpret #define REINTERPRET ::yycc::string::reinterpret
#define AS_UINT8(p) static_cast<u8>(p) #define AS_UINT8(p) static_cast<u8>(p)